From 56b31659dfb6dffc166b4e9645cfa825996cb3cb Mon Sep 17 00:00:00 2001 From: Reto Hablützel Date: Sat, 9 Aug 2014 18:32:02 +0200 Subject: New Plugin MarqueePipeReader Display a long text from a pipe with marquee. It wraps around with a given separator and the length may be specified as well as the delay in 10th seconds. See readme for an example. --- readme.md | 7 +++++ src/Config.hs | 3 +- src/Plugins/MarqueePipeReader.hs | 68 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/Plugins/MarqueePipeReader.hs diff --git a/readme.md b/readme.md index 6209b44..3493e8b 100644 --- a/readme.md +++ b/readme.md @@ -1220,6 +1220,13 @@ can be used in the output template as `%mydate%` - Reads its displayed output from the given pipe. - Prefix an optional default text separated by a colon +**`MarqueePipeReader "default text:/path/to/pipe" (length, rate, sep) Alias`** + +- Generally equivalent to PipeReader +- Text is displayed as marquee with the specified length, rate in 10th seconds and separator when it wraps around + + Run MarqueePipeReader "/tmp/testpipe" (10, 7, "+") "mpipe" + **`BufferedPipeReader Alias [(Timeout, Bool, "/path/to/pipe1"), ..]`** diff --git a/src/Config.hs b/src/Config.hs index ed3e51a..bda8838 100644 --- a/src/Config.hs +++ b/src/Config.hs @@ -30,6 +30,7 @@ import Plugins.Monitors import Plugins.Date import Plugins.PipeReader import Plugins.BufferedPipeReader +import Plugins.MarqueePipeReader import Plugins.CommandReader import Plugins.StdinReader import Plugins.XMonadLog @@ -136,6 +137,6 @@ infixr :*: -- this function's type signature. runnableTypes :: Command :*: Monitors :*: Date :*: PipeReader :*: BufferedPipeReader :*: CommandReader :*: StdinReader :*: XMonadLog :*: EWMH :*: Kbd :*: Locks :*: Mail :*: MBox :*: - DateZone :*: + DateZone :*: MarqueePipeReader :*: () runnableTypes = undefined diff --git a/src/Plugins/MarqueePipeReader.hs b/src/Plugins/MarqueePipeReader.hs new file mode 100644 index 0000000..babcfb6 --- /dev/null +++ b/src/Plugins/MarqueePipeReader.hs @@ -0,0 +1,68 @@ +----------------------------------------------------------------------------- +-- | +-- Module : Plugins.MarqueePipeReader +-- Copyright : (c) Reto Habluetzel +-- License : BSD-style (see LICENSE) +-- +-- Maintainer : Jose A. Ortega Ruiz +-- Stability : unstable +-- Portability : unportable +-- +-- A plugin for reading from named pipes for long texts with marquee +-- +----------------------------------------------------------------------------- + +module Plugins.MarqueePipeReader where + +import System.IO (openFile, IOMode(ReadWriteMode), Handle) +import Plugins (tenthSeconds, Exec(alias, start), hGetLineSafe) +import System.Posix.Files (getFileStatus, isNamedPipe) +import Control.Concurrent(forkIO, threadDelay) +import Control.Concurrent.STM (TChan, atomically, writeTChan, tryReadTChan, newTChan) +import Control.Exception +import Control.Monad(forever, unless) + +type Length = Int -- length of the text to display +type Rate = Int -- delay in tenth seconds +type Seperator = String -- if text wraps around, use separator + +data MarqueePipeReader = MarqueePipeReader String (Length, Rate, Seperator) String + deriving (Read, Show) + +instance Exec MarqueePipeReader where + alias (MarqueePipeReader _ _ a) = a + start (MarqueePipeReader p (len, rate, sep) _) cb = do + let (def, pipe) = split ':' p + unless (null def) (cb def) + checkPipe pipe + h <- openFile pipe ReadWriteMode + line <- hGetLineSafe h + chan <- atomically newTChan + forkIO $ writer (toInfTxt line sep) sep len rate chan cb + forever $ pipeToChan h chan + where + split c xs | c `elem` xs = let (pre, post) = span (c /=) xs + in (pre, dropWhile (c ==) post) + | otherwise = ([], xs) + +pipeToChan :: Handle -> TChan String -> IO () +pipeToChan h chan = do + line <- hGetLineSafe h + atomically $ writeTChan chan line + +writer :: String -> Seperator -> Length -> Rate -> TChan String -> (String -> IO ()) -> IO () +writer txt sep len rate chan cb = do + cb (take len txt) + mbnext <- atomically $ tryReadTChan chan + case mbnext of + Just new -> writer (toInfTxt new sep) sep len rate chan cb + Nothing -> tenthSeconds rate >> writer (drop 1 txt) sep len rate chan cb + +toInfTxt :: String -> String -> String +toInfTxt line sep = concat (repeat $ line ++ " " ++ sep ++ " ") + +checkPipe :: FilePath -> IO () +checkPipe file = handle (\(SomeException _) -> waitForPipe) $ do + status <- getFileStatus file + unless (isNamedPipe status) waitForPipe + where waitForPipe = threadDelay 1000 >> checkPipe file -- cgit v1.2.3