summary refs log blame commit diff stats
path: root/doc/pydoc/ranger.ext.command_parser.html
blob: 9e6fc95094aa4aa26c1fcdf370629dc2b39f20f6 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
 

                                                                   






                                                                                                                                                                                                                                                     
                                                                                                                                                                                                               
                                                                                                                              
     



                                                                                                                                           
     






                                                                                                                                                                              







                                                                                                  
                                                                                          









                                                                                                    
                                                                                                                                                                                   






















                                                                                                                                                                                                                                         
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: module ranger.ext.command_parser</title>
</head><body bgcolor="#f0f0f8">

<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#7799ee">
<td valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong><a href="ranger.html"><font color="#ffffff">ranger</font></a>.<a href="ranger.ext.html"><font color="#ffffff">ext</font></a>.command_parser</strong></big></big></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/hut/ranger/ranger/ext/command_parser.py">/home/hut/ranger/ranger/ext/command_parser.py</a></font></td></tr></table>
    <p><tt>#&nbsp;Copyright&nbsp;(C)&nbsp;2009,&nbsp;2010&nbsp;&nbsp;Roman&nbsp;Zimbelmann&nbsp;&lt;romanz@lavabit.com&gt;<br>
#<br>
#&nbsp;This&nbsp;program&nbsp;is&nbsp;free&nbsp;software:&nbsp;you&nbsp;can&nbsp;redistribute&nbsp;it&nbsp;and/or&nbsp;modify<br>
#&nbsp;it&nbsp;under&nbsp;the&nbsp;terms&nbsp;of&nbsp;the&nbsp;GNU&nbsp;General&nbsp;Public&nbsp;License&nbsp;as&nbsp;published&nbsp;by<br>
#&nbsp;the&nbsp;Free&nbsp;Software&nbsp;Foundation,&nbsp;either&nbsp;version&nbsp;3&nbsp;of&nbsp;the&nbsp;License,&nbsp;or<br>
#&nbsp;(at&nbsp;your&nbsp;option)&nbsp;any&nbsp;later&nbsp;version.<br>
#<br>
#&nbsp;This&nbsp;program&nbsp;is&nbsp;distributed&nbsp;in&nbsp;the&nbsp;hope&nbsp;that&nbsp;it&nbsp;will&nbsp;be&nbsp;useful,<br>
#&nbsp;but&nbsp;WITHOUT&nbsp;ANY&nbsp;WARRANTY;&nbsp;without&nbsp;even&nbsp;the&nbsp;implied&nbsp;warranty&nbsp;of<br>
#&nbsp;MERCHANTABILITY&nbsp;or&nbsp;FITNESS&nbsp;FOR&nbsp;A&nbsp;PARTICULAR&nbsp;PURPOSE.&nbsp;&nbsp;See&nbsp;the<br>
#&nbsp;GNU&nbsp;General&nbsp;Public&nbsp;License&nbsp;for&nbsp;more&nbsp;details.<br>
#<br>
#&nbsp;You&nbsp;should&nbsp;have&nbsp;received&nbsp;a&nbsp;copy&nbsp;of&nbsp;the&nbsp;GNU&nbsp;General&nbsp;Public&nbsp;License<br>
#&nbsp;along&nbsp;with&nbsp;this&nbsp;program.&nbsp;&nbsp;If&nbsp;not,&nbsp;see&nbsp;&lt;<a href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>&gt;.</tt></p>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ee77aa">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
    
<tr><td bgcolor="#ee77aa"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><dl>
<dt><font face="helvetica, arial"><a href="__builtin__.html#object">__builtin__.object</a>
</font></dt><dd>
<dl>
<dt><font face="helvetica, arial"><a href="ranger.ext.command_parser.html#LazyParser">LazyParser</a>
</font></dt></dl>
</dd>
</dl>
 <p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#000000" face="helvetica, arial"><a name="LazyParser">class <strong>LazyParser</strong></a>(<a href="__builtin__.html#object">__builtin__.object</a>)</font></td></tr>
    
