Skip to content
Snippets Groups Projects
Commit f1663d49 authored by Jens Nolte's avatar Jens Nolte
Browse files

Move BlockOutput from QBar.Core to QBar.BlockOutput

parent 047ab399
No related branches found
No related tags found
No related merge requests found
{-# LANGUAGE TemplateHaskell #-}
module QBar.BlockOutput where
import QBar.BlockText
import Control.Lens
import Data.Aeson.TH
import qualified Data.Text.Lazy as T
data BlockOutput = BlockOutput
{ _fullText :: BlockText
, _shortText :: Maybe BlockText
, _blockName :: Maybe T.Text
, _invalid :: Bool
}
$(deriveJSON defaultOptions ''BlockOutput)
makeLenses ''BlockOutput
mkBlockOutput :: BlockText -> BlockOutput
mkBlockOutput text = BlockOutput
{ _fullText = text
, _shortText = Nothing
, _blockName = Nothing
, _invalid = False
}
mkErrorOutput :: T.Text -> BlockOutput
mkErrorOutput = mkBlockOutput . importantText criticalImportant
emptyBlock :: BlockOutput
emptyBlock = mkBlockOutput mempty
addIcon :: T.Text -> BlockOutput -> BlockOutput
addIcon icon = over fullText $ (<>) . normalText $ icon <> " "
...@@ -5,6 +5,7 @@ module QBar.Blocks.Battery where ...@@ -5,6 +5,7 @@ module QBar.Blocks.Battery where
import QBar.Core hiding (name) import QBar.Core hiding (name)
import QBar.BlockOutput
import QBar.BlockText import QBar.BlockText
import Pipes import Pipes
...@@ -86,7 +87,7 @@ batteryBlock = do ...@@ -86,7 +87,7 @@ batteryBlock = do
updateBatteryBlock :: Bool -> [BatteryState] -> Block () updateBatteryBlock :: Bool -> [BatteryState] -> Block ()
updateBatteryBlock _ [] = yield Nothing updateBatteryBlock _ [] = yield Nothing
updateBatteryBlock isPlugged bs = updateBlock $ (shortText.~shortText') $ createBlock fullText' updateBatteryBlock isPlugged bs = updateBlock $ (shortText.~shortText') $ mkBlockOutput fullText'
where where
fullText' :: BlockText fullText' :: BlockText
fullText' = normalText (batteryIcon <> " ") <> overallPercentage <> optionalEachBattery <> optionalOverallEstimate fullText' = normalText (batteryIcon <> " ") <> overallPercentage <> optionalEachBattery <> optionalOverallEstimate
......
module QBar.Blocks.Date where module QBar.Blocks.Date where
import QBar.BlockOutput
import QBar.BlockText
import QBar.Core import QBar.Core
import QBar.Time import QBar.Time
import QBar.BlockText
import qualified Data.Text.Lazy as T import qualified Data.Text.Lazy as T
import Data.Time.Format import Data.Time.Format
...@@ -24,4 +25,4 @@ dateBlockOutput = do ...@@ -24,4 +25,4 @@ dateBlockOutput = do
let date = T.pack (formatTime defaultTimeLocale "%a %F" zonedTime) let date = T.pack (formatTime defaultTimeLocale "%a %F" zonedTime)
let time = T.pack (formatTime defaultTimeLocale "%R" zonedTime) let time = T.pack (formatTime defaultTimeLocale "%R" zonedTime)
let text = normalText ("📅\xFE0E " <> date <> " ") <> activeText time let text = normalText ("📅\xFE0E " <> date <> " ") <> activeText time
return $ blockName ?~ "date" $ createBlock text return $ blockName ?~ "date" $ mkBlockOutput text
...@@ -7,6 +7,7 @@ import QBar.Cli (MainOptions(..)) ...@@ -7,6 +7,7 @@ import QBar.Cli (MainOptions(..))
import QBar.Core import QBar.Core
-- TODO: remove dependency? -- TODO: remove dependency?
import QBar.Filter import QBar.Filter
import QBar.BlockOutput
import QBar.BlockText import QBar.BlockText
import Control.Exception (handle) import Control.Exception (handle)
...@@ -17,6 +18,8 @@ import Control.Concurrent.Async ...@@ -17,6 +18,8 @@ import Control.Concurrent.Async
import Control.Concurrent.STM.TChan (TChan, writeTChan) import Control.Concurrent.STM.TChan (TChan, writeTChan)
import Data.Aeson.TH import Data.Aeson.TH
import Data.ByteString (ByteString) import Data.ByteString (ByteString)
import System.FilePath ((</>))
import System.IO
import Data.Either (either) import Data.Either (either)
import Data.Maybe (maybe) import Data.Maybe (maybe)
import Data.Text.Lazy (Text, pack) import Data.Text.Lazy (Text, pack)
...@@ -30,9 +33,6 @@ import Pipes.Aeson.Unchecked (encode) ...@@ -30,9 +33,6 @@ import Pipes.Aeson.Unchecked (encode)
import Pipes.Network.TCP (fromSocket, toSocket) import Pipes.Network.TCP (fromSocket, toSocket)
import System.Directory (removeFile, doesFileExist) import System.Directory (removeFile, doesFileExist)
import System.Environment (getEnv) import System.Environment (getEnv)
import System.FilePath ((</>))
import System.IO
type CommandChan = TChan Command type CommandChan = TChan Command
data Command = SetFilter Filter data Command = SetFilter Filter
...@@ -128,5 +128,5 @@ handleBlockStream producer = do ...@@ -128,5 +128,5 @@ handleBlockStream producer = do
where where
handleParsedBlock :: Producer ByteString IO () -> String -> PushBlock handleParsedBlock :: Producer ByteString IO () -> String -> PushBlock
handleParsedBlock leftovers update = do handleParsedBlock leftovers update = do
updateBlock $ createBlock . normalText $ TL.pack update updateBlock $ mkBlockOutput . normalText $ TL.pack update
handleBlockStream leftovers handleBlockStream leftovers
...@@ -3,16 +3,18 @@ ...@@ -3,16 +3,18 @@
module QBar.Core where module QBar.Core where
import QBar.BlockOutput
import QBar.BlockText import QBar.BlockText
import Control.Exception (IOException)
import Control.Monad (forever)
import Control.Monad.Reader (ReaderT, runReaderT, ask)
import Control.Concurrent (threadDelay) import Control.Concurrent (threadDelay)
import Control.Concurrent.Async import Control.Concurrent.Async
import Control.Concurrent.Event as Event import Control.Concurrent.Event as Event
import Control.Concurrent.MVar import Control.Concurrent.MVar
import Control.Concurrent.STM.TChan (TChan, writeTChan) import Control.Concurrent.STM.TChan (TChan, writeTChan)
import Control.Exception (IOException)
import Control.Lens
import Control.Monad (forever)
import Control.Monad.Reader (ReaderT, runReaderT, ask)
import Data.Aeson.TH import Data.Aeson.TH
import qualified Data.ByteString.Lazy.Char8 as C8 import qualified Data.ByteString.Lazy.Char8 as C8
import Data.Int (Int64) import Data.Int (Int64)
...@@ -28,7 +30,6 @@ import System.Exit ...@@ -28,7 +30,6 @@ import System.Exit
import System.IO import System.IO
import System.Process.Typed (Process, shell, setStdin, setStdout, import System.Process.Typed (Process, shell, setStdin, setStdout,
getStdout, closed, createPipe, readProcessStdout, startProcess, stopProcess) getStdout, closed, createPipe, readProcessStdout, startProcess, stopProcess)
import Control.Lens
data BlockEvent = Click { data BlockEvent = Click {
...@@ -37,14 +38,6 @@ data BlockEvent = Click { ...@@ -37,14 +38,6 @@ data BlockEvent = Click {
} deriving Show } deriving Show
$(deriveJSON defaultOptions ''BlockEvent) $(deriveJSON defaultOptions ''BlockEvent)
data BlockOutput = BlockOutput
{ _fullText :: BlockText
, _shortText :: Maybe BlockText
, _blockName :: Maybe T.Text
, _invalid :: Bool
}
$(deriveJSON defaultOptions ''BlockOutput)
data PushMode = PushMode data PushMode = PushMode
data PullMode = PullMode data PullMode = PullMode
...@@ -84,7 +77,6 @@ data Bar = Bar { ...@@ -84,7 +77,6 @@ data Bar = Bar {
requestBarUpdate :: IO (), requestBarUpdate :: IO (),
newBlockChan :: TChan CachedBlock newBlockChan :: TChan CachedBlock
} }
makeLenses ''BlockOutput
instance IsBlock PushBlock where instance IsBlock PushBlock where
toCachedBlock = cachePushBlock toCachedBlock = cachePushBlock
...@@ -114,23 +106,6 @@ runBarIO bar action = runReaderT (runSafeT action) bar ...@@ -114,23 +106,6 @@ runBarIO bar action = runReaderT (runSafeT action) bar
askBar :: BarIO Bar askBar :: BarIO Bar
askBar = lift ask askBar = lift ask
createBlock :: BlockText -> BlockOutput
createBlock text = BlockOutput
{ _fullText = text
, _shortText = Nothing
, _blockName = Nothing
, _invalid = False
}
createErrorBlock :: T.Text -> BlockOutput
createErrorBlock = createBlock . importantText criticalImportant
emptyBlock :: BlockOutput
emptyBlock = createBlock mempty
addIcon :: T.Text -> BlockOutput -> BlockOutput
addIcon icon = over fullText $ (<>) . normalText $ icon <> " "
modify :: (BlockOutput -> BlockOutput) modify :: (BlockOutput -> BlockOutput)
-> Pipe BlockState BlockState BarIO r -> Pipe BlockState BlockState BarIO r
modify x = PP.map (over (_Just . _1) x) modify x = PP.map (over (_Just . _1) x)
...@@ -252,9 +227,9 @@ blockScript path = forever $ updateBlock =<< (lift blockScriptAction) ...@@ -252,9 +227,9 @@ blockScript path = forever $ updateBlock =<< (lift blockScriptAction)
(text:short:_) -> shortText ?~ pangoText short $ createScriptBlock text (text:short:_) -> shortText ?~ pangoText short $ createScriptBlock text
(text:_) -> createScriptBlock text (text:_) -> createScriptBlock text
[] -> createScriptBlock "-" [] -> createScriptBlock "-"
(ExitFailure nr) -> return $ createErrorBlock $ "[" <> T.pack (show nr) <> "]" (ExitFailure nr) -> return $ mkErrorOutput $ "[" <> T.pack (show nr) <> "]"
createScriptBlock :: T.Text -> BlockOutput createScriptBlock :: T.Text -> BlockOutput
createScriptBlock text = blockName ?~ T.pack path $ createBlock . pangoText $ text createScriptBlock text = blockName ?~ T.pack path $ mkBlockOutput . pangoText $ text
startPersistentBlockScript :: FilePath -> PushBlock startPersistentBlockScript :: FilePath -> PushBlock
-- The outer catchP only catches errors that occur during process creation -- The outer catchP only catches errors that occur during process creation
...@@ -262,7 +237,7 @@ startPersistentBlockScript path = catchP startScriptProcess handleError ...@@ -262,7 +237,7 @@ startPersistentBlockScript path = catchP startScriptProcess handleError
where where
handleError :: IOException -> PushBlock handleError :: IOException -> PushBlock
handleError e = do handleError e = do
updateBlock . createErrorBlock $ "[" <> T.pack (show e) <> "]" updateBlock . mkErrorOutput $ "[" <> T.pack (show e) <> "]"
exitBlock exitBlock
handleErrorWithProcess :: (Process i o e) -> IOException -> PushBlock handleErrorWithProcess :: (Process i o e) -> IOException -> PushBlock
handleErrorWithProcess process e = do handleErrorWithProcess process e = do
...@@ -278,7 +253,7 @@ startPersistentBlockScript path = catchP startScriptProcess handleError ...@@ -278,7 +253,7 @@ startPersistentBlockScript path = catchP startScriptProcess handleError
blockFromHandle :: Handle -> PushBlock blockFromHandle :: Handle -> PushBlock
blockFromHandle handle = forever $ do blockFromHandle handle = forever $ do
line <- liftIO $ TIO.hGetLine handle line <- liftIO $ TIO.hGetLine handle
updateBlock $ createBlock . pangoText $ line updateBlock $ mkBlockOutput . pangoText $ line
lift updateBar lift updateBar
addBlock :: IsBlock a => a -> BarIO () addBlock :: IsBlock a => a -> BarIO ()
......
module QBar.DefaultConfig where module QBar.DefaultConfig where
import QBar.Blocks import QBar.Blocks
import QBar.BlockOutput
import QBar.Core import QBar.Core
import Pipes import Pipes
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
module QBar.Filter where module QBar.Filter where
import QBar.Core import QBar.BlockOutput
import QBar.BlockText import QBar.BlockText
import Control.Monad.State.Lazy (State, evalState, get, put) import Control.Monad.State.Lazy (State, evalState, get, put)
......
module QBar.Server where module QBar.Server where
import QBar.Blocks import QBar.Blocks
import QBar.BlockOutput
import QBar.BlockText import QBar.BlockText
import QBar.Core import QBar.Core
import QBar.Cli import QBar.Cli
...@@ -36,7 +37,7 @@ data Handle = Handle { ...@@ -36,7 +37,7 @@ data Handle = Handle {
renderIndicator :: CachedBlock renderIndicator :: CachedBlock
-- Using 'cachedBlock' is a hack to actually get the block to update on every bar update (by doing this it will not get a cache later in the pipeline). -- Using 'cachedBlock' is a hack to actually get the block to update on every bar update (by doing this it will not get a cache later in the pipeline).
renderIndicator = forever $ each $ map (mkBlockState . createBlock . normalText) ["/", "-", "\\", "|"] renderIndicator = forever $ each $ map (mkBlockState . mkBlockOutput . normalText) ["/", "-", "\\", "|"]
runBlock :: CachedBlock -> BarIO (Maybe (BlockState, CachedBlock)) runBlock :: CachedBlock -> BarIO (Maybe (BlockState, CachedBlock))
runBlock producer = do runBlock producer = do
...@@ -183,7 +184,7 @@ renderInitialBlocks options handle blockFilter = do ...@@ -183,7 +184,7 @@ renderInitialBlocks options handle blockFilter = do
date <- dateBlockOutput date <- dateBlockOutput
let initialBlocks = [mkBlockState date] let initialBlocks = [mkBlockState date]
-- Attach spinner indicator when verbose flag is set -- Attach spinner indicator when verbose flag is set
let initialBlocks' = if indicator options then initialBlocks <> [mkBlockState $ createBlock . normalText $ "*"] else initialBlocks let initialBlocks' = if indicator options then initialBlocks <> [mkBlockState $ mkBlockOutput . normalText $ "*"] else initialBlocks
-- Render initial time block so the bar is not empty after startup -- Render initial time block so the bar is not empty after startup
renderLine options handle blockFilter initialBlocks' "" renderLine options handle blockFilter initialBlocks' ""
......
module QBar.Themes where module QBar.Themes where
import QBar.BlockOutput
import QBar.BlockText import QBar.BlockText
import QBar.Core
import qualified Data.Text.Lazy as T import qualified Data.Text.Lazy as T
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment