module XMonad.Prompt.AppendFile (
appendFilePrompt,
appendFilePrompt',
AppendFile,
) where
import XMonad.Core
import XMonad.Prompt
import System.IO
import Control.Exception.Extensible (bracket)
data AppendFile = AppendFile FilePath
instance XPrompt AppendFile where
showXPrompt :: AppendFile -> String
showXPrompt (AppendFile String
fn) = String
"Add to " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
fn String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": "
appendFilePrompt :: XPConfig -> FilePath -> X ()
appendFilePrompt :: XPConfig -> String -> X ()
appendFilePrompt XPConfig
c String
fn = XPConfig -> (String -> String) -> String -> X ()
appendFilePrompt' XPConfig
c String -> String
forall a. a -> a
id String
fn
appendFilePrompt' :: XPConfig -> (String -> String) -> FilePath -> X ()
appendFilePrompt' :: XPConfig -> (String -> String) -> String -> X ()
appendFilePrompt' XPConfig
c String -> String
trans String
fn = AppendFile -> XPConfig -> ComplFunction -> (String -> X ()) -> X ()
forall p.
XPrompt p =>
p -> XPConfig -> ComplFunction -> (String -> X ()) -> X ()
mkXPrompt (String -> AppendFile
AppendFile String
fn)
XPConfig
c
(IO [String] -> ComplFunction
forall a b. a -> b -> a
const ([String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return []))
((String -> String) -> String -> String -> X ()
doAppend String -> String
trans String
fn)
doAppend :: (String -> String) -> FilePath -> String -> X ()
doAppend :: (String -> String) -> String -> String -> X ()
doAppend String -> String
trans String
fn = IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> (String -> IO ()) -> String -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO Handle -> (Handle -> IO ()) -> (Handle -> IO ()) -> IO ()
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (String -> IOMode -> IO Handle
openFile String
fn IOMode
AppendMode) Handle -> IO ()
hClose ((Handle -> IO ()) -> IO ())
-> (String -> Handle -> IO ()) -> String -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Handle -> String -> IO ()) -> String -> Handle -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Handle -> String -> IO ()
hPutStrLn (String -> Handle -> IO ())
-> (String -> String) -> String -> Handle -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
trans