Merge pull request #225 from maciasello/master

Add appendFilePrompt' to allow transforming text before appending
This commit is contained in:
Brent Yorgey 2018-02-12 09:59:40 -06:00 committed by GitHub
commit a96d1d0bb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 4 deletions

View File

@ -196,6 +196,11 @@
acts as a generic function to pass the selected Unicode character to any acts as a generic function to pass the selected Unicode character to any
program. program.
* `XMonad.Prompt.AppendFile`
- New function `appendFilePrompt'` which allows for transformation of the
string passed by a user before writing to a file.
## 0.13 (February 10, 2017) ## 0.13 (February 10, 2017)
### Breaking Changes ### Breaking Changes

View File

@ -23,6 +23,7 @@ module XMonad.Prompt.AppendFile (
-- $usage -- $usage
appendFilePrompt, appendFilePrompt,
appendFilePrompt',
AppendFile, AppendFile,
) where ) where
@ -55,6 +56,17 @@ import Control.Exception.Extensible (bracket)
-- --
-- (Put the spawn on the line after the prompt to append the time instead.) -- (Put the spawn on the line after the prompt to append the time instead.)
-- --
-- 'appendFilePrompt'' can be used to transform the string input in the prompt
-- before saving into the file. Previous example with date can be rewritten as:
--
-- > , ((modm .|. controlMask, xK_n), do
-- > date <- io $ liftM (formatTime defaultTimeLocale "[%Y-%m-%d %H:%M] ") getZonedTime
-- > appendFilePrompt' def (date ++) $ "/home/me/NOTES"
-- > )
--
-- A benefit is that if the prompt is cancelled the date is not output to
-- the file too.
--
-- For detailed instructions on editing your key bindings, see -- For detailed instructions on editing your key bindings, see
-- "XMonad.Doc.Extending#Editing_key_bindings". -- "XMonad.Doc.Extending#Editing_key_bindings".
@ -66,11 +78,17 @@ instance XPrompt AppendFile where
-- | Given an XPrompt configuration and a file path, prompt the user -- | Given an XPrompt configuration and a file path, prompt the user
-- for a line of text, and append it to the given file. -- for a line of text, and append it to the given file.
appendFilePrompt :: XPConfig -> FilePath -> X () appendFilePrompt :: XPConfig -> FilePath -> X ()
appendFilePrompt c fn = mkXPrompt (AppendFile fn) appendFilePrompt c fn = appendFilePrompt' c id fn
-- | Given an XPrompt configuration, string transformation function
-- and a file path, prompt the user for a line of text, transform it
-- and append the result to the given file.
appendFilePrompt' :: XPConfig -> (String -> String) -> FilePath -> X ()
appendFilePrompt' c trans fn = mkXPrompt (AppendFile fn)
c c
(const (return [])) (const (return []))
(doAppend fn) (doAppend trans fn)
-- | Append a string to a file. -- | Append a string to a file.
doAppend :: FilePath -> String -> X () doAppend :: (String -> String) -> FilePath -> String -> X ()
doAppend fn = io . bracket (openFile fn AppendMode) hClose . flip hPutStrLn doAppend trans fn = io . bracket (openFile fn AppendMode) hClose . flip hPutStrLn . trans