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