命令行-Haskell(haskeline)字完成



Haskeline使获得文件名选项卡完成功能变得非常容易:

module Main where
import System.Console.Haskeline
import System.Environment
mySettings :: Settings IO
mySettings = defaultSettings {historyFile = Just "myhist"}
main :: IO ()
main = do
        args <- getArgs
        let inputFunc = getInputLine
        runInputT mySettings $ withInterrupt $ loop inputFunc 0
    where
        loop inputFunc n = do
            minput <-  handleInterrupt (return (Just "Caught interrupted"))
                        $ inputFunc (show n ++ ":")
            case minput of
                Nothing -> return ()
                Just s -> do
                            outputStrLn ("line " ++ show n ++ ":" ++ s)
                            loop inputFunc (n+1)

它还提供了completeWord和completeQuotedWord等函数,它们的使用方式应该与completeFilename用于实现上述功能的方式相同
(换句话说,根据单词列表(比如功能名称)完成选项卡,而不是根据文件夹的内容)

有人能提供一个工作示例或工作代码吗?

对其他软件包(如HCL)功能的建议也很有用。

这就是你想要的东西吗?

import Data.List
wordList = [ "Apple", "Pear", "Peach", "Grape", "Grapefruit", "Slime Mold"]
searchFunc :: String -> [Completion]
searchFunc str = map simpleCompletion $ filter (str `isPrefixOf`) wordList
mySettings :: Settings IO
mySettings = Settings { historyFile = Just "myhist"
                      , complete = completeWord Nothing " t" $ return . searchFunc
                      , autoAddHistory = True
                      }

用它替换代码段中mySettings的定义,它应该可以工作。

相关内容

  • 没有找到相关文章

最新更新