summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorS. Gilles <sgilles@math.umd.edu>2017-10-26 21:06:44 -0400
committerOri Bernstein <ori@markovcorp.com>2017-10-27 11:52:01 -0700
commitb88606d8a6632c98b45bf9f68ee736459ab86e0c (patch)
tree24f23ea64410535f6e4f36078e01b0520a5c528b
parent455101bdeb02a67e9821258ed21ccac7ac1716c3 (diff)
downloadlibtermdraw-b88606d8a6632c98b45bf9f68ee736459ab86e0c.tar.gz
Allow multi-arm conditionals
In addition to nested %?...%;, terminfo formats also allow multi-arm conditionals, in the style of if..elif..elif..else..;; Handle those.
-rw-r--r--test/tifmt.myr15
-rw-r--r--tifmt.myr40
2 files changed, 39 insertions, 16 deletions
diff --git a/test/tifmt.myr b/test/tifmt.myr
index 0fd1dcf..026e4ad 100644
--- a/test/tifmt.myr
+++ b/test/tifmt.myr
@@ -83,6 +83,21 @@ const main = {
"%?%p1%t%?%p2%ttruetrue%etruefalse%;%efalsefalse%;", \
params, "truefalse")
}],
+ [.name="cond-multi-arm-1", .fn={ctx
+ checkfmt(ctx, \
+ "%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m", \
+ [`termdraw.Int 5][:], "35m")
+ }],
+ [.name="cond-multi-arm-2", .fn={ctx
+ checkfmt(ctx, \
+ "%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m", \
+ [`termdraw.Int 13][:], "95m")
+ }],
+ [.name="cond-multi-arm-3", .fn={ctx
+ checkfmt(ctx, \
+ "%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m", \
+ [`termdraw.Int 221][:], "38;5;221m")
+ }],
/* oddballs */
][:])
}
diff --git a/tifmt.myr b/tifmt.myr
index 4ae92b8..c16b2e3 100644
--- a/tifmt.myr
+++ b/tifmt.myr
@@ -84,14 +84,14 @@ const eval = {f, sb, str
}
const evalcond ={f, sb, str, pi
- var cond, iftrue, iffalse
- var start, depth, i
+ var condition
+ var start, depth, i, done, seen_e
- cond = ""
- iftrue = ""
- iffalse = ""
+ condition = ""
start = pi#
depth = 1
+ done = false
+ seen_e = false
for i = pi#; i < str.len - 1; i++
if str[i] != ('%' : byte)
continue
@@ -100,24 +100,32 @@ const evalcond ={f, sb, str, pi
depth++
elif str[i + 1] == (';' : byte)
depth--
- elif depth == 1 && str[i + 1] == ('t' : byte)
- cond = str[start:i]
+ elif !done && depth == 1 && str[i + 1] == ('t' : byte)
+ condition = str[start:i]
start = i + 2
- elif depth == 1 && str[i+ 1] == ('e' : byte)
- iftrue = str[start:i]
+ elif !done && depth == 1 && str[i + 1] == ('e' : byte)
+ seen_e = true
+
+ eval(f, sb, condition)
+ if popi(f) != 0
+ eval(f, sb, str[start:i])
+ done = true
+ ;;
+
start = i + 2
;;
if depth == 0
- iffalse = str[start:i]
+ if !done && seen_e
+ eval(f, sb, str[start:i])
+ elif !done
+ eval(f, sb, condition)
+ if popi(f) != 0
+ eval(f, sb, str[start:i])
+ ;;
+ ;;
break
;;
;;
- eval(f, sb, cond)
- if popi(f) != 0
- eval(f, sb, iftrue)
- else
- eval(f, sb, iffalse)
- ;;
pi# = i + 2
}