Skip to content

Instantly share code, notes, and snippets.

@dgulino
Last active August 30, 2024 00:20
Show Gist options
  • Save dgulino/dfdcf3c2f1bcd57f28a769988daa2b90 to your computer and use it in GitHub Desktop.
Save dgulino/dfdcf3c2f1bcd57f28a769988daa2b90 to your computer and use it in GitHub Desktop.
ncurses numerical plotter
#! /usr/bin/env gforth
variable width
variable height
variable maxnum
100 maxnum !
variable num-digits
4 num-digits !
variable effective-width
variable partial-char
: set-screen-dimensions width ! height ! ;
form set-screen-dimensions
: blx $2588 xemit ;
: blocks 0 ?do blx loop ;
: partial-block
0 over = if else
1 over = if $258F xemit else
2 over = if $258E xemit else
3 over = if $258D xemit else
4 over = if $258C xemit else
5 over = if $258B xemit else
6 over = if $258A xemit else
7 over = if $2589 xemit
then then then then then then then then ;
: divide-and-remainder ( )
dup
8 mod
swap
8 / ;
: set-maxnum
dup
dup
maxnum @ > if
maxnum !
else
drop
endif ;
: scale-number
width @ num-digits @ - effective-width !
s>f 8 s>f f* effective-width @ s>f f* maxnum @ s>f f/ f>s
;
: is-num?
true = if
dup 3 u.r space
set-maxnum
scale-number
divide-and-remainder
blocks
drop
partial-block
cr
else
.s cr
endif ;
\ https://rosettacode.org/wiki/Read_a_file_line_by_line#Forth
4096 constant max-line
: read-lines ( fileid -- )
begin
pad max-line third read-line throw
while
pad
swap ( fileid c-addr u ) \ string excludes the newline
snumber?
is-num?
repeat
2drop ;
stdin read-lines
bye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment