Skip to content

Commit

Permalink
Merge pull request #864 from dcousens/no-xp-io
Browse files Browse the repository at this point in the history
X.P: Add escape hatch for preventing X.P IO
  • Loading branch information
slotThe authored Mar 31, 2024
2 parents 5192685 + fcd2f60 commit 6e43da8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
12 changes: 12 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions XMonad/Actions/Search.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion XMonad/Actions/WorkspaceNames.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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: "
Expand Down
24 changes: 13 additions & 11 deletions XMonad/Prompt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 $
Expand All @@ -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
Expand Down Expand Up @@ -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) ->
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6e43da8

Please sign in to comment.