-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_tty.ks
44 lines (32 loc) · 1016 Bytes
/
lib_tty.ks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// lib_tty.ks
// Copyright © 2020 Steve Meacham and contributors
// Lic. GPL-3.0-or-later
PARAMETER nsr.
LOCAL FUNCTION wrap {
PARAMETER text, limit.
IF text:LENGTH <= limit {PRINT text. RETURN.}
LOCAL edge IS text:SUBSTRING(0, limit):LASTINDEXOF(" ").
IF edge <= 0 {PRINT text. RETURN.}
PRINT text:SUBSTRING(0, edge).
wrap(text:REMOVE(0, edge+1):TRIMSTART, limit).
}
LOCAL FUNCTION println {
PARAMETER s IS "", a IS ""+s.
IF a:LENGTH <=0 {PRINT " ". RETURN.}
wrap(a, TERMINAL:WIDTH).
}
LOCAL FUNCTION print_hr {
PARAMETER s IS "-",
t IS ""+s, n IS TERMINAL:WIDTH/t:LENGTH, l IS "".
UNTIL n <= 0 {SET l TO l+t. SET n TO n-1.}
println(l).
}
LOCAL FUNCTION print_file {
PARAMETER p,
e IS EXISTS(p),
f IS CHOOSE OPEN(p) IF e ELSE 0,
c IS CHOOSE f:READALL IF e AND f:ISFILE ELSE 0,
i IS CHOOSE c:ITERATOR IF e AND c:TYPE="ASCII" ELSE List():ITERATOR.
UNTIL NOT i:NEXT println(i:VALUE).
}
nsr:put("tty", Lexicon("println", println@, "print_hr", print_hr@, "print_file", print_file@)).