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)

また、completeFilename を使用して上記の機能を実現するのと同じ方法で使用できるように、completeWord や completeQuotedWord などの関数も提供します。
(つまり、フォルダーの内容に基づくのではなく、単語のリスト (関数名など) に基づいてタブ補完を行います)

誰かがこれの実用的な例 - または実用的なコード - を提供できますか?

他のパッケージ (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 に答える