From a2016241df9cb4d35c03db5b24f08ec0cc8ca7e8 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 10 Apr 2021 12:53:22 +0100 Subject: [PATCH] support Home, End and Del keys (#87) * add Home and End keys * support Delete key * simplify updateTermState --- apps/dog-food/ChatTerminal/Core.hs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/apps/dog-food/ChatTerminal/Core.hs b/apps/dog-food/ChatTerminal/Core.hs index 24856e498..09d919795 100644 --- a/apps/dog-food/ChatTerminal/Core.hs +++ b/apps/dog-food/ChatTerminal/Core.hs @@ -51,6 +51,9 @@ updateTermState ac tw (key, ms) ts@TerminalState {inputString = s, inputPosition | otherwise -> ts TabKey -> insertCharsWithContact " " BackspaceKey -> backDeleteChar + DeleteKey -> deleteChar + HomeKey -> setPosition 0 + EndKey -> setPosition $ length s ArrowKey d -> case d of Leftwards | ms == mempty -> setPosition $ max 0 (p - 1) @@ -85,10 +88,12 @@ updateTermState ac tw (key, ms) ts@TerminalState {inputString = s, inputPosition Nothing -> "" backDeleteChar | p == 0 || null s = ts - | p >= length s = ts' backDeleteLast - | otherwise = ts' backDelete - backDeleteLast = if null s then (s, 0) else let s' = init s in (s', length s') - backDelete = let (b, a) = splitAt p s in (init b <> a, p - 1) + | p >= length s = ts' (init s, length s - 1) + | otherwise = let (b, a) = splitAt p s in ts' (init b <> a, p - 1) + deleteChar + | p >= length s || null s = ts + | p == 0 = ts' (tail s, 0) + | otherwise = let (b, a) = splitAt p s in ts' (b <> tail a, p) setPosition p' = ts' (s, p') prevWordPos | p == 0 || null s = p