-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcompleat.hs
38 lines (34 loc) · 1.27 KB
/
compleat.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Completer (run)
import Numeric (readDec)
import System.Environment (getEnv, getArgs, lookupEnv)
import Tokenize (tokenize)
import Usage (Environment, commands, fromFile, lookupCommand)
-- Parse the usage file from the first argument. If there is a second argument,
-- it is a command-name; find the usage rules for that command and use them to
-- complete the input line. Otherwise, list all command names in the usage file.
main = do
args <- getArgs
env <- fromFile (head args)
if length args > 1
then completeLine env (args !! 1)
else listCommands env
completeLine :: Environment -> String -> IO ()
completeLine env command = do
line <- getInput
let completer = lookupCommand env command
suggestions <- run completer (tokenize line)
is_fish <- lookupEnv "COMPLEAT_IS_FISH"
case is_fish of
Nothing -> mapM_ putStrLn suggestions
_ -> do
-- The trailing space must be removed in fish
let suggestions' = map init suggestions
mapM_ putStrLn suggestions'
getInput :: IO String
getInput = do
line <- getEnv "COMP_LINE"
point <- getEnv "COMP_POINT"
let [(n,[])] = readDec point
return (take n line)
listCommands :: Environment -> IO ()
listCommands = mapM_ putStrLn . commands