-
Notifications
You must be signed in to change notification settings - Fork 2
/
Prompt.hs
108 lines (94 loc) · 3.22 KB
/
Prompt.hs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
module Prompt where
import Data.List (isInfixOf)
import System.Environment (lookupEnv)
import System.IO.Unsafe (unsafePerformIO)
import XMonad.Prompt
import qualified Data.Map as M
import qualified XMonad.Prompt.Shell as Shell
import Imports
import Misc
data GenericPrompt = GenericPrompt String
instance XPrompt GenericPrompt where
showXPrompt (GenericPrompt x) = x
shellPrompt :: XX ()
shellPrompt = do
(cmds, xpConfig) <- runXio $
io Shell.getCommands `concurrently` getXpConfig
let completion = Shell.getShellCompl cmds $ searchPredicate xpConfig
env <- ask
toXX $ mkXPrompt Shell.Shell xpConfig completion $ \input ->
withEnv env $ spawn "sh" ["-c", input]
data ActionsPrompt = ActionsPrompt
instance XPrompt ActionsPrompt where
showXPrompt ActionsPrompt = "M-x "
actionPrompt :: M.Map String (XX ()) -> XX ()
actionPrompt actions = do
xpConfig <- getXpConfig
let completion = mkComplFunFromList' xpConfig (M.keys actions)
env <- ask
toXX $ mkXPrompt ActionsPrompt xpConfig completion $ \input ->
withEnv env $
case M.lookup input actions of
Nothing -> forkXio $ notify $ "No action matching " <> input
Just action -> do
logDebug $ "Running action " <> fromString input
action
logDebug $ "Finished running action " <> fromString input
plainPrompt :: String -> (String -> XX ()) -> XX ()
plainPrompt prompt handleResult = do
xpConfig <- getXpConfig
env <- ask
toXX $ mkXPrompt (GenericPrompt prompt) xpConfig completions $ withEnv env . handleResult
where
completions _ = return []
data ColorScheme = Light | Dark
getColorScheme :: Xio ColorScheme
getColorScheme = do
result <- timeout (100 * 1000) $ syncSpawnAndRead
"gsettings"
[ "get"
, "org.gnome.desktop.interface"
, "color-scheme"
]
return $ case result of
Just value | "prefer-light" `isInfixOf` value -> Light
_ -> Dark
getXpConfig :: (MonadIO m, MonadReader Env m) => m XPConfig
getXpConfig = mkXpConfig <$> runXio getColorScheme
mkXpConfig :: ColorScheme -> XPConfig
mkXpConfig colorScheme = setColors $ def
{ font = if isHiDpi
then "xft:Hack:pixelsize=18"
else "xft:Hack:pixelsize=10"
, promptBorderWidth = 1
, position = Bottom
, height = if isHiDpi then 32 else 18
, historySize = 1000
, promptKeymap = km
}
where
setColors x = case colorScheme of
Light -> x
{ bgColor = "white"
, fgColor = "black"
, bgHLight = "yellow"
, fgHLight = "black"
, borderColor = "orange"
}
Dark -> x
{ bgColor = "black"
, fgColor = "white"
, bgHLight = "gray"
, fgHLight = "black"
, borderColor = "orange"
}
km =
M.insert (controlMask, xK_Right) (moveWord Next) $
M.insert (controlMask, xK_Left) (moveWord Prev) $
-- C-y makes no sense for paste??
M.delete (controlMask, xK_y) $
M.insert (controlMask, xK_v) pasteString $
M.insert (mod4Mask, xK_v) pasteString $
emacsLikeXPKeymap
isHiDpi :: Bool
isHiDpi = isJust $ unsafePerformIO $ lookupEnv "HIDPI"