{-# LANGUAGE ViewPatterns #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Plugins.StdinReader
-- Copyright   :  (c) Andrea Rossato
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  Jose A. Ortega Ruiz <jao@gnu.org>
-- Stability   :  unstable
-- Portability :  unportable
--
-- A plugin for reading from `stdin`.
--
-- Exports:
-- - `StdinReader` to safely display stdin content (striping actions).
-- - `UnsafeStdinReader` to display stdin content as-is.
--
-----------------------------------------------------------------------------

module Xmobar.Plugins.StdinReader (StdinReader(..)) where

import Prelude
import System.Posix.Process
import System.Exit
import System.IO
import System.IO.Error (isEOFError)
import Xmobar.Run.Exec
import Xmobar.Run.Actions (stripActions)
import Control.Exception
import Control.Monad (forever)

data StdinReader = StdinReader | UnsafeStdinReader
  deriving (ReadPrec [StdinReader]
ReadPrec StdinReader
Int -> ReadS StdinReader
ReadS [StdinReader]
(Int -> ReadS StdinReader)
-> ReadS [StdinReader]
-> ReadPrec StdinReader
-> ReadPrec [StdinReader]
-> Read StdinReader
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [StdinReader]
$creadListPrec :: ReadPrec [StdinReader]
readPrec :: ReadPrec StdinReader
$creadPrec :: ReadPrec StdinReader
readList :: ReadS [StdinReader]
$creadList :: ReadS [StdinReader]
readsPrec :: Int -> ReadS StdinReader
$creadsPrec :: Int -> ReadS StdinReader
Read, Int -> StdinReader -> ShowS
[StdinReader] -> ShowS
StdinReader -> String
(Int -> StdinReader -> ShowS)
-> (StdinReader -> String)
-> ([StdinReader] -> ShowS)
-> Show StdinReader
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StdinReader] -> ShowS
$cshowList :: [StdinReader] -> ShowS
show :: StdinReader -> String
$cshow :: StdinReader -> String
showsPrec :: Int -> StdinReader -> ShowS
$cshowsPrec :: Int -> StdinReader -> ShowS
Show)

instance Exec StdinReader where
  start :: StdinReader -> (String -> IO ()) -> IO ()
start StdinReader
stdinReader String -> IO ()
cb = IO () -> IO ()
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ (String -> IO ()
cb (String -> IO ()) -> ShowS -> String -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StdinReader -> ShowS
escape StdinReader
stdinReader (String -> IO ()) -> IO String -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO String
getLine) IO () -> (SomeException -> IO ()) -> IO ()
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` SomeException -> IO ()
handler
    where
      -- rethrow async exceptions like ThreadKilled, etc.
      handler :: SomeException -> IO ()
handler (SomeException -> Maybe SomeAsyncException
forall e. Exception e => SomeException -> Maybe e
fromException -> Just SomeAsyncException
e) = SomeAsyncException -> IO ()
forall e a. Exception e => e -> IO a
throwIO (SomeAsyncException
e :: SomeAsyncException)
      -- XMonad.Hooks.DynamicLog.statusBar starts new xmobar on every xmonad
      -- reload and the old xmobar is only signalled to exit via the pipe
      -- being closed, so we must unconditionally terminate on EOF, otherwise
      -- there'd be a pileup of xmobars
      handler (SomeException -> Maybe IOError
forall e. Exception e => SomeException -> Maybe e
fromException -> Just IOError
e) | IOError -> Bool
isEOFError IOError
e = ExitCode -> IO ()
exitImmediately ExitCode
ExitSuccess
      -- any other exception, like "invalid argument (invalid byte sequence)",
      -- is logged to both stderr and the bar itself
      handler SomeException
e = do
        let errorMessage :: String
errorMessage = String
"xmobar: Received exception " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> SomeException -> String
forall a. Show a => a -> String
show SomeException
e
        Handle -> String -> IO ()
hPutStrLn Handle
stderr String
errorMessage
        String -> IO ()
cb (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ ShowS
stripActions String
errorMessage

escape :: StdinReader -> String -> String
escape :: StdinReader -> ShowS
escape StdinReader
StdinReader = ShowS
stripActions
escape StdinReader
UnsafeStdinReader = ShowS
forall a. a -> a
id