summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--doc/plugins.org34
-rw-r--r--src/Xmobar/Plugins/PacmanUpdates.hs74
2 files changed, 77 insertions, 31 deletions
diff --git a/doc/plugins.org b/doc/plugins.org
index 442af34..4d14771 100644
--- a/doc/plugins.org
+++ b/doc/plugins.org
@@ -1367,7 +1367,7 @@
*** =PacmanUpdates (Zero, One, Many, Error) Rate=
- - *This constructor is deprecated. Use =PacmanUpdatesK= or =PacmanUpdatesNoK= instead.*
+ - *This constructor is deprecated. Use =PacmanUpdatesK=, =PacmanUpdatesPredicateK=, or =PacmanUpdatesNoK= instead.*
- Aliases to =pacman=
- =Zero=: a =String= to use when the system is up to date.
- =One=: a =String= to use when only one update is available.
@@ -1407,6 +1407,38 @@
_ -> error "This is impossible"
#+end_src
+*** =PacmanUpdatesPredicateK Rate IsKernelUpdate (Bool -> Either String (Int, Bool) -> String)=
+
+ - Aliases to =pacman=
+ - =IsKernelUpdate=: a =String -> Bool= predicate that receives each available
+ package name and returns =True= for kernel packages. This is useful for
+ distributions with versioned kernel package names, such as Manjaro's
+ =linux618= or =linux70= packages.
+ - =(Bool -> Either String (Int, Bool) -> String)=: a function producing the
+ string to be shown by the plugin; it is fed with a =Bool= telling whether
+ the running kernel is older than the installed kernel, and an =Int= and
+ =Bool= telling the number of available updates and whether one of them
+ matches the kernel predicate (or an error message if =checkupdates= fails).
+ - Example:
+ This example requires =import Data.Char (isDigit)= and
+ =import Data.List (stripPrefix)= in your configuration.
+ #+begin_src haskell
+ PacmanUpdatesPredicateK
+ 600
+ ( \packageName ->
+ case stripPrefix "linux" packageName of
+ Just n -> not (null n) && all isDigit n
+ Nothing -> False
+ )
+ $ \oldKern mayb -> (if oldKern then "Running old kernel!" else "") ++
+ case mayb of
+ Left _ -> "Some error occurred!"
+ Right (0, False) -> "Up to date"
+ Right (n, pendingK) | n >= 1 -> show n ++ " updates available"
+ ++ if pendingK then " including a kernel update" else ""
+ _ -> error "This is impossible"
+ #+end_src
+
*** =PacmanUpdatesNoK Rate (Bool -> Either String Int -> String)=
- Aliases to =pacman=
diff --git a/src/Xmobar/Plugins/PacmanUpdates.hs b/src/Xmobar/Plugins/PacmanUpdates.hs
index 03b556f..527580b 100644
--- a/src/Xmobar/Plugins/PacmanUpdates.hs
+++ b/src/Xmobar/Plugins/PacmanUpdates.hs
@@ -25,10 +25,12 @@ available (provided the name of the kernel package), and whether the running ker
than the installed one.
-}
module Xmobar.Plugins.PacmanUpdates (
- {-# DEPRECATED "This ctor is DEPRECATED; please use `PacmanUpdates` type and `PacmanUpdatesK` and `PacmanUpdatesNoK` constructors instead." #-}
+ {-# DEPRECATED "This ctor is DEPRECATED; please use `PacmanUpdates` type and `PacmanUpdatesK`, `PacmanUpdatesPredicateK` and `PacmanUpdatesNoK` constructors instead." #-}
pattern PacmanUpdates
, PacmanUpdates ()
+ , PacmanUpdatesKernelCheck (..)
, pattern PacmanUpdatesK
+ , pattern PacmanUpdatesPredicateK
, pattern PacmanUpdatesNoK) where
import System.Exit (ExitCode (..))
@@ -43,13 +45,13 @@ import Control.Arrow ((&&&))
import qualified Data.Vector as V
-- | Deprecated plugin ctor (will be deleted in 2027).
--- Use `PacmanUpdatesK` or `PacmanUpdatesNoK` instead.
+-- Use `PacmanUpdatesK`, `PacmanUpdatesPredicateK`, or `PacmanUpdatesNoK` instead.
pattern PacmanUpdates :: (String, String, String, String) -- ^ `String`s to be shown for 0, 1, ≥ 2 updates,
-- and for error respectively (in the 3rd string, for
-- ≥ 2 updates, any occurrence of the '?' character
-- is a placeholder for the number of available updates).
-> Rate -- ^ `Rate` of update (see [Xmobar doc](https://codeberg.org/xmobar/xmobar/src/commit/39fd70308c3aef5402abe7152ade76ff7bb331bb/src/Xmobar/Plugins/Command.hs#L34)).
- -> PacmanUpdates False
+ -> PacmanUpdates NoKernelCheck
pattern PacmanUpdates irrelevant <- (error "PacmanUpdates: PacmanUpdates is a build-only pattern synonym (a ctor synonym)." -> irrelevant)
where PacmanUpdates zome r
= let (z, o, m, e) = zome
@@ -65,10 +67,12 @@ pattern PacmanUpdates irrelevant <- (error "PacmanUpdates: PacmanUpdates is a bu
++ "deprecated plugin, click here</action>)</fc>"
in PacmanUpdatesNoK r printer
--- | PacmanUpdates plugin parametrized over `Bool` kind: if `True` the plugin
--- will detect if there's pending update(s) for the kernel package; if `False`
--- it wont.
-data PacmanUpdates (b :: Bool)
+
+-- | Different types of kernel checks.
+data PacmanUpdatesKernelCheck = NoKernelCheck | PredicateKernelCheck
+
+-- | PacmanUpdates plugin parametrized over the `PacmanUpdatesKernelCheck` kind.
+data PacmanUpdates (b :: PacmanUpdatesKernelCheck)
= Make -- ^ Constructor.
Rate -- ^ `Rate` of update (see [Xmobar doc](https://codeberg.org/xmobar/xmobar/src/commit/39fd70308c3aef5402abe7152ade76ff7bb331bb/src/Xmobar/Plugins/Command.hs#L34)).
(Arg b) -- ^ Optional further argument. See instances of `Updates`.
@@ -80,12 +84,12 @@ instance Show (PacmanUpdates b) where
instance Read (PacmanUpdates b) where
readsPrec = error "PacmanUpdates: Read instance is stub"
-instance Updates b => Exec (PacmanUpdates (b :: Bool)) where
+instance Updates b => Exec (PacmanUpdates (b :: PacmanUpdatesKernelCheck)) where
alias = const "pacman"
rate (Make r _ _) = r
run = Xmobar.Plugins.PacmanUpdates.run'
-class Updates (b :: Bool) where
+class Updates (b :: PacmanUpdatesKernelCheck) where
-- | See `Updates`'s instances.
type Arg b = (a :: Type) | a -> b
-- | See `Updates`'s instances.
@@ -98,34 +102,44 @@ class Updates (b :: Bool) where
-- the system is running an outdated kernel, and an `Int` telling
-- the number of available updates (or `Left` if an error occurred
-- when calling `checkupdates`).
-instance Updates False where
- type Arg False = Void
- type Printer False = Bool -> Either String Int -> String
+instance Updates NoKernelCheck where
+ type Arg NoKernelCheck = Void
+ type Printer NoKernelCheck = Bool -> Either String Int -> String
run' (Make _ _ printer)
= printer
<$> kernIsOld
<*> (fmap V.length <$> checkUpdates)
--- | Constructing the plugin requires an additional `String` telling the name
--- name of the kernel package; the user-provided printer is fed with a `Bool`
--- telling whether the system is running an outdated kernel, and an `(Int,
--- Bool)` pair telling the number of available updates and whether one of these
--- is a kernel update (or `Left` if an error occurred when calling
--- `checkupdates`).
-instance Updates True where
- type Arg True = String
- type Printer True = Bool -> Either String (Int, Bool) -> String
- run' (Make _ kernName printer)
+-- | Constructing the plugin requires an additional `String -> Bool` predicate
+-- that receives each available package name and returns `True` for kernel
+-- packages; the user-provided printer is fed with a `Bool` telling whether the
+-- system is running an outdated kernel, and an `(Int, Bool)` pair telling the
+-- number of available updates and whether one of these is a kernel update (or
+-- `Left` if an error occurred when calling `checkupdates`).
+instance Updates PredicateKernelCheck where
+ type Arg PredicateKernelCheck = String -> Bool
+ type Printer PredicateKernelCheck = Bool -> Either String (Int, Bool) -> String
+ run' (Make _ checkKern printer)
= printer
<$> kernIsOld
- <*> (fmap (V.length &&& elem kernName) <$> checkUpdates)
-
--- | Pattern synonym used to construct a `PacmanUpdates True`.
-pattern PacmanUpdatesK :: Rate -> Arg True -> Printer True -> PacmanUpdates True
-pattern PacmanUpdatesK r a p = Make r a p
-
--- | Pattern synonym used to construct a `PacmanUpdates False`.
-pattern PacmanUpdatesNoK :: Rate -> Printer False -> PacmanUpdates False
+ <*> (fmap (V.length &&& V.any checkKern) <$> checkUpdates)
+
+-- | Pattern synonym to construct a `PacmanUpdates PredicateKernelCheck` that
+-- detects updates for packages matched by the given @(String -> Bool)@
+-- predicate. This can be used to detect kernel updates for distributions
+-- with versioned kernel package names (e.g. Manjaro's @linux618@)
+pattern PacmanUpdatesPredicateK :: Rate -> Arg PredicateKernelCheck -> Printer PredicateKernelCheck -> PacmanUpdates PredicateKernelCheck
+pattern PacmanUpdatesPredicateK r a p = Make r a p
+
+-- | A convenience wrapper around PacmanUpdatesPredicateK with the predicate @(== kernName)@
+-- Construction only: the kernel name cannot be recovered when matching.
+pattern PacmanUpdatesK :: Rate -> String -> Printer PredicateKernelCheck -> PacmanUpdates PredicateKernelCheck
+pattern PacmanUpdatesK r kernName p <-
+ (error "PacmanUpdatesK: build-only pattern synonym (a ctor synonym)." -> (r, kernName, p))
+ where PacmanUpdatesK r kernName p = PacmanUpdatesPredicateK r (== kernName) p
+
+-- | Pattern synonym used to construct a `PacmanUpdates NoKernelCheck`.
+pattern PacmanUpdatesNoK :: Rate -> Printer NoKernelCheck -> PacmanUpdates NoKernelCheck
pattern PacmanUpdatesNoK r p <- Make r _ p
where PacmanUpdatesNoK r p = Make r undefined p