Make terminal ui binaries work well everywhere

Here's some screenshots of an emulator tui program that was compiled on
Linux, then scp'd it to Windows, Mac, and FreeBSD.

https://justine.storage.googleapis.com/blinkenlights-cmdexe.png
https://justine.storage.googleapis.com/blinkenlights-imac.png
https://justine.storage.googleapis.com/blinkenlights-freebsd.png
https://justine.storage.googleapis.com/blinkenlights-lisp.png

How is this even possible that we have a nontrivial ui binary that just
works on Mac, Windows, Linux, and BSD? Surely a first ever achievement.

Fixed many bugs. Bootstrapped John McCarthy's metacircular evaluator on
bare metal in half the size of Altair BASIC (about 2.5kb) and ran it in
emulator for fun and profit.
This commit is contained in:
Justine Tunney
2020-10-10 21:18:53 -07:00
parent 680daf1210
commit 9e3e985ae5
276 changed files with 7026 additions and 3790 deletions

View File

@@ -35,8 +35,8 @@ const char *kShades[] = {
jmp_buf jb_;
double light_[3] = {-50, 0, 50};
struct Sphere pos_ = {20, 20, 20, 20};
struct Sphere neg_ = {1, 1, -6, 20};
struct Sphere pos_ = {11, 11, 11, 11};
struct Sphere neg_ = {1, 1, -4, 11};
static void OnCtrlC(int sig) {
longjmp(jb_, 1);
@@ -128,7 +128,7 @@ int main() {
double ang;
struct termios old;
if (cancolor()) {
ttyhidecursor(fileno(stdout));
WRITE("\e[?25l");
if (!setjmp(jb_)) {
xsigaction(SIGINT, OnCtrlC, 0, 0, NULL);
ang = 0;
@@ -143,7 +143,7 @@ int main() {
usleep(1. / FRAMERATE * 1e6);
}
}
ttyshowcursor(fileno(stdout));
WRITE("\e[0m\e[H\e[J\e[?25h");
return 0;
} else {
return 1;

View File

@@ -144,10 +144,7 @@ Effects Shortcuts:\n\
H +Hue ALT+H -Hue\n\
S +Saturation ALT+S -Saturation\n\
L +Lightness ALT+L -Lightness\n\
F1 {Unsharp,Sharp}\n\
F2 {Gaussian,Boxblur}\n\
F3 Sobel\n\
F4 Emboss\n\
CTRL-G {Unsharp,Sharp}\n\
\n\
Environment Variables:\n\
SOX overrides location of SoX executable\n\
@@ -1157,8 +1154,6 @@ static optimizesize void ReadKeyboard(void) {
case CTRL('G'):
sharp_ = (sharp_ + 1) % kSharpMAX;
break;
case CTRL('B'):
longjmp(jb_, 1);
case CTRL('\\'):
raise(SIGQUIT);
break;

102
tool/viz/tailf.c Normal file
View File

@@ -0,0 +1,102 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2020 Justine Alexandra Roberts Tunney │
│ │
│ This program is free software; you can redistribute it and/or modify │
│ it under the terms of the GNU General Public License as published by │
│ the Free Software Foundation; version 2 of the License. │
│ │
│ This program is distributed in the hope that it will be useful, but │
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
│ General Public License for more details. │
│ │
│ You should have received a copy of the GNU General Public License │
│ along with this program; if not, write to the Free Software │
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
│ 02110-1301 USA │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/bits/safemacros.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sig.h"
#include "libc/time/time.h"
#include "libc/x/x.h"
/**
* @fileoverview tail -f with lower poll rate
* @see busybox not having interval flag
*/
int fd;
bool exited;
struct stat st;
char buf[FRAMESIZE];
int WriteString(const char *s) {
return write(1, s, strlen(s));
}
void HideCursor(void) {
WriteString("\e[?25l");
}
void ShowCursor(void) {
WriteString("\e[?25h");
}
void OnInt(void) {
exited = true;
}
void OnExit(void) {
ShowCursor();
}
int main(int argc, char *argv[]) {
char *p;
ssize_t n;
size_t i, j;
bool chopped;
if (argc < 2) return 1;
if ((fd = open(argv[1], O_RDONLY)) == -1) return 2;
if (fstat(fd, &st) == -1) return 3;
n = st.st_size - MIN(st.st_size, sizeof(buf));
if ((n = pread(fd, buf, sizeof(buf), n)) == -1) return 4;
for (p = buf + n, i = 0; i < 10; ++i) {
p = firstnonnull(memrchr(buf, '\n', p - buf), buf);
}
chopped = false;
if (buf + n - p) ++p;
i = st.st_size - (buf + n - p);
atexit(OnExit);
HideCursor();
xsigaction(SIGINT, OnInt, 0, 0, 0);
xsigaction(SIGTERM, OnInt, 0, 0, 0);
while (!exited) {
if (fstat(fd, &st) == -1) return 5;
if (i > st.st_size) i = 0;
for (; i < st.st_size; i += n) {
if ((n = pread(fd, buf, sizeof(buf), i)) == -1) return 6;
j = n;
while (j && (buf[j - 1] == '\n' || buf[j - 1] == '\r')) --j;
if (j) {
if (chopped) {
WriteString("\r\n");
}
write(1, buf, j);
}
chopped = j != n;
}
dsleep(.01);
}
close(fd);
WriteString("\r\n");
return 0;
}

92
tool/viz/unicode.c Normal file
View File

@@ -0,0 +1,92 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2020 Justine Alexandra Roberts Tunney │
│ │
│ This program is free software; you can redistribute it and/or modify │
│ it under the terms of the GNU General Public License as published by │
│ the Free Software Foundation; version 2 of the License. │
│ │
│ This program is distributed in the hope that it will be useful, but │
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
│ General Public License for more details. │
│ │
│ You should have received a copy of the GNU General Public License │
│ along with this program; if not, write to the Free Software │
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
│ 02110-1301 USA │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/fmt/fmt.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/unicode/unicode.h"
#include "libc/x/x.h"
int a, b, w, i;
char name[512];
void DisplayUnicodeCharacter(void) {
int c, cw;
c = i;
cw = wcwidth(c);
if (cw < 1) {
c = '?';
cw = 1;
}
if (w) {
if (w + 1 + cw > 80) {
printf("\n");
w = 0;
} else {
fputc(' ', stdout);
w += 1 + cw;
}
}
if (!w) {
printf("%08x ", i);
w = 9 + cw;
}
fputwc(c, stdout);
}
void DisplayUnicodeBlock(void) {
if (a == 0x10000) {
printf("\n\n\n\n\n\n\n "
"ASTRAL PLANES\n\n\n\n\n");
}
if (a == 0x0590 /* hebrew */) return;
if (a == 0x0600 /* arabic */) return;
if (a == 0x08a0 /* arabic */) return;
if (a == 0x0750 /* arabic */) return;
if (a == 0x0700 /* syriac */) return;
if (a == 0x10800 /* cypriot */) return;
printf("\n\n%-60s%20s\n"
"──────────────────────────────────────────────"
"──────────────────────────────────\n",
name, gc(xasprintf("%04x .. %04x", a, b)));
w = 0;
for (i = a; i <= b; ++i) {
DisplayUnicodeCharacter();
}
}
int main(int argc, char *argv[]) {
FILE *f;
char *line;
size_t linesize;
printf("\n\n\n\n\n UNICODE PLANES\n\n\n\n");
f = fopen("libc/unicode/blocks.txt", "r");
line = NULL;
linesize = 0;
while (!feof(f)) {
if (getline(&line, &linesize, f) == -1) break;
if (sscanf(line, "%x..%x; %s", &a, &b, name) != 3) continue;
DisplayUnicodeBlock();
}
free(line);
fclose(f);
return 0;
}