{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE GADTs #-}
module System.MemInfo.Choices (
Choices (..),
PrintOrder (..),
cmdInfo,
getChoices,
) where
import GHC.Generics (Generic)
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 -> Bool
choiceReversed :: !Bool
, Choices -> Maybe Natural
choiceWatchSecs :: !(Maybe Natural)
, Choices -> Maybe (NonEmpty ProcessID)
choicePidsToShow :: !(Maybe (NonEmpty ProcessID))
, Choices -> Maybe PrintOrder
choicePrintOrder :: !(Maybe PrintOrder)
}
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, (forall x. Choices -> Rep Choices x)
-> (forall x. Rep Choices x -> Choices) -> Generic Choices
forall x. Rep Choices x -> Choices
forall x. Choices -> Rep Choices x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Choices -> Rep Choices x
from :: forall x. Choices -> Rep Choices x
$cto :: forall x. Rep Choices x -> Choices
to :: forall x. Rep Choices x -> Choices
Generic)
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
-> Bool
-> Maybe Natural
-> Maybe (NonEmpty ProcessID)
-> Maybe PrintOrder
-> Choices
Choices
(Bool
-> Bool
-> Bool
-> Bool
-> Bool
-> Maybe Natural
-> Maybe (NonEmpty ProcessID)
-> Maybe PrintOrder
-> Choices)
-> Parser Bool
-> Parser
(Bool
-> Bool
-> Bool
-> Bool
-> Maybe Natural
-> Maybe (NonEmpty ProcessID)
-> Maybe PrintOrder
-> Choices)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Bool
parseSplitArgs
Parser
(Bool
-> Bool
-> Bool
-> Bool
-> Maybe Natural
-> Maybe (NonEmpty ProcessID)
-> Maybe PrintOrder
-> Choices)
-> Parser Bool
-> Parser
(Bool
-> Bool
-> Bool
-> Maybe Natural
-> Maybe (NonEmpty ProcessID)
-> Maybe PrintOrder
-> 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
-> Bool
-> Maybe Natural
-> Maybe (NonEmpty ProcessID)
-> Maybe PrintOrder
-> Choices)
-> Parser Bool
-> Parser
(Bool
-> Bool
-> Maybe Natural
-> Maybe (NonEmpty ProcessID)
-> Maybe PrintOrder
-> 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
-> Bool
-> Maybe Natural
-> Maybe (NonEmpty ProcessID)
-> Maybe PrintOrder
-> Choices)
-> Parser Bool
-> Parser
(Bool
-> Maybe Natural
-> Maybe (NonEmpty ProcessID)
-> Maybe PrintOrder
-> 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
(Bool
-> Maybe Natural
-> Maybe (NonEmpty ProcessID)
-> Maybe PrintOrder
-> Choices)
-> Parser Bool
-> Parser
(Maybe Natural
-> Maybe (NonEmpty ProcessID) -> Maybe PrintOrder -> 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
parseReversed
Parser
(Maybe Natural
-> Maybe (NonEmpty ProcessID) -> Maybe PrintOrder -> Choices)
-> Parser (Maybe Natural)
-> Parser
(Maybe (NonEmpty ProcessID) -> Maybe PrintOrder -> 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) -> Maybe PrintOrder -> Choices)
-> Parser (Maybe (NonEmpty ProcessID))
-> Parser (Maybe PrintOrder -> 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
Parser (Maybe PrintOrder -> Choices)
-> Parser (Maybe PrintOrder) -> 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 PrintOrder -> Parser (Maybe PrintOrder)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser PrintOrder
parsePrintOrder
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"
parseReversed :: Parser Bool
parseReversed :: Parser Bool
parseReversed =
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
'r'
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
"reverse"
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
"Reverses the output order so that output descends on the sorting field"
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
parsePrintOrder :: Parser PrintOrder
parsePrintOrder :: Parser PrintOrder
parsePrintOrder =
ReadM PrintOrder
-> Mod OptionFields PrintOrder -> Parser PrintOrder
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM PrintOrder
forall a. Read a => ReadM a
auto
(Mod OptionFields PrintOrder -> Parser PrintOrder)
-> Mod OptionFields PrintOrder -> Parser PrintOrder
forall a b. (a -> b) -> a -> b
$ Char -> Mod OptionFields PrintOrder
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'b'
Mod OptionFields PrintOrder
-> Mod OptionFields PrintOrder -> Mod OptionFields PrintOrder
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields PrintOrder
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"order-by"
Mod OptionFields PrintOrder
-> Mod OptionFields PrintOrder -> Mod OptionFields PrintOrder
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields PrintOrder
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"<Private | Swap | Shared | Count>"
Mod OptionFields PrintOrder
-> Mod OptionFields PrintOrder -> Mod OptionFields PrintOrder
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields PrintOrder
forall (f :: * -> *) a. String -> Mod f a
help String
"Orders the output by ascending values of the given field"
data PrintOrder
= Swap
| Private
| Shared
| Count
deriving (PrintOrder -> PrintOrder -> Bool
(PrintOrder -> PrintOrder -> Bool)
-> (PrintOrder -> PrintOrder -> Bool) -> Eq PrintOrder
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PrintOrder -> PrintOrder -> Bool
== :: PrintOrder -> PrintOrder -> Bool
$c/= :: PrintOrder -> PrintOrder -> Bool
/= :: PrintOrder -> PrintOrder -> Bool
Eq, Int -> PrintOrder -> ShowS
[PrintOrder] -> ShowS
PrintOrder -> String
(Int -> PrintOrder -> ShowS)
-> (PrintOrder -> String)
-> ([PrintOrder] -> ShowS)
-> Show PrintOrder
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PrintOrder -> ShowS
showsPrec :: Int -> PrintOrder -> ShowS
$cshow :: PrintOrder -> String
show :: PrintOrder -> String
$cshowList :: [PrintOrder] -> ShowS
showList :: [PrintOrder] -> ShowS
Show, ReadPrec [PrintOrder]
ReadPrec PrintOrder
Int -> ReadS PrintOrder
ReadS [PrintOrder]
(Int -> ReadS PrintOrder)
-> ReadS [PrintOrder]
-> ReadPrec PrintOrder
-> ReadPrec [PrintOrder]
-> Read PrintOrder
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS PrintOrder
readsPrec :: Int -> ReadS PrintOrder
$creadList :: ReadS [PrintOrder]
readList :: ReadS [PrintOrder]
$creadPrec :: ReadPrec PrintOrder
readPrec :: ReadPrec PrintOrder
$creadListPrec :: ReadPrec [PrintOrder]
readListPrec :: ReadPrec [PrintOrder]
Read, (forall x. PrintOrder -> Rep PrintOrder x)
-> (forall x. Rep PrintOrder x -> PrintOrder) -> Generic PrintOrder
forall x. Rep PrintOrder x -> PrintOrder
forall x. PrintOrder -> Rep PrintOrder x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PrintOrder -> Rep PrintOrder x
from :: forall x. PrintOrder -> Rep PrintOrder x
$cto :: forall x. Rep PrintOrder x -> PrintOrder
to :: forall x. Rep PrintOrder x -> PrintOrder
Generic)