<tr bgcolor="#ffc8d8"><td rowspan=2><tt>&nbsp;&nbsp;&nbsp;</tt></td>
<td colspan=2><tt>Parse&nbsp;commands&nbsp;and&nbsp;extract&nbsp;information<br>&nbsp;</tt></td></tr>
<tr><td>&nbsp;</td>
<td width="100%">Methods defined here:<br>
<dl><dt><a name="LazyParser-__add__"><strong>__add__</strong></a>(self, newpart)</dt></dl>

<dl><dt><a name="LazyParser-__init__"><strong>__init__</strong></a>(self, line)</dt></dl>

<dl><dt><a name="LazyParser-chunk"><strong>chunk</strong></a>(self, n, otherwise<font color="#909090">=''</font>)</dt><dd><tt>Chunks&nbsp;are&nbsp;pieces&nbsp;of&nbsp;the&nbsp;command&nbsp;seperated&nbsp;by&nbsp;spaces</tt></dd></dl>

<dl><dt><a name="LazyParser-rest"><strong>rest</strong></a>(self, n, otherwise<font color="#909090">=''</font>)</dt><dd><tt>Rests&nbsp;are&nbsp;the&nbsp;strings&nbsp;which&nbsp;come&nbsp;after&nbsp;each&nbsp;word.</tt></dd></dl>

<hr>
Data descriptors defined here:<br>
<dl><dt><strong>__dict__</strong></dt>
<dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd>
</dl>
<dl><dt><strong>__weakref__</strong></dt>
<dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd>
</dl>
</td></tr></table></td></tr></table>
</body></html>
lt;nincsnevem662@gmail.com> 2021-08-04 17:54:27 +0200 More css stuff' href='/ahoang/chawan/commit/src/io/twtio.nim?id=caad7b577162a73524277a943050493c489bfb59'>caad7b57 ^
91d18d27 ^
d7a05e7a ^


91d18d27 ^
b1c8e5ed ^
d7a05e7a ^
34b02351 ^
d7a05e7a ^


34b02351 ^
d7a05e7a ^
a6bbcd0d ^
6f7bcc54 ^
d7a05e7a ^



a6bbcd0d ^

d7a05e7a ^
a6bbcd0d ^
91d18d27 ^
a6bbcd0d ^




34b02351 ^
a6bbcd0d ^




d7a05e7a ^
6f7bcc54 ^
6f7bcc54 ^
d7a05e7a ^
a6bbcd0d ^
91d18d27 ^
a6bbcd0d ^





d7a05e7a ^
a6bbcd0d ^

d7a05e7a ^
a6bbcd0d ^




d7a05e7a ^
a6bbcd0d ^



d7a05e7a ^
a6bbcd0d ^


91d18d27 ^
a6bbcd0d ^


d7a05e7a ^
a6bbcd0d ^

91d18d27 ^
a6bbcd0d ^

91d18d27 ^
a6bbcd0d ^



d7a05e7a ^
a6bbcd0d ^






91d18d27 ^
a6bbcd0d ^


d7a05e7a ^
a6bbcd0d ^
91d18d27 ^
a6bbcd0d ^





91d18d27 ^
d7a05e7a ^
a6bbcd0d ^



d7a05e7a ^
a6bbcd0d ^













d7a05e7a ^
a6bbcd0d ^










d7a05e7a ^
a6bbcd0d ^

91d18d27 ^
d7a05e7a ^
a6bbcd0d ^
d7a05e7a ^
a6bbcd0d ^
a6bbcd0d ^
d7a05e7a ^
a6bbcd0d ^
91d18d27 ^

d7a05e7a ^
a6bbcd0d ^
d7a05e7a ^
a6bbcd0d ^
a6bbcd0d ^
d7a05e7a ^





dad0c1c0 ^
d7a05e7a ^

a6bbcd0d ^
d7a05e7a ^



dad0c1c0 ^
d7a05e7a ^




dad0c1c0 ^
d7a05e7a ^


a6bbcd0d ^

8e20aaaf ^
d7a05e7a ^
8e20aaaf ^

91d18d27 ^
8e20aaaf ^

91d18d27 ^
8e20aaaf ^



91d18d27 ^
d7a05e7a ^



