Haskell (haskeline) word completion
Asked Answered
T

1

24

Haskeline makes it very easy to get filename tab-completion functionality:

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)

It also provides functions like completeWord and completeQuotedWord, which should be able to be used in the same way that completeFilename is used to make the above functionality.
(In other words, have tab-completion based on a list of words (say, function names), instead of based on the contents of a folder)

Can anyone provide a working example - or working code - of this?

Recommendations for functions from other packages (like HCL) are helpful also.

Trajectory answered 27/5, 2011 at 2:23 Comment(3)
I've never used these functions, but if you're ever looking for example code with a certain function or module, you can always try Google Code Search. It's not always helpful, but it'll often give you some great examples in a variety of contexts.Robertoroberts
I didn't find any references for the functions, but thanks for the suggestion.Trajectory
Offering 62% of my reputation for an answer to this question :)Trajectory
C
24

Is this the sort of thing you're after?

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
                      }

Replace the definition of mySettings in your snippet with that and it should work.

Climber answered 29/5, 2011 at 4:15 Comment(4)
@amindfv: You're welcome! Not that I really need more rep anyway, but I don't usually see bounties on Haskell questions so I guess it served its purpose of getting attention? I did test it quickly to verify that it worked at all, but if you have any problems I'll be happy to help (since that didn't really feel like +50 worth of effort, hahaha).Climber
It seems that when I use a history file that is not in the current directory(such as "~/.myhist"), haskeline history no longer works. Any thoughts on that?Nelda
@Carson: Have you tried giving an absolute path that doesn't rely on expanding ~? Other than that, I have no idea.Climber
Yes, I have. Doesn't work as well. I'm guessing it's just a simple bug on my side.Nelda

© 2022 - 2024 — McMap. All rights reserved.