24

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)的功能建议也很有帮助。

4

1 回答 1

23

这是你追求的那种东西吗?

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代码段中的定义,它应该可以工作。

于 2011-05-29T04:15:17.780 回答