a6bbcd0d ^


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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351


               
            
 
                       
                  
                   
                    
                  
                   
 



                        


                           


                        
                





                         
                 


                         
                  


                     
 

                                     
 


























                                                         


                                        
 
                                
                                                         
               
 
                                         
                              
 
                                        
                             
 


                             
 
                                    
                                
 
                                                 
                                                     


                               
                  
               
                       
                       

                                   
                                 
       
                       
                         
                    
                                 


                  

                                       
                                                                
 
                              

                                                  
                                                                        
                                   
                                                                
                     
                
                                                                      
                
                                          
       
                      
                                                                 
                
                                                             
 
                                       
                 
                                
                
 

                                  
                                
                                                            
                                      
                     
                                               

                                          
                
 



                                                                                                       

                 
 
                                                                                                            


                         
                                           
         
                       
       


                                     
 
                                        
                     
 



                                                          

                     
                                           
                     
                                              




                                                        
         




                                                         
                          
               
 
                                        
                                                      
                                          





                                                        
                                        

                     
                                       




                                        
                                      



                                 
                                          


                                                   
                                                    


                       
                                         

                                 
                                                                

                                     
                                          



                       
                                                                          






                                                   
                                                      


                       
                                                                          
                      
                                                   





                                                  
                                             
                               



                       
                                                                           













                                      
                                                                          










                                                          
                                       

                       
                                                     
                     
         
                     
                       
 
                                       
                                 

                                                                  
                                 
         
                                 
                       
 





                                                         
                

                     
 



                                                         
                




                                            
                


                     

                                                            
                                                        
                                                             

                    
                            

                            
                           



                           
                                     



                                              


                                         
import unicode
import strutils
import sequtils
import sugar

import bindings/quickjs
import buffer/cell
import display/term
import js/javascript
import types/color
import utils/twtstr

type
  LineEditState* = enum
    EDIT, FINISH, CANCEL

  LineHistory* = ref object
    lines: seq[string]

  LineEdit* = ref object
    news*: seq[Rune]
    prompt*: string
    promptw: int
    current: string
    state*: LineEditState
    escNext*: bool
    cursor: int
    shift: int
    minlen: int
    maxwidth: int
    displen: int
    disallowed: set[char]
    hide: bool
    term: Terminal
    hist: LineHistory
    histindex: int
    histtmp: string

func newLineHistory*(): LineHistory =
  return LineHistory()

const colorFormat = (func(): Format =
  result = newFormat()
  result.fgcolor = ColorsANSIFg[4] # blue
)()
const defaultFormat = newFormat()
proc printesc(edit: LineEdit, rs: seq[Rune]) =
  var s = ""
  var format = newFormat()
  for r in rs:
    if r.isControlChar():
      s &= edit.term.processFormat(format, colorFormat)
    else:
      s &= edit.term.processFormat(format, defaultFormat)
    s &= r
  edit.term.write(s)

proc printesc(edit: LineEdit, s: string) =
  var s = ""
  var format = newFormat()
  for r in s.runes:
    if r.isControlChar():
      s &= edit.term.processFormat(format, colorFormat)
    else:
      s &= edit.term.processFormat(format, defaultFormat)
    s &= r
  edit.term.write(s)

template kill0(edit: LineEdit, i: int) =
  edit.space(i)
  edit.backward0(i)

template kill0(edit: LineEdit) =
  let w = min(edit.news.width(edit.cursor), edit.displen)
  edit.kill0(w)

proc backward0(state: LineEdit, i: int) =
  state.term.cursorBackward(i)

proc forward0(state: LineEdit, i: int) =
  state.term.cursorForward(i)

proc begin0(edit: LineEdit) =
  edit.term.cursorBegin()
  edit.forward0(edit.minlen)

proc space(edit: LineEdit, i: int) =
  edit.term.write(' '.repeat(i))

proc generateOutput*(edit: LineEdit): FixedGrid =
  result = newFixedGrid(edit.promptw + edit.maxwidth)
  var x = 0
  for r in edit.prompt.runes():
    result[x].str &= $r
    x += r.width()
  if edit.hide:
    for r in edit.news:
      let w = r.width()
      result[x].str = '*'.repeat(w)
      x += w
      if x >= result.width: break
  else:
    for r in edit.news:
      result[x].str &= $r
      x += r.width()
      if x >= result.width: break
  var s = ""
  for c in result:
    s &= c.str

proc getCursorX*(edit: LineEdit): int =
  return edit.promptw + edit.news.width(edit.shift, edit.cursor)

