Skip to content

Instantly share code, notes, and snippets.

@yeti0904
Created August 13, 2021 13:35
Show Gist options
  • Save yeti0904/43784adea6a671100f0447e193c192cb to your computer and use it in GitHub Desktop.
Save yeti0904/43784adea6a671100f0447e193c192cb to your computer and use it in GitHub Desktop.
Terminal resizeable box
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <stdbool.h>
#include <sys/ioctl.h>
#define clear() printf("\033[H\033[J")
#define gotoxy(x,y) printf("\033[%d;%dH", (y), (x))
void limitFPS(int fps) {
usleep((int)round(1000000 / fps));
}
void printbox(int width, int height) {
printf("+");
for (int i = 0; i<width - 2; ++i) {
printf("-");
}
printf("+");
for (int i = 0; i<height - 2; ++i) {
printf("|");
for (int j = 0; j<width - 2; ++j) {
printf(" ");
}
printf("|\n");
}
printf("+");
for (int i = 0; i<width - 2; ++i) {
printf("-");
}
printf("+");
}
int main (void) {
struct winsize win;
clear();
while (true) {
gotoxy(0, 0);
ioctl(0, TIOCGWINSZ, &win);
printbox(win.ws_col, win.ws_row);
limitFPS(60);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment