-
Notifications
You must be signed in to change notification settings - Fork 0
/
battery_default.cpp
138 lines (105 loc) · 3.27 KB
/
battery_default.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* An alternative to the official tmux-battery plugin
* written mostly in C++ with a nice gauge
*
* Copyright (C) 2018/2019 Geoffroy Montane
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// In order to install it, see README.md.
// In order to change colors or other things, change the code below and
// recompile by executing install.sh again once you have finished.
// In order to use acpi instead of upower, see battery.sh.
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main(int argc,char *argv[]){
// Gauge length (count of chars)
int bLen(6);
// Path to the directory of this plugin
string path(argv[0]);
// Output from upower or acpi
string cliOutput(argv[1]);
/*
COLORS - (Execute tmuxcolors.sh to see all colors available in your terminal.)
*/
// Color supposed to be red
string colorR("colour1");
// Color supposed to be green
string colorG("colour2");
// Color supposed to be yellow
string colorY("colour3");
// Background color
string color("colour15");
// Text color
string colorTxt("colour16");
// Critical threshold of battery in % : if less, the bar turns to yellow.
int thresholdY(70);
// Critical threshold of battery in % : if less, the bar turns to red.
int thresholdR(30);
/*
PARSING
*/
int index(cliOutput.find("%"));
string percentage;
int percentage_;
int progress(-1);
string progress_("");
string blank_("");
char state('~');
if(index==string::npos){
cout << "?" << endl;
return 0;
}
else{
percentage=cliOutput.substr(index-3,3);
percentage_=stoi(percentage);
progress=roundf(stof(percentage)*bLen/100);
}
if(cliOutput.find("discharging")!=string::npos || cliOutput.find("Discharging")!=string::npos){
state='-';
}
else if(cliOutput.find("charging")!=string::npos || cliOutput.find("Charging")!=string::npos){
state='+';
}
for(int i=0;i<progress;i++){
progress_=progress_+state;
state=' ';
}
for(int i=0;i<bLen-progress;i++){
blank_=blank_+state;
state=' ';
}
/*
Output
*/
string *gaugeColor(&colorG);
if(percentage_<=thresholdY){
gaugeColor=&colorY;
}
if(percentage_<=thresholdR){
gaugeColor=&colorR;
}
if(progress!=bLen){
cout << "#[fg=" << colorTxt << ",bg=" << *gaugeColor << ",bold][" << progress_
<< "#[bg=" << color << ",bold]" << blank_ << "]#[fg=" << colorTxt << ",bg="
<< color << ",bold]" << percentage << "%" << "#[fg=default,bg=default]" << endl;
}
else{
cout << "#[fg=" << colorTxt << ",bg=" << colorG << ",bold][" << progress_
<< "]#[bg=" << color << ",bold]#[fg=" << colorTxt << ",bg=" << color
<< ",bold]" << percentage << "%" << "#[fg=default,bg=default]"<< endl;
}
return 0;
}