proc redraw(state: LineEdit) =
  if state.shift + state.displen > state.news.len:
    state.displen = state.news.len - state.shift
  var dispw = state.news.width(state.shift, state.shift + state.displen)
  while dispw > state.maxwidth - 1:
    dispw -= state.news[state.shift + state.displen - 1].width()
    dec state.displen
  state.begin0()
  let os = state.news.substr(state.shift, state.shift + state.displen)
  if state.hide:
    state.printesc('*'.repeat(os.width()))
  else:
    state.printesc(os)
  state.space(max(state.maxwidth - state.minlen - os.width(), 0))
  state.begin0()
  state.forward0(state.news.width(state.shift, state.cursor))

proc zeroShiftRedraw(state: LineEdit) =
  state.shift = 0
  state.displen = state.news.len
  state.redraw()

proc fullRedraw(state: LineEdit) =
  state.displen = state.news.len
  if state.cursor > state.shift:
    var shiftw = state.news.width(state.shift, state.cursor)
    while shiftw > state.maxwidth - 1:
      inc state.shift
      shiftw -= state.news[state.shift].width()
  else:
    state.shift = max(state.cursor - 1, 0)
  state.redraw()

proc insertCharseq(edit: LineEdit, cs: var seq[Rune]) =
  let escNext = edit.escNext
  cs.keepIf((r) => (escNext or not r.isControlChar) and not (r.isAscii and char(r) in edit.disallowed))
  edit.escNext = false
  if cs.len == 0:
    return

  if edit.cursor >= edit.news.len and edit.news.width(edit.shift, edit.cursor) + cs.width() < edit.maxwidth:
    edit.news &= cs
    edit.cursor += cs.len
    if edit.hide:
      edit.printesc('*'.repeat(cs.width()))
    else:
      edit.printesc(cs)
  else:
    edit.news.insert(cs, edit.cursor)
    edit.cursor += cs.len
    edit.fullRedraw()

proc cancel(edit: LineEdit) {.jsfunc.} =
  edit.state = CANCEL

proc submit(edit: LineEdit) {.jsfunc.} =
  let s = $edit.news
  if edit.hist.lines.len == 0 or s != edit.hist.lines[^1]:
    edit.hist.lines.add(s)
  edit.state = FINISH

proc backspace(edit: LineEdit) {.jsfunc.} =
  if edit.cursor > 0:
    let w = edit.news[edit.cursor - 1].width()
    edit.news.delete(edit.cursor - 1..edit.cursor - 1)
    dec edit.cursor
    if edit.cursor == edit.news.len and edit.shift == 0:
      edit.backward0(w)
      edit.kill0(w)
    else:
      edit.fullRedraw()

proc write*(edit: LineEdit, s: string): bool {.jsfunc.} =
  if validateUtf8(s) == -1:
    var cs = s.toRunes()
    edit.insertCharseq(cs)
    return true

proc delete(edit: LineEdit) {.jsfunc.} =
  if edit.cursor >= 0 and edit.cursor < edit.news.len:
    let w = edit.news[edit.cursor].width()
    edit.news.delete(edit.cursor..edit.cursor)
    if edit.cursor == edit.news.len and edit.shift == 0:
      edit.kill0(w)
    else:
      edit.fullRedraw()

proc escape(edit: LineEdit) {.jsfunc.} =
  edit.escNext = true

proc clear(edit: LineEdit) {.jsfunc.} =
  if edit.cursor > 0:
    edit.news.delete(0..edit.cursor - 1)
    edit.cursor = 0
    edit.zeroShiftRedraw()

proc kill(edit: LineEdit) {.jsfunc.} =
  if edit.cursor < edit.news.len:
    edit.kill0()
    edit.news.setLen(edit.cursor)

proc backward(edit: LineEdit) {.jsfunc.} =
  if edit.cursor > 0:
    dec edit.cursor
    if edit.cursor > edit.shift or edit.shift == 0:
      edit.backward0(edit.news[edit.cursor].width())
    else:
      edit.fullRedraw()

proc forward(edit: LineEdit) {.jsfunc.} =
  if edit.cursor < edit.news.len:
    inc edit.cursor
    if edit.news.width(edit.shift, edit.cursor) < edit.maxwidth:
      var n = 1
      if edit.news.len > edit.cursor:
        n = edit.news[edit.cursor].width()
      edit.forward0(n)
    else:
      edit.fullRedraw()

