💾 Archived View for thrig.me › blog › 2024 › 05 › 12 › badui.c captured on 2024-07-09 at 02:24:53.

View Raw

More Information

⬅️ Previous capture (2024-05-12)

-=-=-=-=-=-=-

// badui - a bad user interface with latency and such

#include <ctype.h>
#include <curses.h>
#include <locale.h>
#include <stdlib.h>
#include <time.h>

// This may need to be set higher if you are a very slow typer.
#define NOISE 20

void
latency(void)
{
	int n = NOISE * 2;
	while (1) {
		int r = rand() % NOISE;
		if (r) {
			n += r;
			break;
		} else
			n += NOISE;
	}
	napms(n);
}

int
maybe(int odds)
{
	return rand() % 100 < odds;
}

void
prompt(void)
{
	while (1) {
		move(0, 0);
		clear();
		addstr("Are you sure? [Y/N]");
		if (maybe(20)) latency();
		if (maybe(50)) flushinp();
		int ch = getch();
		if (maybe(1)) continue;
		if (ch == 'Y' || ch == 'N') break;
	}
	move(0, 0);
	clear();
}

void
spam(void)
{
	int y, x;
	getyx(stdscr, y, x);
	for (size_t n = 0; n < 3; ++n) {
		move(LINES - 1, 0);
		clrtoeol();
		addstr(maybe(75) ? "spam!" : "lovely spam!");
		latency();
		clrtoeol();
	}
	move(y, x);
}

void
whoops(void)
{
	int y, x;
	getyx(stdscr, y, x);
	if (x) move(y, x - 1);
}

int
main(int argc, char *argv[])
{
	setlocale(LC_ALL, "");
	initscr();
	keypad(stdscr, TRUE);
	cbreak();
	typeahead(-1);
	noecho();
	srand(time(NULL));
	napms(1000 * (3 + rand() % 33));
	addstr("Just type... (or 'Q' to quit)\n");
	while (1) {
		if (maybe(5)) spam();
		latency();
		if (maybe(80)) flushinp();
		int ch = getch();
		if (ch == 'Q' || (maybe(1) && maybe(1))) break;
		if (maybe(5)) continue;
		if (maybe(2)) {
			prompt();
			continue;
		}
		latency();
		if (ch == 127)
			whoops();
		else if (isprint(ch))
			addch(ch);
	}
	endwin();
}