Skip to content

Support flushing command history #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion System/Console/Haskeline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ module System.Console.Haskeline(
getHistory,
putHistory,
modifyHistory,
flushHistory,
-- * Ctrl-C handling
withInterrupt,
Interrupt(..),
Expand All @@ -92,6 +93,7 @@ import System.Console.Haskeline.RunCommand

import System.IO
import Data.Char (isSpace, isPrint)
import Control.Monad (when)


-- | A useful default. In particular:
Expand Down Expand Up @@ -182,13 +184,16 @@ maybeAddHistory :: forall m . MonadIO m => Maybe String -> InputT m ()
maybeAddHistory result = do
settings :: Settings m <- InputT ask
histDupes <- InputT $ asks historyDuplicates
doFlush <- InputT $ asks flushEveryCommand
case result of
Just line | autoAddHistory settings && not (all isSpace line)
-> let adder = case histDupes of
AlwaysAdd -> addHistory
IgnoreConsecutive -> addHistoryUnlessConsecutiveDupe
IgnoreAll -> addHistoryRemovingAllDupes
in modifyHistory (adder line)
in do
modifyHistory (adder line)
when doFlush flushHistory
_ -> return ()

----------
Expand Down
16 changes: 13 additions & 3 deletions System/Console/Haskeline/InputT.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ data Settings m = Settings {complete :: CompletionFunc m, -- ^ Custom tab comple
historyFile :: Maybe FilePath, -- ^ Where to read/write the history at the
-- start and end of each
-- line input session.
autoAddHistory :: Bool -- ^ If 'True', each nonblank line returned by
autoAddHistory :: Bool -- ^ If 'True', each nonblank line returned by
-- @getInputLine@ will be automatically added to the history.

}
}

-- | Because 'complete' is the only field of 'Settings' depending on @m@,
-- the expression @defaultSettings {completionFunc = f}@ leads to a type error
Expand Down Expand Up @@ -64,6 +63,17 @@ getHistory = InputT get
putHistory :: MonadIO m => History -> InputT m ()
putHistory = InputT . put

-- | Writes command history to file if 'historyFile' is not 'Nothing'
flushHistory :: forall m . MonadIO m => InputT m ()
flushHistory = do
settings :: Settings m <- InputT ask
getHistory >>= maybeFlushHistory (historyFile settings)

-- | Flushes history if given filepath is not 'Nothing'
maybeFlushHistory :: MonadIO m => Maybe FilePath -> History -> InputT m ()
maybeFlushHistory Nothing _ = return ()
maybeFlushHistory (Just f) hist = liftIO $ writeHistory f hist

-- | Change the current line input history.
modifyHistory :: MonadIO m => (History -> History) -> InputT m ()
modifyHistory = InputT . modify
Expand Down
10 changes: 7 additions & 3 deletions System/Console/Haskeline/Prefs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ data Prefs = Prefs { bellStyle :: !BellStyle,
-- presses @TAB@ again.
customBindings :: Map.Map Key [Key],
-- (termName, keysequence, key)
customKeySequences :: [(Maybe String, String,Key)]
customKeySequences :: [(Maybe String, String,Key)],
flushEveryCommand :: Bool
-- ^ If 'True' and @historyFile@ not 'Nothing'
-- flushes command history after every command
}
deriving Show

Expand Down Expand Up @@ -76,8 +79,9 @@ defaultPrefs = Prefs {bellStyle = AudibleBell,
listCompletionsImmediately = True,
historyDuplicates = AlwaysAdd,
customBindings = Map.empty,
customKeySequences = []
}
customKeySequences = [],
flushEveryCommand = False
}

mkSettor :: Read a => (a -> Prefs -> Prefs) -> String -> Prefs -> Prefs
mkSettor f str = maybe id f (readMaybe str)
Expand Down