proc prevWord(edit: LineEdit, check = none(BoundaryFunction)) {.jsfunc.} =
  let oc = edit.cursor
  while edit.cursor > 0:
    dec edit.cursor
    if edit.news[edit.cursor].breaksWord(check):
      break
  if edit.cursor != oc:
    if edit.cursor > edit.shift or edit.shift == 0:
      edit.backward0(edit.news.width(edit.cursor, oc))
    else:
      edit.fullRedraw()

proc nextWord(edit: LineEdit, check = none(BoundaryFunction)) {.jsfunc.} =
  let oc = edit.cursor
  let ow = edit.news.width(edit.shift, edit.cursor)
  while edit.cursor < edit.news.len:
    inc edit.cursor
    if edit.cursor < edit.news.len:
      if edit.news[edit.cursor].breaksWord(check):
        break
  if edit.cursor != oc:
    let dw = edit.news.width(oc, edit.cursor)
    if ow + dw < edit.maxwidth:
      edit.forward0(dw)
    else:
      edit.fullRedraw()

proc clearWord(edit: LineEdit, check = none(BoundaryFunction)) {.jsfunc.} =
  var i = edit.cursor
  if i > 0:
    # point to the previous character
    dec i
  while i > 0:
    dec i
    if edit.news[i].breaksWord(check):
      inc i
      break
  if i != edit.cursor:
    edit.news.delete(i..<edit.cursor)
    edit.cursor = i
    edit.fullRedraw()

proc killWord(edit: LineEdit, check = none(BoundaryFunction)) {.jsfunc.} =
  var i = edit.cursor
  if i < edit.news.len and edit.news[i].breaksWord(check):
    inc i
  while i < edit.news.len:
    if edit.news[i].breaksWord(check):
      break
    inc i
  if i != edit.cursor:
    edit.news.delete(edit.cursor..<i)
    edit.fullRedraw()

proc begin(edit: LineEdit) {.jsfunc.} =
  if edit.cursor > 0:
    if edit.shift == 0:
      edit.backward0(edit.news.width(0, edit.cursor))
      edit.cursor = 0
    else:
      edit.cursor = 0
      edit.fullRedraw()

proc `end`(edit: LineEdit) {.jsfunc.} =
  if edit.cursor < edit.news.len:
    if edit.news.width(edit.shift, edit.news.len) < edit.maxwidth:
      edit.forward0(edit.news.width(edit.cursor, edit.news.len))
      edit.cursor = edit.news.len
    else:
      edit.cursor = edit.news.len
      edit.fullRedraw()

proc prevHist(edit: LineEdit) {.jsfunc.} =
  if edit.histindex > 0:
    if edit.news.len > 0:
      edit.histtmp = $edit.news
    dec edit.histindex
    edit.news = edit.hist.lines[edit.histindex].toRunes()
    edit.begin()
    edit.end()
    edit.fullRedraw()

proc nextHist(edit: LineEdit) {.jsfunc.} =
  if edit.histindex + 1 < edit.hist.lines.len:
    inc edit.histindex
    edit.news = edit.hist.lines[edit.histindex].toRunes()
    edit.begin()
    edit.end()
    edit.fullRedraw()
  elif edit.histindex < edit.hist.lines.len:
    inc edit.histindex
    edit.news = edit.histtmp.toRunes()
    edit.begin()
    edit.end()
    edit.fullRedraw()
    edit.histtmp = ""

proc readLine*(prompt: string, termwidth: int, current = "",
               disallowed: set[char] = {}, hide = false,
               term: Terminal, hist: LineHistory): LineEdit =
  result = LineEdit(
    prompt: prompt,
    promptw: prompt.width(),
    current: current,
    news: current.toRunes(),
    minlen: prompt.width(),
    disallowed: disallowed,
    hide: hide,
    term: term
  )
  result.cursor = result.news.width()
  result.maxwidth = termwidth - result.promptw
  result.displen = result.cursor
  result.hist = hist
  result.histindex = result.hist.lines.len

proc addLineEditModule*(ctx: JSContext) =
  ctx.registerType(LineEdit)