summaryrefslogtreecommitdiffhomepage
path: root/src/Plugins/Monitors/Wireless.hs
blob: c87bec63d87950710f926959b93ebaed0ffc8f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
-----------------------------------------------------------------------------
-- |
-- Module      :  Plugins.Monitors.Wireless
-- Copyright   :  (c) Jose Antonio Ortega Ruiz
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  Jose Antonio Ortega Ruiz
-- Stability   :  unstable
-- Portability :  unportable
--
-- A monitor reporting ESSID and link quality for wireless interfaces
--
-----------------------------------------------------------------------------

module Plugins.Monitors.Wireless (wirelessConfig, runWireless)  where

import System.Console.GetOpt

import Plugins.Monitors.Common
import IWlib

data WirelessOpts = WirelessOpts
  { qualityDynamicString :: Maybe DynamicString
  }

defaultOpts :: WirelessOpts
defaultOpts = WirelessOpts
  { qualityDynamicString = Nothing
  }

options :: [OptDescr (WirelessOpts -> WirelessOpts)]
options =
  [ Option "" ["quality-dynamic-string"] (ReqArg (\d opts ->
     opts { qualityDynamicString = Just $ parseDynamicString d }) "") ""
  ]

parseOpts :: [String] -> IO WirelessOpts
parseOpts argv =
  case getOpt Permute options argv of
       (o, _, []) -> return $ foldr id defaultOpts o
       (_, _, errs) -> ioError . userError $ concat errs

wirelessConfig :: IO MConfig
wirelessConfig =
  mkMConfig "<essid> <quality>" ["essid", "quality", "qualitybar", "qualityvbar", "qualitydstr"]

runWireless :: String -> [String] -> Monitor String
runWireless iface args = do
  opts <- io $ parseOpts args
  wi <- io $ getWirelessInfo iface
  na <- getConfigValue naString
  let essid = wiEssid wi
      qlty = fromIntegral $ wiQuality wi
      e = if essid == "" then na else essid
  ep <- showWithPadding e
  q <- if qlty >= 0
       then showPercentWithColors (qlty / 100)
       else showWithPadding ""
  qb <- showPercentBar qlty (qlty / 100)
  qvb <- showVerticalBar qlty (qlty / 100)
  qdstr <- showDynamicString (qualityDynamicString opts) (qlty / 100)
  parseTemplate [ep, q, qb, qvb, qdstr]