Xmonad/Config archive/Kapil's networkmanagerprompt.hs

From HaskellWiki
Jump to: navigation, search

A snippet to use "nmcli" to bring up and take down connections via a XPrompt interaction.

import XMonad
import XMonad.Config
import XMonad.Prompt
import XMonad.Prompt.Input (inputPromptWithCompl, (?+))
import XMonad.Util.Run (runProcessWithInput)

import Data.Bits ((.|.))
import qualified Data.Map as M
 
main = xmonad $ defaultConfig {
     keys         = addlKeys <+> keys defaultConfig
   }

-- networking with network Manager

addlKeys conf@(XConfig {modMask = modm}) = M.fromList $
    [ ((modm                , xK_F3), nmPrompt "up")
    , ((modm .|. controlMask, xK_F3), nmPrompt "down") ]

-- A set up to use XPrompt to find out which connections need to go up and down

-- get the list of connections using nmcli
getNmConnections :: X [String]
getNmConnections = fmap lines $ runProcessWithInput "nmcli" [ "-t", "-f", "NAME", "con", "list" ] []

-- a utility to run an nmcli command

nmConAction a = spawn . ((++) "nmcli con ") . ((++) a)

-- Prompt for nm up or down action

nmPrompt :: String -> X ()
nmPrompt a = do 
                conns <- getNmConnections
                inputPromptWithCompl defaultXPConfig ("Connection to " ++ a)
                  (mkComplFunFromList conns) ?+ (nmConAction (" " ++ a ++ " id "))