welcome - welcome - a terminal program which always greets you

git clone git://git.bcharge.de/welcome.git

About | Log | Files | Refs | License

commit b22ec601297a9249437d3567fc0390e5a40cad87
parent 45d357e410024113a778dd18a174728673d152fc
Author: Bakar Chargeishvili <bakar@bcharge.de>
Date:   Tue, 17 Dec 2024 18:18:25 +0100

Detemine max_width inside syspipe

Diffstat:
Mwelcome.c | 131+++++++++++++++++++++++++++++++++++++++++++------------------------------------
1 file changed, 72 insertions(+), 59 deletions(-)

diff --git a/welcome.c b/welcome.c @@ -65,56 +65,66 @@ int curl(char* input, string* output) return 0; } -int syspipe(char* input, char* output) { - //char buf[11500] = {0}; - char* buf = output; - - FILE *fp; - - if ((fp = popen(input, "r")) == NULL) { - printf("Error opening pipe!\n"); - } - size_t len=MAXPIPE; - getdelim(&buf, &len, '\0', fp); - //while (fgets(buf, 400, fp) != NULL) { - //mvaddstr(++lineno, COLS/2-shift, buf); - //printw(buf); - //printf("OUTPUT: %s", buf); - //} - //printf("OUTPUT: %s", output); - - if (pclose(fp)) { - printf("Command not found or exited with error status\n"); - } - - return 0; +int syspipe(char* input, char* output, size_t* max_width) { + //char buf[11500] = {0}; + char* buf = output; + + FILE *fp; + + if ((fp = popen(input, "r")) == NULL) { + printf("Error opening pipe!\n"); + } + size_t len=MAXPIPE; + //printf("INPUT: %s\n", input); + getdelim(&buf, &len, '\0', fp); + //printf("OUTPUT: %s", output); + *max_width = 0; + size_t current = 0; + int ch; + for (int i = 0; buf[i] != 0; i++) { + ch = buf[i]; + if(ch == '\n') { + if(current > *max_width) *max_width = current; + current = 0; + } + else { + current++; + } + } + if(current > *max_width) *max_width = current; + + if (pclose(fp)) { + printf("Command not found or exited with error status\n"); + } + + return 0; } int print_greet(WINDOW* frame) { - char buf[500] = {0}; - const char *cmd = "figlet Welcome Bakar!"; - - FILE *fp; - - if ((fp = popen(cmd, "r")) == NULL) { - printf("Error opening pipe!\n"); - } - //ssize_t bytes_read = getdelim(&buf, &len, '\0', fp); - int lineno = 0; - int shift = 0; - while (fgets(buf, 400, fp) != NULL) { - if(lineno==0) + char buf[500] = {0}; + const char *cmd = "figlet Welcome Bakar!"; + + FILE *fp; + + if ((fp = popen(cmd, "r")) == NULL) { + printf("Error opening pipe!\n"); + } + //ssize_t bytes_read = getdelim(&buf, &len, '\0', fp); + int lineno = 0; + int shift = 0; + while (fgets(buf, 400, fp) != NULL) { + if(lineno==0) shift=strlen(buf)/2; - mvwaddstr(frame,++lineno, COLS/2-shift, buf); - //printw(buf); - //printf("OUTPUT: %s", buf); - } + mvwaddstr(frame,++lineno, COLS/2-shift, buf); + //printw(buf); + //printf("OUTPUT: %s", buf); + } - if (pclose(fp)) { - printf("Command not found or exited with error status\n"); - } + if (pclose(fp)) { + printf("Command not found or exited with error status\n"); + } - return 0; + return 0; } int main() @@ -133,14 +143,14 @@ int main() WINDOW* mainframe = newwin(LINES,COLS-2,0,1); wattron(mainframe,COLOR_PAIR(1)); //printw("Hello World !!!"); - wattron(mainframe,A_BLINK); - wattron(mainframe,A_BOLD); - print_greet(mainframe); - wattroff(mainframe,A_BLINK); - - /***************************** - * Extract the information * - *****************************/ + wattron(mainframe,A_BLINK); + wattron(mainframe,A_BOLD); + print_greet(mainframe); + wattroff(mainframe,A_BLINK); + + /***************************** + * Extract the information * + *****************************/ string location; init_string(&location); string temperature; @@ -154,22 +164,25 @@ int main() char *events = calloc(MAXPIPE + 1,sizeof(char)); char *today = calloc(MAXPIPE + 1,sizeof(char)); char *skull = calloc(MAXPIPE + 1,sizeof(char)); + size_t w_pipe_events = 0; + size_t w_pipe_today = 0; + size_t w_pipe_skull = 0; curl("wttr.in/?format=%l",&location); curl("wttr.in/?format=%t%20(%f)",&temperature); curl("wttr.in/?format=%w",&wind); curl("wttr.in/?format=%p",&precip); curl("wttr.in/?format=%S%20-%20%s",&sunlife); - syspipe("calcurse -r7 --format-apt='- %S -> %E %m\n' --output-datefmt=\"%B %d, %Y\"",events); - syspipe("date '+%B %d, %Y' | tr -d '\n'",today); - syspipe("cat skull",skull); + syspipe("calcurse -r7 --format-apt='- %S -> %E %m\n' --output-datefmt=\"%B %d, %Y\"",events,&w_pipe_events); + syspipe("date '+%B %d, %Y' | tr -d '\n'",today,&w_pipe_today); + syspipe("cat skull",skull,&w_pipe_skull); if(events[0]=='\0') strcpy(events,"🎉 Looks like you have not planned anything 🎉\n" - ); + ); /*************************** - * Print the information * - ***************************/ + * Print the information * + ***************************/ wprintw(mainframe,"Today is:\t\t%s\n",today); wprintw(mainframe,"We are in:\t\t%s\n",location.ptr); wprintw(mainframe,"Temperature:\t\t%s\n",temperature.ptr); @@ -180,7 +193,7 @@ int main() refresh(); wprintw(mainframe,"%s\n", skull); wrefresh(mainframe); - for (int i = 0; i < 9; i++) { + for (int i = 0; i < COLS/w_pipe_skull; i++) { BOARD[i]=derwin(mainframe,8,21,20,i*20); wattron(BOARD[i],COLOR_PAIR(2)); wprintw(BOARD[i],skull);