Skip to content

Instantly share code, notes, and snippets.

@kiwiyou
Last active July 3, 2020 16:44
Show Gist options
  • Save kiwiyou/2abe40bccf552dc5a29d8352a357ec9f to your computer and use it in GitHub Desktop.
Save kiwiyou/2abe40bccf552dc5a29d8352a357ec9f to your computer and use it in GitHub Desktop.
draw square interactively
#include <stdio.h>
#include <curses.h>
int sq(int w, int h) {
int x, y = 1;
while (y <= h) {
x = 1;
while (x <= w) {
if ((x == 1 || x == w) && (y == 1 || y == h)) {
addch('o');
} else if (x == 1 || x == w) {
addch('|');
} else if (y == 1 || y == h) {
addch('-');
} else {
addch(' ');
}
++x;
}
addch('\n');
++y;
}
}
int main() {
int key;
int w = 1, h = 1;
int i;
int redraw = true;
initscr();
noecho();
curs_set(0);
nodelay(stdscr, true);
keypad(stdscr, true);
while (true) {
if (redraw) {
move(0, 0);
printw("sq(%u, %u)\n", w, h);
sq(w, h);
refresh();
} else {
timeout(1000);
}
switch (key = getch()) {
case KEY_UP:
if (h < 10) {
++h;
redraw = true;
}
break;
case KEY_DOWN:
if (h > 1) {
move(h, 0);
clrtoeol();
--h;
redraw = true;
}
break;
case 'w':
case 'W':
if (w < 30) {
++w;
redraw = true;
}
break;
case 's':
case 'S':
if (w > 1) {
--w;
i = 0;
while (i < h) {
move(i, w);
clrtoeol();
++i;
}
redraw = true;
}
break;
case 'q':
case 'Q':
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment