blob: e1bfaaf8a5e8aec1abc55768ecdd8500773974f2 (
plain)
1 /**
2 * I3cstatus prints a configurable status bar for the i3 window manager
3 * Copyright (C) 2021 Aaron Ball <nullspoon@oper.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "common.h"
19 #include <stdio.h>
20
21 int str_is_empty(char* str) {
22 int i = 0;
23 while(str[i] == ' ' || str[i] == '\t')
24 i++;
25 if(str[i] == '\0' || str[i] == '\n')
26 return 1;
27 return 0;
28 }
29
30 char* trim(char* str) {
31 int i = 0;
32 char* start;
33
34 // Move the cursor forward
35 while(str[i] == ' ' || str[i] == '\t')
36 i++;
37 start = &str[i];
38
39 // Reset i to end of string
40 i = strlen(str) - 1;
41 while(str[i] == ' ' || str[i] == '\t' || str[i] == '\n')
42 i--;
43
44 if(str[i] != '\0')
45 str[i + 1] = '\0';
46 return start;
47 }
48
49 void print_bar(int width, float percent, char* buf) {
50 double fill = width * percent;
51 int i = 1;
52 int remainder = 0;
53
54 buf[0] = '[';
55
56 while(i < fill) {
57 buf[i] = ':';
58 i++;
59 }
60
61 remainder = (fill + 1 - i) * 10;
62 if(i > fill && remainder >= 5.0) {
63 buf[i] = ':';
64 i++;
65 } else if(i > fill && remainder < 5.0 && remainder > 0) {
66 buf[i] = '.';
67 i++;
68 }
69
70 while(i < width) {
71 buf[i] = ' ';
72 i++;
73 }
74
75 buf[i] = ']';
76 buf[i+1] = '\0';
77 }
|