Repository : ssh://darcs.haskell.org//srv/darcs/packages/haskeline On branch : master
http://hackage.haskell.org/trac/ghc/changeset/03cc9beffeedc8234bfe812550771d564fa0d9ca >--------------------------------------------------------------- commit 03cc9beffeedc8234bfe812550771d564fa0d9ca Author: Judah Jacobson <[email protected]> Date: Mon Feb 21 23:26:28 2011 +0000 Add new function getInputLineWithInitial. This function lets the user specify initial "default" text for the prompt. Adapted from a patch by Robert Massaioli. >--------------------------------------------------------------- System/Console/Haskeline.hs | 35 ++++++++++++++++++++++++++++++----- examples/Test.hs | 2 ++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/System/Console/Haskeline.hs b/System/Console/Haskeline.hs index fb3dc68..c4d6b91 100644 --- a/System/Console/Haskeline.hs +++ b/System/Console/Haskeline.hs @@ -43,6 +43,7 @@ module System.Console.Haskeline( -- ** Reading user input -- $inputfncs getInputLine, + getInputLineWithInitial, getInputChar, getPassword, -- ** Outputting text @@ -135,15 +136,39 @@ spaces), it will be automatically added to the history. -} getInputLine :: MonadException m => String -- ^ The input prompt -> InputT m (Maybe String) -getInputLine = promptedInput getInputCmdLine $ unMaybeT . getLocaleLine +getInputLine = promptedInput (getInputCmdLine emptyIM) $ unMaybeT . getLocaleLine + +{- | Reads one line of input and fills the insertion space with initial text. When using +terminal-style interaction, this function provides a rich line-editing user interface with the +added ability to give the user default values. + +This function behaves in the exact same manner as 'getInputLine', except that +it pre-populates the input area. The text that resides in the input area is given as a 2-tuple +with two 'String's. The string on the left of the tuple (obtained by calling 'fst') is +what will appear to the left of the cursor and the string on the right (obtained by +calling 'snd') is what will appear to the right of the cursor. + +Some examples of calling of this function are: + +> getInputLineWithInitial "prompt> " ("left", "") -- The cursor starts at the end of the line. +> getInputLineWithInitial "prompt> " ("left ", "right") -- The cursor starts before the second word. + -} +getInputLineWithInitial :: MonadException m + => String -- ^ The input prompt + -> (String, String) -- ^ The initial value left and right of the cursor + -> InputT m (Maybe String) +getInputLineWithInitial prompt (left,right) = promptedInput (getInputCmdLine initialIM) + (unMaybeT . getLocaleLine) prompt + where + initialIM = insertString left $ moveToStart $ insertString right $ emptyIM -getInputCmdLine :: MonadException m => TermOps -> String -> InputT m (Maybe String) -getInputCmdLine tops prefix = do +getInputCmdLine :: MonadException m => InsertMode -> TermOps -> String -> InputT m (Maybe String) +getInputCmdLine initialIM tops prefix = do emode <- asks editMode result <- runInputCmdT tops $ case emode of - Emacs -> runCommandLoop tops prefix emacsCommands emptyIM + Emacs -> runCommandLoop tops prefix emacsCommands initialIM Vi -> evalStateT' emptyViState $ - runCommandLoop tops prefix viKeyCommands emptyIM + runCommandLoop tops prefix viKeyCommands initialIM maybeAddHistory result return result diff --git a/examples/Test.hs b/examples/Test.hs index cc6deb3..817efcd 100644 --- a/examples/Test.hs +++ b/examples/Test.hs @@ -11,6 +11,7 @@ Usage: ./Test chars (character input) ./Test password (no masking characters) ./Test password \* +./Test initial (use initial text in the prompt) --} mySettings :: Settings IO @@ -23,6 +24,7 @@ main = do ["chars"] -> fmap (fmap (\c -> [c])) . getInputChar ["password"] -> getPassword Nothing ["password", [c]] -> getPassword (Just c) + ["initial"] -> flip getInputLineWithInitial ("left ", "right") _ -> getInputLine runInputT mySettings $ withInterrupt $ loop inputFunc 0 where _______________________________________________ Cvs-libraries mailing list [email protected] http://www.haskell.org/mailman/listinfo/cvs-libraries
