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.
This commit is contained in:
adamrk
2017-05-29 00:26:46 -04:00
parent a7a06a7684
commit 5b9f0abeb5
2 changed files with 11 additions and 2 deletions

View File

@@ -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"

View File

@@ -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)