welcome.c (4982B)
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | /* See LICENSE file for copyright and license details. */ #include <curses.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <curl/curl.h> #include <locale.h> #define MAXPIPE 1024 #define EVBOX_W 50 typedef struct { char *ptr; size_t len; } string; void init_string(string *s) { s->len = 0; s->ptr = malloc(s->len+1); if (s->ptr == NULL) { fprintf(stderr, "malloc() failed\n"); exit(EXIT_FAILURE); } s->ptr[0] = '\0'; } size_t writefunc(void *ptr, size_t size, size_t nmemb, string *s) { size_t new_len = s->len + size*nmemb; s->ptr = realloc(s->ptr, new_len+1); if (s->ptr == NULL) { fprintf(stderr, "realloc() failed\n"); exit(EXIT_FAILURE); } memcpy(s->ptr+s->len, ptr, size*nmemb); s->ptr[new_len] = '\0'; s->len = new_len; return size*nmemb; } int curl(char* input, string* output) { CURL *curl; CURLcode res; curl = curl_easy_init(); if(curl) { //string s; //init_string(&s); curl_easy_setopt(curl, CURLOPT_URL, input); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_WRITEDATA, output); res = curl_easy_perform(curl); //printf("Inside curl: %s", output->ptr); //free(s.ptr); /* always cleanup */ curl_easy_cleanup(curl); } 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 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) shift=strlen(buf)/2; 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"); } return 0; } int main() { setlocale(LC_ALL, ""); initscr(); cbreak(); noecho(); curs_set(0); start_color(); use_default_colors(); init_pair(1, COLOR_RED, -1); init_pair(2, COLOR_GREEN, -1); 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 * *****************************/ string location; init_string(&location); string temperature; init_string(&temperature); string precip; init_string(&precip); string sunlife; init_string(&sunlife); string wind; init_string(&wind); char *events = calloc(MAXPIPE + 1,sizeof(char)); char *today = calloc(MAXPIPE + 1,sizeof(char)); char *skull = calloc(MAXPIPE + 1,sizeof(char)); 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); if(events[0]=='\0') strcpy(events,"🎉 Looks like you have not planned anything 🎉\n" ); /*************************** * 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); wprintw(mainframe,"Wind speed:\t\t%s\n",wind.ptr); wprintw(mainframe,"Precipitation:\t\t%s\n",precip.ptr); wprintw(mainframe,"Sunrise and sunset:\t%s\n",sunlife.ptr); WINDOW *BOARD[10]; refresh(); wprintw(mainframe,"%s\n", skull); wrefresh(mainframe); for (int i = 0; i < 9; i++) { BOARD[i]=derwin(mainframe,8,21,20,i*20); wattron(BOARD[i],COLOR_PAIR(2)); wprintw(BOARD[i],skull); wrefresh(BOARD[i]); napms(1000); } WINDOW* fr = newwin(LINES/2,EVBOX_W,LINES/2,(COLS-EVBOX_W)/2); WINDOW* c = derwin(fr,LINES/2-2,EVBOX_W-4,1,2); box(fr,0,0); //wmove(fr, 0, 0); //printw("Events within next 7 days:\t%s\n",events); char evts_title[]="Events within next 7 days\n\n"; mvwaddstr(c,0,(EVBOX_W-strlen(evts_title))/2-1,evts_title); wprintw(c,events); wrefresh(fr); getch(); endwin(); /* TODO: Clean memory <11-12-23, bakar> */ return 0; } |