-----------------------------------------------------------------------------
-- |
-- 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 Xmobar.Run.Exec
import Xmobar.X11.Actions (stripActions)
import Xmobar.System.Utils (onSomeException)
import Control.Monad (when)

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
    -- The EOF check is necessary for certain systems
    -- More details here https://github.com/jaor/xmobar/issues/442
    Bool
eof <- IO Bool
isEOF
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
eof (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
      do Handle -> String -> IO ()
forall a. Show a => Handle -> a -> IO ()
hPrint Handle
stderr String
"xmobar: eof at an early stage"
         ExitCode -> IO ()
exitImmediately ExitCode
ExitSuccess
    String
s <-
      IO String
getLine IO String -> (SomeException -> IO ()) -> IO String
forall a b. IO a -> (SomeException -> IO b) -> IO a
`onSomeException`
      (\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 ()
forall a. Show a => Handle -> a -> IO ()
hPrint Handle
stderr String
errorMessage
         String -> IO ()
cb String
errorMessage)
    String -> IO ()
cb (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ StdinReader -> ShowS
escape StdinReader
stdinReader String
s
    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