welcome - welcome - a terminal program which always greets you

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

About | Log | Files | Refs | License

welcome.c (4982B)


/* 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;
}