From eb9f19c593c25eb4ad2a458f00ef486189818ac9 Mon Sep 17 00:00:00 2001 From: Jose Antonio Ortega Ruiz Date: Sun, 19 Dec 2010 20:52:50 +0100 Subject: Non-throwing parsing of ints and floats --- Plugins/Monitors/Common.hs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'Plugins/Monitors/Common.hs') diff --git a/Plugins/Monitors/Common.hs b/Plugins/Monitors/Common.hs index e177e19..cc1a6a7 100644 --- a/Plugins/Monitors/Common.hs +++ b/Plugins/Monitors/Common.hs @@ -47,6 +47,8 @@ module Plugins.Monitors.Common ( , takeDigits , showDigits , floatToPercent + , parseFloat + , parseInt , stringParser -- * Threaded Actions -- $thread @@ -335,6 +337,16 @@ padString mnw mxw pad pr s = else let ps = take (rlen - len) (cycle pad) in if pr then s ++ ps else ps ++ s +parseFloat :: String -> Float +parseFloat s = case readFloat s of + (v, _):_ -> v + _ -> 0 + +parseInt :: String -> Int +parseInt s = case readDec s of + (v, _):_ -> v + _ -> 0 + floatToPercent :: Float -> Monitor String floatToPercent n = do pad <- getConfigValue ppad -- cgit v1.2.3