Last active
August 18, 2024 18:10
-
-
Save yne/c71104cb03f9fb307d72d43a3d13d74d to your computer and use it in GitHub Desktop.
CLI Chat Mockup for libstrophe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/ioctl.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdbool.h> | |
#include <string.h> | |
#define POS(ROW,COL) printf("\033[%i;%iH", ROW, COL) | |
struct winsize w; | |
void clear_screen() | |
{ | |
ioctl(0, TIOCGWINSZ, &w); | |
for(int row=0;row<=w.ws_row;row++) | |
for(int col=0;col<=w.ws_col;col++) | |
POS(row,col),printf(" "); | |
} | |
bool read_user(char*msg,int len) | |
{ | |
ioctl(0, TIOCGWINSZ, &w); | |
POS(w.ws_row, 0),printf(">"); | |
if(fgets(msg, len, stdin) == NULL) | |
return false; | |
/* remove trailing linefeed */ | |
msg[strcspn(msg, "\r\n")] = 0; | |
return true; | |
} | |
void print_received(char* message){ | |
int len = strlen(message); | |
ioctl(0, TIOCGWINSZ, &w); | |
POS(w.ws_row, w.ws_col-len),printf("%s<\n", message); | |
} | |
int main (void){ | |
clear_screen(); | |
char my_message[1024]; | |
while(read_user(my_message, sizeof(my_message))){ | |
//send(my_message); | |
/* pseudo-random reply chance */ | |
if(*my_message&1) | |
print_received(my_message); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment