[PATCH 3/4] Allow multi-arm conditionals
[Thread Prev] | [Thread Next]
- Subject: [PATCH 3/4] Allow multi-arm conditionals
- From: "S. Gilles" <sgilles@xxxxxxxxxxxx>
- Reply-to: myrddin-dev@xxxxxxxxxxxxxx
- Date: Thu, 26 Oct 2017 21:06:44 -0400
- To: "myrddin-dev" <myrddin-dev@xxxxxxxxxxxxxx>
- Cc: "S. Gilles" <sgilles@xxxxxxxxxxxx>
In addition to nested %?...%;, terminfo formats also allow multi-arm
conditionals, in the style of if..elif..elif..else..;; Handle those.
---
test/tifmt.myr | 15 +++++++++++++++
tifmt.myr | 40 ++++++++++++++++++++++++----------------
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
}
--
2.14.3
| [PATCH 0/4] Fixes to allow using terminfo for colors on st | "S. Gilles" <sgilles@xxxxxxxxxxxx> |
- Prev by Date: [PATCH 2/4] Reorder binary operations
- Next by Date: [PATCH 4/4] Use Set_a_foreground and Set_a_background for colors
- Previous by thread: [PATCH 2/4] Reorder binary operations
- Next by thread: [PATCH 4/4] Use Set_a_foreground and Set_a_background for colors
- Index(es):