From b3f07ba590def8f150789f044446ded2591c9bbd Mon Sep 17 00:00:00 2001 From: ndwarshuis Date: Sun, 1 Jan 2023 11:44:36 -0500 Subject: [PATCH] ENH use optparse for xmobar --- bin/xmobar.hs | 52 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/bin/xmobar.hs b/bin/xmobar.hs index e81af0d..e7fa370 100644 --- a/bin/xmobar.hs +++ b/bin/xmobar.hs @@ -13,12 +13,12 @@ module Main (main) where import Data.Internal.DBus import Data.Internal.Dependency +import Options.Applicative import RIO hiding (hFlush) import qualified RIO.ByteString.Lazy as BL import RIO.List import RIO.Process import qualified RIO.Text as T -import UnliftIO.Environment import XMonad.Core hiding (config) import XMonad.Internal.Command.Desktop import XMonad.Internal.Command.Power @@ -40,19 +40,42 @@ import Xmobar.Plugins.Screensaver import Xmobar.Plugins.VPN main :: IO () -main = getArgs >>= parse +main = parse >>= xio -parse :: [String] -> IO () -parse [] = run -parse ["--deps"] = withCache printDeps -parse ["--test"] = withCache $ withDBus_ evalConfig -parse _ = usage +parse :: IO XOpts +parse = execParser opts + where + parseOpts = parseDeps <|> parseTest <|> pure XRun + opts = + info (parseOpts <**> helper) $ + fullDesc <> header "xmobar: the best taskbar ever" -run :: IO () +data XOpts = XDeps | XTest | XRun + +parseDeps :: Parser XOpts +parseDeps = + flag' + XDeps + (long "deps" <> short 'd' <> help "print dependencies") + +parseTest :: Parser XOpts +parseTest = + flag' + XTest + (long "test" <> short 't' <> help "test dependencies without running") + +xio :: XOpts -> IO () +xio o = withCache $ + case o of + XDeps -> printDeps + XTest -> withDBus_ evalConfig + XRun -> run + +run :: FIO () run = do -- IDK why this is needed, I thought this was default - hSetBuffering stdout LineBuffering - withCache $ withDBus_ $ \db -> do + liftIO $ hSetBuffering stdout LineBuffering + withDBus_ $ \db -> do c <- evalConfig db liftIO $ xmobar c @@ -73,15 +96,6 @@ printDeps = withDBus_ $ \db -> concatMap dumpFeature $ allFeatures db -usage :: IO () -usage = - putStrLn $ - intercalate - "\n" - [ "xmobar: run greatest taskbar" - , "xmobar --deps: print dependencies" - ] - -------------------------------------------------------------------------------- -- toplevel configuration