-----------------------------------------------------------------------------
-- |
-- Module      :  Plugins.Monitors.Batt.Common
-- Copyright   :  (c) 2010, 2011, 2012, 2013, 2015, 2016, 2018, 2019 Jose A Ortega
--                (c) 2010 Andrea Rossato, Petr Rockai
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  Jose A. Ortega Ruiz <jao@gnu.org>
-- Stability   :  unstable
-- Portability :  unportable
--
-- A battery monitor for Xmobar
--
-----------------------------------------------------------------------------

module Xmobar.Plugins.Monitors.Batt.Common (BattOpts(..)
                                           , Result(..)
                                           , Status(..)
                                           , maybeAlert) where

import System.Process (system)
import Control.Monad (unless, void)
import Xmobar.Plugins.Monitors.Common

data Status = Charging | Discharging | Full | Idle | Unknown deriving (ReadPrec [Status]
ReadPrec Status
Int -> ReadS Status
ReadS [Status]
(Int -> ReadS Status)
-> ReadS [Status]
-> ReadPrec Status
-> ReadPrec [Status]
-> Read Status
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Status]
$creadListPrec :: ReadPrec [Status]
readPrec :: ReadPrec Status
$creadPrec :: ReadPrec Status
readList :: ReadS [Status]
$creadList :: ReadS [Status]
readsPrec :: Int -> ReadS Status
$creadsPrec :: Int -> ReadS Status
Read, Status -> Status -> Bool
(Status -> Status -> Bool)
-> (Status -> Status -> Bool) -> Eq Status
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Status -> Status -> Bool
$c/= :: Status -> Status -> Bool
== :: Status -> Status -> Bool
$c== :: Status -> Status -> Bool
Eq)
-- Result perc watts time-seconds Status
data Result = Result Float Float Float Status | NA

data BattOpts = BattOpts
  { BattOpts -> String
onString :: String
  , BattOpts -> String
offString :: String
  , BattOpts -> String
idleString :: String
  , BattOpts -> Maybe String
posColor :: Maybe String
  , BattOpts -> Maybe String
lowWColor :: Maybe String
  , BattOpts -> Maybe String
mediumWColor :: Maybe String
  , BattOpts -> Maybe String
highWColor :: Maybe String
  , BattOpts -> Float
lowThreshold :: Float
  , BattOpts -> Float
highThreshold :: Float
  , BattOpts -> Maybe String
onLowAction :: Maybe String
  , BattOpts -> Float
actionThreshold :: Float
  , BattOpts -> String
onlineFile :: FilePath
  , BattOpts -> Float
scale :: Float
  , BattOpts -> Maybe IconPattern
onIconPattern :: Maybe IconPattern
  , BattOpts -> Maybe IconPattern
offIconPattern :: Maybe IconPattern
  , BattOpts -> Maybe IconPattern
idleIconPattern :: Maybe IconPattern
  , BattOpts -> String
lowString :: String
  , BattOpts -> String
mediumString :: String
  , BattOpts -> String
highString :: String
  , BattOpts -> Bool
incPerc :: Bool
  }

maybeAlert :: BattOpts -> Float -> IO ()
maybeAlert :: BattOpts -> Float -> IO ()
maybeAlert BattOpts
opts Float
left =
  case BattOpts -> Maybe String
onLowAction BattOpts
opts of
    Maybe String
Nothing -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    Just String
x -> Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Float -> Bool
forall a. RealFloat a => a -> Bool
isNaN Float
left Bool -> Bool -> Bool
|| BattOpts -> Float
actionThreshold BattOpts
opts Float -> Float -> Bool
forall a. Ord a => a -> a -> Bool
< Float
100 Float -> Float -> Float
forall a. Num a => a -> a -> a
* Float
left)
                (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ IO ExitCode -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO ExitCode -> IO ()) -> IO ExitCode -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> IO ExitCode
system String
x