summaryrefslogtreecommitdiff
path: root/src/common.c
blob: 011dda87a479460a6864701b7baefdf80d76f372 (plain)
    1 /**
    2  * I3cstat prints a configurable status bar for the i3 window manager
    3  * Copyright (C) 2022 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   int fill_maj = width * percent;
   51   int fill_min = (width * percent - fill_maj) * 10;
   52   int i = 1;
   53 
   54   buf[0] = '[';
   55 
   56   while(i <= fill_maj) {
   57     buf[i] = ':';
   58     i++;
   59   }
   60 
   61   if(fill_maj > 0 && fill_min > 0) {
   62     if(fill_min >= 5)
   63       buf[i] = ':';
   64     else
   65       buf[i] = '.';
   66     i++;
   67   }
   68 
   69   while(i <= width) {
   70     buf[i] = ' ';
   71     i++;
   72   }
   73 
   74   buf[i] = ']';
   75   buf[i+1] = '\0';
   76 }

Generated by cgit