-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prompt.hs
153 lines (128 loc) · 4.45 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
module Prompt
( promptMode
, promptBind
, completerSet
) where
import Data.Char
import Data.List
import qualified Data.Map as Map
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import Safe
import Util
import Config
import Keys
import Uzbl
historyLength :: Int
historyLength = 255
input :: Input -> String
input (il, ir) = reverse il ++ ir
unInput :: String -> Input
unInput s = (reverse s, "")
promptStop :: UzblM ()
promptStop = do
p <- gets uzblBindings
promptExec p Nothing
promptRun :: UzblM ()
promptRun = do
u@UzblState{ uzblBindings = p } <- get
let s = input (promptInput p)
put $ historyPush s u
promptExec p $ Just s
modifyPrompt :: (Bindings -> Bindings) -> UzblM ()
modifyPrompt = modifyBindings
modifyInput :: ((String,String) -> (String,String)) -> UzblM ()
modifyInput f = modifyPrompt $ \p -> p{ promptInput = f $ promptInput p }
promptInsert :: String -> UzblM ()
promptInsert s = modifyInput $ first (reverse s++)
pasteInput :: UzblM ()
pasteInput = io paste >>= maybe nop promptInsert
inputLeft :: Input -> Input
inputLeft (c:il,ir) = (il,c:ir)
inputLeft i = i
inputRight :: Input -> Input
inputRight (il,c:ir) = (c:il,ir)
inputRight i = i
historyPush :: String -> UzblState -> UzblState
historyPush x u = u{ uzblPromptHistory =
x Seq.<| Seq.take historyLength (uzblPromptHistory u) }
historyUp :: UzblState -> UzblState
historyUp u@UzblState{ uzblBindings = p }
| n Seq.:< r <- Seq.viewl (uzblPromptHistory u) =
u{ uzblBindings = p{ promptInput = unInput n }
, uzblPromptHistory = if null i then r else r Seq.|> i
} where i = input (promptInput p)
historyUp u = u
historyDown :: UzblState -> UzblState
historyDown u@UzblState{ uzblBindings = p }
| r Seq.:> n <- Seq.viewr (uzblPromptHistory u) =
u{ uzblBindings = p{ promptInput = unInput n }
, uzblPromptHistory = if null i then r else i Seq.<| r
} where i = input (promptInput p)
historyDown u = u
historyFind :: Bool -> UzblState -> UzblState
historyFind dir u@UzblState
{ uzblBindings = p@Prompt{ promptInput = (il,_) }
, uzblPromptHistory = h }
| Just n <- (if dir then Seq.findIndexL else Seq.findIndexR) (reverse il `isPrefixOf`) h
, (l,i Seq.:< r) <- second Seq.viewl $ Seq.splitAt n h =
u{ uzblBindings = p{ promptInput = (il,drop (length il) i) }
, uzblPromptHistory = r Seq.>< l
}
historyFind _ u = u
complete :: UzblM ()
complete = do
u@UzblState{ uzblBindings = p } <- get
maybe nop (\c ->
put u{ uzblBindings = p{ promptInput = unInput c } })
=<< promptCompleter p (input (promptInput p))
promptBinds :: Map.Map ModKey (UzblM ())
promptBinds = Map.fromAscList
[ ((0, "BackSpace"), modifyInput $ first tailSafe)
, ((0, "Delete"), modifyInput $ second tailSafe)
, ((0, "Down"), modify historyDown)
, ((0, "End"), modifyInput $ \(a,b) -> (input (b,a),""))
, ((0, "Escape"), promptStop)
, ((0, "Home"), modifyInput $ (,) "" . input)
, ((0, "Left"), modifyInput inputLeft)
, ((0, "Return"), promptRun)
, ((0, "Right"), modifyInput inputRight)
, ((0, "Tab"), complete)
, ((0, "Up"), modify historyUp)
, ((0, "space"), promptInsert " ")
, ((modShift, "Insert"), pasteInput)
, ((modCtrl, "n"), modify $ historyFind True)
, ((modCtrl, "t"), modify $ historyFind False)
, ((modCtrl, "u"), modifyInput $ const ("",""))
, ((modCtrl, "w"), modifyInput $ \(il,ir) -> (tailSafe $ dropWhile (not . isSpace) il,ir))
]
setPrompt :: String -> UzblM ()
setPrompt = setVar "status_message" . ValStr
promptUpdate :: UzblM ()
promptUpdate = pu =<< gets uzblBindings where
pu Prompt{ promptPrompt = p, promptInput = (il,ir) } =
setPrompt $ "<span bgcolor='#000' color='#CCC'>" ++ p ++ "<span face='monospace'>"
++ mlEscape (reverse il) ++ "<span face='sans'>|</span>" ++ mlEscape ir
++ "</span></span>"
pu _ = setPrompt ""
promptBind :: ModKey -> UzblM ()
promptBind mk = do
bindMap promptBinds (promptInsert . snd) mk
promptUpdate
promptMode :: String -> String -> Completer -> (Maybe String -> UzblM ()) -> UzblM ()
promptMode p i c e = do
modifyBindings $ const Prompt
{ promptPrompt = p
, promptInput = unInput i
, promptCompleter = c
, promptExec = e
}
promptUpdate
completerSet :: Set.Set String -> String -> Maybe String
completerSet s x
| e = Just x
| Just (y,_) <- Set.minView r
, x `isPrefixOf` y = Just y
| otherwise = Nothing
where
(_,e,r) = Set.splitMember x s