make seperate Rofi module to abstract away rofi stuff in xmonad config

This commit is contained in:
Leon Kowarschick 2020-03-25 15:25:15 +01:00
parent 2d301c621e
commit f81f1238c6
7 changed files with 93 additions and 21 deletions

View file

@ -1,9 +1,10 @@
{-# Language ScopedTypeVariables #-} {-# Language ScopedTypeVariables, LambdaCase #-}
{-# OPTIONS_GHC -fno-warn-missing-signatures -fno-warn-unused-binds #-} {-# OPTIONS_GHC -fno-warn-missing-signatures -fno-warn-unused-binds #-}
-- Imports -------------------------------------------------------- {{{ -- Imports -------------------------------------------------------- {{{
module Config (main) where module Config (main) where
import qualified Rofi as Rofi
import Data.List (isSuffixOf, isPrefixOf) import Data.List (isSuffixOf, isPrefixOf, elem)
import Data.Char (isDigit) import Data.Char (isDigit)
import System.Exit (exitSuccess) import System.Exit (exitSuccess)
@ -50,12 +51,14 @@ import XMonad.Layout.Minimize
import qualified XMonad.Layout.BoringWindows as BoringWindows import qualified XMonad.Layout.BoringWindows as BoringWindows
import XMonad.Hooks.Minimize import XMonad.Hooks.Minimize
import XMonad.Actions.Minimize import XMonad.Actions.Minimize
import XMonad.Actions.WindowBringer
import XMonad.Actions.Commands
-- }}} -- }}}
-- Values -------------------- {{{ -- Values -------------------- {{{
myModMask = mod4Mask myModMask = mod4Mask
myLauncher = "rofi -show run -theme /home/leon/scripts/rofi-scripts/launcher_grid_full_style.rasi" myLauncher = Rofi.asCommand def ["-show run"] -- "rofi -show run -theme /home/leon/scripts/rofi-scripts/launcher_grid_full_style.rasi"
myTerminal = "kitty --single-instance" myTerminal = "kitty --single-instance"
myBrowser = "google-chrome-stable" myBrowser = "google-chrome-stable"
--yBar = "xmobar" --yBar = "xmobar"
@ -162,14 +165,17 @@ myKeys = [ ("M-C-k", sendMessage MirrorExpand >> sendMessage ShrinkSlave )
-- programs -- programs
, ("M-p", spawn myLauncher) , ("M-p", spawn myLauncher)
, ("M-S-p", spawn "rofi -combi-modi drun,window,ssh -show combi -theme /home/leon/scripts/rofi-scripts/launcher_grid_full_style.rasi")
, ("M-S-e", spawn "rofi -show emoji -modi emoji -theme /home/leon/scripts/rofi-scripts/launcher_grid_full_style.rasi")
, ("M-b", spawn myBrowser) , ("M-b", spawn myBrowser)
, ("M-S-p", Rofi.showCombi def [ "drun", "window", "ssh" ])
, ("M-S-e", Rofi.showNormal def "emoji" )
, ("M-s", spawn $ scriptFile "rofi-search.sh") , ("M-s", spawn $ scriptFile "rofi-search.sh")
, ("M-S-s", spawn $ "cat " ++ scriptFile "bookmarks" ++ " | rofi -p open -dmenu | bash") , ("M-S-s", spawn $ "cat " ++ scriptFile "bookmarks"
, ("M-n", scratchpadSubmap) ++ " | " ++ Rofi.asCommand def ["-dmenu", "-p open"]
, ("M-m", mediaSubmap) ++ " | bash")
, ("M-e", promptExecute specialCommands) , ("M-n", scratchpadSubmap )
, ("M-m", mediaSubmap )
, ("M-e", Rofi.promptRunCommand specialCommands)
, ("M-C-e", Rofi.promptRunCommand =<< defaultCommands )
-- BSP -- BSP
@ -190,12 +196,24 @@ myKeys = [ ("M-C-k", sendMessage MirrorExpand >> sendMessage ShrinkSlave )
, ("M-j", BoringWindows.focusDown) , ("M-j", BoringWindows.focusDown)
, ("M-ü", withFocused minimizeWindow) , ("M-ü", withFocused minimizeWindow)
, ("M-S-ü", withLastMinimized maximizeWindow) , ("M-S-ü", withLastMinimized maximizeWindow)
, ("M-C-ü", promptRestoreWindow)
, ("M1-<Tab>", cycleMinimizedWindow)
] ++ copyToWorkspaceMappings ] ++ copyToWorkspaceMappings
where where
copyToWorkspaceMappings :: [(String, X())] copyToWorkspaceMappings :: [(String, X())]
copyToWorkspaceMappings = [("M-C-" ++ wsp, windows $ copy wsp) | wsp <- map show [1..9]] copyToWorkspaceMappings = [("M-C-" ++ wsp, windows $ copy wsp) | wsp <- map show [1..9]]
cycleMinimizedWindow :: X ()
cycleMinimizedWindow = withLastMinimized $ \window -> do
withFocused minimizeWindow
maximizeWindowAndFocus window
promptRestoreWindow = do
wm <- windowMap
shownWindows <- withMinimized (\minimizedWindows -> pure $ M.filter (`elem` minimizedWindows) wm)
w <- Rofi.promptSimple def (M.keys shownWindows)
whenJust (M.lookup w wm) (\w -> maximizeWindow w >> (windows $ bringWindow w))
toggleFullscreen :: X () toggleFullscreen :: X ()
toggleFullscreen = do toggleFullscreen = do
sendMessage ToggleLayout -- toggle fullscreen layout sendMessage ToggleLayout -- toggle fullscreen layout
@ -203,6 +221,7 @@ myKeys = [ ("M-C-k", sendMessage MirrorExpand >> sendMessage ShrinkSlave )
--sendMessage ToggleGaps -- show a small gap around the window --sendMessage ToggleGaps -- show a small gap around the window
safeSpawn "polybar-msg" ["cmd", "toggle"] -- toggle polybar visibility safeSpawn "polybar-msg" ["cmd", "toggle"] -- toggle polybar visibility
scratchpadSubmap :: X () scratchpadSubmap :: X ()
scratchpadSubmap = describedSubmap "Scratchpads" scratchpadSubmap = describedSubmap "Scratchpads"
[ ((myModMask, xK_n), "<M-n> terminal", namedScratchpadAction scratchpads "terminal") [ ((myModMask, xK_n), "<M-n> terminal", namedScratchpadAction scratchpads "terminal")
@ -236,13 +255,6 @@ myKeys = [ ("M-C-k", sendMessage MirrorExpand >> sendMessage ShrinkSlave )
mySubmap = submap $ M.fromList $ map (\(k, _, f) -> (k, f)) mappings mySubmap = submap $ M.fromList $ map (\(k, _, f) -> (k, f)) mappings
descriptions = map (\(_,x,_) -> x) mappings descriptions = map (\(_,x,_) -> x) mappings
promptExecute :: [(String, X ())] -> X ()
promptExecute commands = do
selection <- Dmenu.menuMapArgs "rofi" ["-dmenu", "-i"] $ M.fromList commands -- -i -> case-insensitive
Maybe.fromMaybe (return ()) selection
-- }}} -- }}}
-- ManageHook -------------------------------{{{ -- ManageHook -------------------------------{{{

50
files/.xmonad/lib/Rofi.hs Normal file
View file

@ -0,0 +1,50 @@
{-# LANGUAGE RecordWildCards, OverloadedStrings #-}
module Rofi (asCommand, promptSimple, promptMap, promptRunCommand, showNormal, showCombi) where
import Data.List ( intercalate )
import qualified Data.Map as M
import XMonad
import qualified XMonad.Util.Dmenu as Dmenu
import qualified XMonad.Actions.Commands as XCommands
rofiCmd :: String
rofiCmd = "rofi"
data RofiConfig
= RofiConfig { theme :: String, caseInsensitive :: Bool } deriving (Show, Eq)
instance Default RofiConfig where
def = RofiConfig
{ theme = "/home/leon/scripts/rofi-scripts/launcher_grid_full_style.rasi"
, caseInsensitive = True
}
toArgList :: RofiConfig -> [String]
toArgList RofiConfig {..} =
["-theme " ++ theme, if caseInsensitive then "-i" else ""]
-- |given an array of arguments, generate a string that would call rofi with the configuration and arguments
asCommand :: RofiConfig -> [String] -> String
asCommand config args = unwords $ rofiCmd : toArgList config ++ args
-- |Let the user choose an element of a list
promptSimple :: MonadIO m => RofiConfig -> [String] -> m String
promptSimple config = Dmenu.menuArgs rofiCmd ("-dmenu" : toArgList config)
-- |Let the user choose an entry of a map by key. return's the chosen value.
promptMap :: MonadIO m => RofiConfig -> M.Map String a -> m (Maybe a)
promptMap config = Dmenu.menuMapArgs rofiCmd ("-dmenu" : toArgList config)
-- |Display a list of commands, of which the chosen one will be executed. See `Xmonad.Actions.Commands.runCommandConfig`
promptRunCommand :: [(String, X ())] -> X ()
promptRunCommand = XCommands.runCommandConfig $ Rofi.promptSimple def
-- |prompt a single rofi mode. ex: `showNormal def "run"`
showNormal :: RofiConfig -> String -> X ()
showNormal config mode =
spawn $ asCommand config ["-modi " ++ mode, "-show " ++ mode]
-- |Show a rofi combi prompt, combining all given modes
showCombi :: RofiConfig -> [String] -> X ()
showCombi config modi = spawn
$ asCommand config ["-show combi", "-combi-modi " ++ intercalate "," modi]

View file

@ -7,6 +7,7 @@ executable my-xmonad
main-is: ../xmonad.hs main-is: ../xmonad.hs
-- other-modules lists custom modules in my ~/.xmonad/lib/ directory -- other-modules lists custom modules in my ~/.xmonad/lib/ directory
other-modules: Config other-modules: Config
, Rofi
build-depends: base build-depends: base
, xmonad >= 0.13 , xmonad >= 0.13
, xmonad-contrib >= 0.13 , xmonad-contrib >= 0.13
@ -15,6 +16,7 @@ executable my-xmonad
, netlink >= 1.1.1.0 , netlink >= 1.1.1.0
, containers >= 0.6.2.1 , containers >= 0.6.2.1
, utf8-string >= 1.0.1.1 , utf8-string >= 1.0.1.1
, text >= 1.2.4.0
hs-source-dirs: lib hs-source-dirs: lib
default-language: Haskell2010 default-language: Haskell2010
ghc-options: -Wall -threaded -fno-warn-missing-signatures ghc-options: -Wall -threaded -fno-warn-missing-signatures

View file

@ -43,6 +43,7 @@ extra-deps:
- iwlib-0.1.0 - iwlib-0.1.0
- netlink-1.1.1.0 - netlink-1.1.1.0
- dbus-1.2.12 - dbus-1.2.12
- text-1.2.4.0
# Override default flag values for local packages and extra-deps # Override default flag values for local packages and extra-deps
# flags: {} # flags: {}

View file

@ -25,6 +25,13 @@ packages:
sha256: 4dabb3bf0b86adc529116f22cb552ebc68e05f41017bd266797694129716192f sha256: 4dabb3bf0b86adc529116f22cb552ebc68e05f41017bd266797694129716192f
original: original:
hackage: dbus-1.2.12 hackage: dbus-1.2.12
- completed:
hackage: text-1.2.4.0@sha256:8c24450feb8e3bbb7ea3e17af24ef57e85db077c4bf53e5bcc345b283d1b1d5b,10081
pantry-tree:
size: 7457
sha256: 3437b0a73ce2ae1a81aa8b3438d41a85981c00894cdbee0d6d6d6873046a5d5d
original:
hackage: text-1.2.4.0
snapshots: snapshots:
- completed: - completed:
size: 491373 size: 491373

Binary file not shown.