-----------------------------------------------------------------------------
-- |
-- 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 Control.Exception (SomeException(..), handle)
import Xmobar.Run.Exec
import Xmobar.X11.Actions (stripActions)
import Xmobar.System.Utils (hGetLineSafe)

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 = do
    String
s <- (SomeException -> IO String) -> IO String -> IO String
forall e a. Exception e => (e -> IO a) -> IO a -> IO a
handle (\(SomeException e
e) -> do Handle -> e -> IO ()
forall a. Show a => Handle -> a -> IO ()
hPrint Handle
stderr e
e; String -> IO String
forall (m :: * -> *) a. Monad m => a -> m a
return String
"")
                (Handle -> IO String
hGetLineSafe Handle
stdin)
    String -> IO ()
cb (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ StdinReader -> ShowS
escape StdinReader
stdinReader String
s
    Bool
eof <- IO Bool
isEOF
    if Bool
eof
      then ExitCode -> IO ()
exitImmediately ExitCode
ExitSuccess
      else StdinReader -> (String -> IO ()) -> IO ()
forall e. Exec e => e -> (String -> IO ()) -> IO ()
start StdinReader
stdinReader String -> IO ()
cb

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