diff options
Diffstat (limited to 'src/common.c')
-rw-r--r-- | src/common.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/common.c b/src/common.c new file mode 100644 index 0000000..6d5d540 --- /dev/null +++ b/src/common.c @@ -0,0 +1,77 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball <nullspoon@oper.io> + * + * 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/>. + */ +#include "common.h" +#include <stdio.h> + +int str_is_empty(char* str) { + int i = 0; + while(str[i] == ' ' || str[i] == '\t') + i++; + if(str[i] == '\0' || str[i] == '\n') + return 1; + return 0; +} + +char* trim(char* str) { + int i = 0; + char* start; + + // Move the cursor forward + while(str[i] == ' ' || str[i] == '\t') + i++; + start = &str[i]; + + // Reset i to end of string + i = strlen(str) - 1; + while(str[i] == ' ' || str[i] == '\t' || str[i] == '\n') + i--; + + if(str[i] != '\0') + str[i + 1] = '\0'; + return start; +} + +void print_bar(int width, float percent, char* buf) { + double fill = width * percent; + int i = 1; + int remainder = 0; + + buf[0] = '['; + + while(i < fill) { + buf[i] = ':'; + i++; + } + + remainder = (fill + 1 - i) * 10; + if(i > fill && remainder >= 5.0) { + buf[i] = ':'; + i++; + } else if(i > fill && remainder < 5.0 && remainder > 0) { + buf[i] = '.'; + i++; + } + + while(i < width) { + buf[i] = ' '; + i++; + } + + buf[i] = ']'; + buf[i+1] = '\0'; +} |