【发布时间】:2021-03-29 18:29:58
【问题描述】:
我想写一个文本界面,它提供了一些默认的命令。该程序支持这些命令的制表符补全。
该程序还记录用户输入并将其存储在StateData 中。现在我希望这个程序支持这些用户输入的选项卡完成。例如:
*Main > main
> read a<tab> -- press tab and no suggestions (read is a default command)
> read abcde
...
> read a<tab> -- press tab
abcde -- suggestions
是否可以在不使用 IORef 之类的不安全机制的情况下做到这一点?有没有办法将更新后的st 从loop(repl)传递到replSettings startState(repl)?
我是 Haskeline 的新手,感谢您抽出宝贵时间。
repl :: StateData -> IO()
repl startState = runInputT (replSettings startState) $ loop startState
where
loop :: StateData -> InputT IO ()
loop st = do
inputL <- getInputLine "> "
case inputL of
Nothing -> return ()
Just "quit" -> outputStrLn "--Exited--" >> return ()
Just ipt -> do (opt, st') <- process ipt `runStateT` st
...
loop st'
replSettings :: StateData -> Settings IO
replSettings st =
Settings
{ complete = replCompletion st,
historyFile = Just "history.txt",
autoAddHistory = True
}
replCompletion :: StateData -> CompletionFunc IO
replCompletion st = completeWordWithPrev Nothing [' '] st (\x y -> return $ completionGenerator x y)
completionGenerator :: String -> String -> StateData -> [Completion]
completionGenerator "" c st =
commandSuggestion c (updatesSuggestions st) -- I wish to update it at run time
completionGenerator p c st = ...
【问题讨论】: