module System.MemInfo.Choices (
Choices (..),
cmdInfo,
getChoices,
) where
import Options.Applicative (
Parser,
ParserInfo,
ReadM,
auto,
execParser,
help,
helper,
info,
long,
metavar,
option,
optional,
readerError,
short,
switch,
)
import Options.Applicative.NonEmpty (some1)
import System.MemInfo.Prelude
getChoices :: IO Choices
getChoices :: IO Choices
getChoices = ParserInfo Choices -> IO Choices
forall a. ParserInfo a -> IO a
execParser ParserInfo Choices
cmdInfo
data Choices = Choices
{ Choices -> Bool
choiceSplitArgs :: !Bool
, Choices -> Bool
choiceOnlyTotal :: !Bool
, Choices -> Bool
choiceByPid :: !Bool
, Choices -> Bool
choiceShowSwap :: !Bool
, Choices -> Maybe Natural
choiceWatchSecs :: !(Maybe Natural)
, Choices -> Maybe (NonEmpty ProcessID)
choicePidsToShow :: !(Maybe (NonEmpty ProcessID))
}
deriving (Choices -> Choices -> Bool
(Choices -> Choices -> Bool)
-> (Choices -> Choices -> Bool) -> Eq Choices
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Choices -> Choices -> Bool
== :: Choices -> Choices -> Bool
$c/= :: Choices -> Choices -> Bool
/= :: Choices -> Choices -> Bool
Eq, Int -> Choices -> ShowS
[Choices] -> ShowS
Choices -> String
(Int -> Choices -> ShowS)
-> (Choices -> String) -> ([Choices] -> ShowS) -> Show Choices
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Choices -> ShowS
showsPrec :: Int -> Choices -> ShowS
$cshow :: Choices -> String
show :: Choices -> String
$cshowList :: [Choices] -> ShowS
showList :: [Choices] -> ShowS
Show)
cmdInfo :: ParserInfo Choices
cmdInfo :: ParserInfo Choices
cmdInfo = Parser Choices -> InfoMod Choices -> ParserInfo Choices
forall a. Parser a -> InfoMod a -> ParserInfo a
info (Parser (Choices -> Choices)
forall a. Parser (a -> a)
helper Parser (Choices -> Choices) -> Parser Choices -> Parser Choices
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Choices
parseChoices) InfoMod Choices
forall a. Monoid a => a
mempty
parseChoices :: Parser Choices
parseChoices :: Parser Choices
parseChoices =
Bool
-> Bool
-> Bool
-> Bool
-> Maybe Natural
-> Maybe (NonEmpty ProcessID)
-> Choices
Choices
(Bool
-> Bool
-> Bool
-> Bool
-> Maybe Natural
-> Maybe (NonEmpty ProcessID)
-> Choices)
-> Parser Bool
-> Parser
(Bool
-> Bool
-> Bool
-> Maybe Natural
-> Maybe (NonEmpty ProcessID)
-> Choices)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Bool
parseSplitArgs
Parser
(Bool
-> Bool
-> Bool
-> Maybe Natural
-> Maybe (NonEmpty ProcessID)
-> Choices)
-> Parser Bool
-> Parser
(Bool
-> Bool -> Maybe Natural -> Maybe (NonEmpty ProcessID) -> Choices)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Bool
parseOnlyTotal
Parser
(Bool
-> Bool -> Maybe Natural -> Maybe (NonEmpty ProcessID) -> Choices)
-> Parser Bool
-> Parser
(Bool -> Maybe Natural -> Maybe (NonEmpty ProcessID) -> Choices)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Bool
parseDiscriminateByPid
Parser
(Bool -> Maybe Natural -> Maybe (NonEmpty ProcessID) -> Choices)
-> Parser Bool
-> Parser (Maybe Natural -> Maybe (NonEmpty ProcessID) -> Choices)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Bool
parseShowSwap
Parser (Maybe Natural -> Maybe (NonEmpty ProcessID) -> Choices)
-> Parser (Maybe Natural)
-> Parser (Maybe (NonEmpty ProcessID) -> Choices)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Natural -> Parser (Maybe Natural)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser Natural
parseWatchPeriodSecs
Parser (Maybe (NonEmpty ProcessID) -> Choices)
-> Parser (Maybe (NonEmpty ProcessID)) -> Parser Choices
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (NonEmpty ProcessID) -> Parser (Maybe (NonEmpty ProcessID))
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser (NonEmpty ProcessID)
parseChoicesPidsToShow
parseChoicesPidsToShow :: Parser (NonEmpty ProcessID)
parseChoicesPidsToShow :: Parser (NonEmpty ProcessID)
parseChoicesPidsToShow =
Parser ProcessID -> Parser (NonEmpty ProcessID)
forall a. Parser a -> Parser (NonEmpty a)
some1 (Parser ProcessID -> Parser (NonEmpty ProcessID))
-> Parser ProcessID -> Parser (NonEmpty ProcessID)
forall a b. (a -> b) -> a -> b
$
ReadM ProcessID -> Mod OptionFields ProcessID -> Parser ProcessID
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM ProcessID
forall a. (Read a, Ord a, Num a) => ReadM a
positiveNum (Mod OptionFields ProcessID -> Parser ProcessID)
-> Mod OptionFields ProcessID -> Parser ProcessID
forall a b. (a -> b) -> a -> b
$
Char -> Mod OptionFields ProcessID
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'p'
Mod OptionFields ProcessID
-> Mod OptionFields ProcessID -> Mod OptionFields ProcessID
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields ProcessID
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"pids"
Mod OptionFields ProcessID
-> Mod OptionFields ProcessID -> Mod OptionFields ProcessID
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields ProcessID
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"<pid1> [ -p pid2 ... -p pidN ]"
Mod OptionFields ProcessID
-> Mod OptionFields ProcessID -> Mod OptionFields ProcessID
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields ProcessID
forall (f :: * -> *) a. String -> Mod f a
help String
"Only show memory usage of the specified PIDs"
parseSplitArgs :: Parser Bool
parseSplitArgs :: Parser Bool
parseSplitArgs =
Mod FlagFields Bool -> Parser Bool
switch (Mod FlagFields Bool -> Parser Bool)
-> Mod FlagFields Bool -> Parser Bool
forall a b. (a -> b) -> a -> b
$
Char -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
's'
Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"split-args"
Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields Bool
forall (f :: * -> *) a. String -> Mod f a
help String
"Show and separate by all command line arguments"
parseOnlyTotal :: Parser Bool
parseOnlyTotal :: Parser Bool
parseOnlyTotal =
Mod FlagFields Bool -> Parser Bool
switch (Mod FlagFields Bool -> Parser Bool)
-> Mod FlagFields Bool -> Parser Bool
forall a b. (a -> b) -> a -> b
$
Char -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
't'
Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"total"
Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields Bool
forall (f :: * -> *) a. String -> Mod f a
help String
"Only show the total value"
parseDiscriminateByPid :: Parser Bool
parseDiscriminateByPid :: Parser Bool
parseDiscriminateByPid =
Mod FlagFields Bool -> Parser Bool
switch (Mod FlagFields Bool -> Parser Bool)
-> Mod FlagFields Bool -> Parser Bool
forall a b. (a -> b) -> a -> b
$
Char -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'd'
Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"discriminate-by-pid"
Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields Bool
forall (f :: * -> *) a. String -> Mod f a
help String
"Show by process rather than by program"
parseShowSwap :: Parser Bool
parseShowSwap :: Parser Bool
parseShowSwap =
Mod FlagFields Bool -> Parser Bool
switch (Mod FlagFields Bool -> Parser Bool)
-> Mod FlagFields Bool -> Parser Bool
forall a b. (a -> b) -> a -> b
$
Char -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'S'
Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"show_swap"
Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields Bool
forall (f :: * -> *) a. String -> Mod f a
help String
"Show swap information"
parseWatchPeriodSecs :: Parser Natural
parseWatchPeriodSecs :: Parser Natural
parseWatchPeriodSecs =
ReadM Natural -> Mod OptionFields Natural -> Parser Natural
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Natural
forall a. (Read a, Ord a, Num a) => ReadM a
positiveNum (Mod OptionFields Natural -> Parser Natural)
-> Mod OptionFields Natural -> Parser Natural
forall a b. (a -> b) -> a -> b
$
Char -> Mod OptionFields Natural
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'w'
Mod OptionFields Natural
-> Mod OptionFields Natural -> Mod OptionFields Natural
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields Natural
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"watch"
Mod OptionFields Natural
-> Mod OptionFields Natural -> Mod OptionFields Natural
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields Natural
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"N"
Mod OptionFields Natural
-> Mod OptionFields Natural -> Mod OptionFields Natural
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields Natural
forall (f :: * -> *) a. String -> Mod f a
help String
"Measure and show memory every N seconds (N > 0)"
positiveNum :: (Read a, Ord a, Num a) => ReadM a
positiveNum :: forall a. (Read a, Ord a, Num a) => ReadM a
positiveNum =
let
checkPositive :: a -> ReadM a
checkPositive a
i
| a
i a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0 = a -> ReadM a
forall a. a -> ReadM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
i
| Bool
otherwise = String -> ReadM a
forall a. String -> ReadM a
readerError String
"Value must be greater than 0"
in
ReadM a
forall a. Read a => ReadM a
auto ReadM a -> (a -> ReadM a) -> ReadM a
forall a b. ReadM a -> (a -> ReadM b) -> ReadM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> ReadM a
forall {a}. (Ord a, Num a) => a -> ReadM a
checkPositive