From 5b9f0abeb5f8780f75014e1589c077a3c61e416c Mon Sep 17 00:00:00 2001 From: adamrk Date: Mon, 29 May 2017 00:26:46 -0400 Subject: [PATCH] add tab completion by displaying functions matching prefix Added a function to State which returns a list of all defined values/functions matching a prefix. Modify the tab printout to display 5 of these results. --- src/BC/Prompt.hs | 8 ++++++-- src/BC/State.hs | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/BC/Prompt.hs b/src/BC/Prompt.hs index 54023d9..5b4b1dc 100644 --- a/src/BC/Prompt.hs +++ b/src/BC/Prompt.hs @@ -80,10 +80,14 @@ printStatus state str = printCompletions :: State -> String -> IO () printCompletions state str = - let completions = "" -- TODO: get possible completions + let tokens = words str + completions = if tokens == [] + then [] + else getCompletions (last tokens) state + compString = intercalate " " $ take 5 completions toPrint = "\n\x1b[32m" -- color to green ++ replicate (length promptStr) ' ' - ++ completions + ++ compString ++ "\x1b[0m\r\x1b[A" -- color to white, move to prev line in do putStr toPrint putStr $ concat $ replicate (length promptStr + length str) "\x1b[C" diff --git a/src/BC/State.hs b/src/BC/State.hs index a3e6764..1b31680 100644 --- a/src/BC/State.hs +++ b/src/BC/State.hs @@ -9,3 +9,8 @@ type State = Map String Value newState :: State newState = fromList primitives + +getCompletions :: String -> State -> [String] +getCompletions s = foldWithKey addKey [] + where addKey k _ ks = if isPrefix s k then k:ks else ks + isPrefix s1 s2 = length s2 >= length s1 && (all id $ zipWith (==) s1 s2) \ No newline at end of file