From 18f5e7a9a27bf15ee0bcf4a51155674074a6b8d1 Mon Sep 17 00:00:00 2001 From: 4Lost Date: Sat, 16 Mar 2024 16:17:39 +0100 Subject: [PATCH] xmobar -> test own Modules 2 --- home/programs/xmonad/default.nix | 8 +- home/programs/xmonad/src/xmobar/Commands.hs | 85 +++++++++++++++++++ .../xmonad/src/xmobar/Plugins/Audio.hs | 2 +- .../xmonad/src/xmobar/xmobar-custom.cabal | 5 +- 4 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 home/programs/xmonad/src/xmobar/Commands.hs diff --git a/home/programs/xmonad/default.nix b/home/programs/xmonad/default.nix index 3648c49..0626f46 100644 --- a/home/programs/xmonad/default.nix +++ b/home/programs/xmonad/default.nix @@ -12,5 +12,11 @@ }; }; - home.packages = with pkgs; [ xmobar maim xdotool xorg.xmessage ]; + home.packages = with pkgs; [ + xmobar + maim + xdotool + xorg.xmessage + cabal-install + ]; } diff --git a/home/programs/xmonad/src/xmobar/Commands.hs b/home/programs/xmonad/src/xmobar/Commands.hs new file mode 100644 index 0000000..a4ab5ed --- /dev/null +++ b/home/programs/xmonad/src/xmobar/Commands.hs @@ -0,0 +1,85 @@ +----------------------------------------------------------------------------- +-- | +-- Module : Xmobar.Commands +-- Copyright : (c) Andrea Rossato +-- License : BSD-style (see LICENSE) +-- +-- Maintainer : Jose A. Ortega Ruiz +-- Stability : unstable +-- Portability : unportable +-- +-- The 'Exec' class and the 'Command' data type. +-- +-- The 'Exec' class rappresents the executable types, whose constructors may +-- appear in the 'Config.commands' field of the 'Config.Config' data type. +-- +-- The 'Command' data type is for OS commands to be run by xmobar +-- +----------------------------------------------------------------------------- + +module Commands + ( Command (..) + , Exec (..) + , tenthSeconds + ) where + +import Prelude +import Control.Concurrent +import Control.Exception (handle, SomeException(..)) +import Data.Char +import System.Process +import System.Exit +import System.IO (hClose) + +import Signal +import XUtil + +class Show e => Exec e where + alias :: e -> String + alias e = takeWhile (not . isSpace) $ show e + rate :: e -> Int + rate _ = 10 + run :: e -> IO String + run _ = return "" + start :: e -> (String -> IO ()) -> IO () + start e cb = go + where go = run e >>= cb >> tenthSeconds (rate e) >> go + trigger :: e -> (Maybe SignalType -> IO ()) -> IO () + trigger _ sh = sh Nothing + +data Command = Com Program Args Alias Rate + deriving (Show,Read,Eq) + +type Args = [String] +type Program = String +type Alias = String +type Rate = Int + +instance Exec Command where + alias (Com p _ a _) + | p /= "" = if a == "" then p else a + | otherwise = "" + start (Com prog args _ r) cb = if r > 0 then go else exec + where go = exec >> tenthSeconds r >> go + exec = do + (i,o,e,p) <- runInteractiveCommand (unwords (prog:args)) + exit <- waitForProcess p + let closeHandles = hClose o >> hClose i >> hClose e + getL = handle (\(SomeException _) -> return "") + (hGetLineSafe o) + case exit of + ExitSuccess -> do str <- getL + closeHandles + cb str + _ -> do closeHandles + cb $ "Could not execute command " ++ prog + + +-- | Work around to the Int max bound: since threadDelay takes an Int, it +-- is not possible to set a thread delay grater than about 45 minutes. +-- With a little recursion we solve the problem. +tenthSeconds :: Int -> IO () +tenthSeconds s | s >= x = do threadDelay (x * 100000) + tenthSeconds (s - x) + | otherwise = threadDelay (s * 100000) + where x = (maxBound :: Int) `div` 100000 diff --git a/home/programs/xmonad/src/xmobar/Plugins/Audio.hs b/home/programs/xmonad/src/xmobar/Plugins/Audio.hs index e8e5ec5..4f4919d 100644 --- a/home/programs/xmonad/src/xmobar/Plugins/Audio.hs +++ b/home/programs/xmonad/src/xmobar/Plugins/Audio.hs @@ -16,7 +16,7 @@ module Plugins.Audio where ---import Plugins +import Plugins import System.Process import GHC.Show (Show(show)) diff --git a/home/programs/xmonad/src/xmobar/xmobar-custom.cabal b/home/programs/xmonad/src/xmobar/xmobar-custom.cabal index 9547c55..c48143b 100644 --- a/home/programs/xmonad/src/xmobar/xmobar-custom.cabal +++ b/home/programs/xmonad/src/xmobar/xmobar-custom.cabal @@ -42,7 +42,10 @@ executable xmobar -- .hs or .lhs file containing the Main module. main-is: xmobar.hs - other-modules: Plugins.Audio + other-modules: Commands, + XUtil, + Plugins, + Plugins.Audio -- Other library packages from which modules are imported. build-depends: base ^>=4.17.2.1, xmobar ^>=0.47.1,