From fcd2f60226d78783ddf15e4e4200047e2b9c5a1a Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 18 Jan 2024 11:11:42 +1100 Subject: [PATCH] X.P: Don't read/write to history file if size is 0 This contains a breaking change for readHistory, writeHistory, historyCompletion, and historyCompletionP to take an XPConfig, so they are aware of this choice. While the latter two are exported, it seems unlikely to affect many users. --- CHANGES.md | 12 ++++++++++++ XMonad/Actions/Search.hs | 4 ++-- XMonad/Actions/WorkspaceNames.hs | 2 +- XMonad/Prompt.hs | 24 +++++++++++++----------- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 0dbb2a33ad..569daea02a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,6 +13,13 @@ order to lift any existing `IO StatusBarConfig` values into `X StatusBarConfig` values. + * `XMonad.Prompt` + + - Added an additional `XPConfig` argument to `historyCompletion` and + `historyCompletionP`. Calls along the lines of `historyCompletionP + myFunc` should be changed to `historyCompletionP myConf myFunc`. + If not `myConf` is lying around, `def` can be used instead. + ### New Modules * `XMonad.Actions.Profiles`. @@ -37,6 +44,11 @@ - Added `HH:MM-HH:MM` and `HH:MM+HH` syntax to specify time spans. + * `XMonad.Prompt` + + - The history file is not extraneously read and written anymore if + the `historySize` is set to 0. + ### Other changes ## 0.18.0 (February 3, 2024) diff --git a/XMonad/Actions/Search.hs b/XMonad/Actions/Search.hs index 1fd6b841f4..5158a5d2bf 100644 --- a/XMonad/Actions/Search.hs +++ b/XMonad/Actions/Search.hs @@ -437,14 +437,14 @@ namedEngine name (SearchEngine _ site) = searchEngineF name site browser. -} promptSearchBrowser :: XPConfig -> Browser -> SearchEngine -> X () promptSearchBrowser config browser (SearchEngine name site) = do - hc <- historyCompletionP ("Search [" `isPrefixOf`) + hc <- historyCompletionP config ("Search [" `isPrefixOf`) mkXPrompt (Search name) config hc $ search browser site {- | Like 'promptSearchBrowser', but only suggest previous searches for the given 'SearchEngine' in the prompt. -} promptSearchBrowser' :: XPConfig -> Browser -> SearchEngine -> X () promptSearchBrowser' config browser (SearchEngine name site) = do - hc <- historyCompletionP (searchName `isPrefixOf`) + hc <- historyCompletionP config (searchName `isPrefixOf`) mkXPrompt (Search name) config hc $ search browser site where searchName = showXPrompt (Search name) diff --git a/XMonad/Actions/WorkspaceNames.hs b/XMonad/Actions/WorkspaceNames.hs index 4ba5fd08b8..ea417c9b70 100644 --- a/XMonad/Actions/WorkspaceNames.hs +++ b/XMonad/Actions/WorkspaceNames.hs @@ -138,7 +138,7 @@ setCurrentWorkspaceName name = do -- | Prompt for a new name for the current workspace and set it. renameWorkspace :: XPConfig -> X () renameWorkspace conf = do - completion <- historyCompletionP (prompt ==) + completion <- historyCompletionP conf (prompt ==) mkXPrompt (Wor prompt) conf completion setCurrentWorkspaceName where prompt = "Workspace name: " diff --git a/XMonad/Prompt.hs b/XMonad/Prompt.hs index 85e7f2b0ed..5c30fc90d8 100644 --- a/XMonad/Prompt.hs +++ b/XMonad/Prompt.hs @@ -562,7 +562,7 @@ mkXPromptImplementation historyKey conf om = do s <- gets $ screenRect . W.screenDetail . W.current . windowset cleanMask <- cleanKeyMask cachedir <- asks (cacheDir . directories) - hist <- io $ readHistory cachedir + hist <- io $ readHistory conf cachedir fs <- initXMF (font conf) let width = getWinWidth s (position conf) st' <- io $ @@ -582,7 +582,7 @@ mkXPromptImplementation historyKey conf om = do releaseXMF fs when (successful st') $ do let prune = take (historySize conf) - io $ writeHistory cachedir $ + io $ writeHistory conf cachedir $ M.insertWith (\xs ys -> prune . historyFilter conf $ xs ++ ys) historyKey @@ -1690,16 +1690,18 @@ emptyHistory = M.empty getHistoryFile :: FilePath -> FilePath getHistoryFile cachedir = cachedir ++ "/prompt-history" -readHistory :: FilePath -> IO History -readHistory cachedir = readHist `E.catch` \(SomeException _) -> return emptyHistory +readHistory :: XPConfig -> FilePath -> IO History +readHistory (XPC { historySize = 0 }) _ = return emptyHistory +readHistory _ cachedir = readHist `E.catch` \(SomeException _) -> return emptyHistory where readHist = do let path = getHistoryFile cachedir xs <- withFile path ReadMode hGetLine readIO xs -writeHistory :: FilePath -> History -> IO () -writeHistory cachedir hist = do +writeHistory :: XPConfig -> FilePath -> History -> IO () +writeHistory (XPC { historySize = 0 }) _ _ = return () +writeHistory _ cachedir hist = do let path = getHistoryFile cachedir filtered = M.filter (not . null) hist writeFile path (show filtered) `E.catch` \(SomeException e) -> @@ -1793,17 +1795,17 @@ breakAtSpace s -- | 'historyCompletion' provides a canned completion function much like -- 'getShellCompl'; you pass it to mkXPrompt, and it will make completions work -- from the query history stored in the XMonad cache directory. -historyCompletion :: X ComplFunction -historyCompletion = historyCompletionP (const True) +historyCompletion :: XPConfig -> X ComplFunction +historyCompletion conf = historyCompletionP conf (const True) -- | Like 'historyCompletion' but only uses history data from Prompts whose -- name satisfies the given predicate. -historyCompletionP :: (String -> Bool) -> X ComplFunction -historyCompletionP p = do +historyCompletionP :: XPConfig -> (String -> Bool) -> X ComplFunction +historyCompletionP conf p = do cd <- asks (cacheDir . directories) pure $ \x -> let toComplList = deleteConsecutive . filter (isInfixOf x) . M.foldr (++) [] - in toComplList . M.filterWithKey (const . p) <$> readHistory cd + in toComplList . M.filterWithKey (const . p) <$> readHistory conf cd -- | Sort a list and remove duplicates. Like 'deleteAllDuplicates', but trades off -- laziness and stability for efficiency.