Skip to content
Newer
Older
100644 236 lines (214 sloc) 7.78 KB
26c68c7 Added a directory brower dialog.
megaannum authored
1
2 " ------------------------------------------------------------
3 " forms#dialog#directorybrowser#Make:
4 " A form to browse file system and select directory.
5 " The form has:
6 " Label (title)
7 " VariableLengthField (file name)
8 " SelectList
9 " Cancel and Open buttons
10 " Example:
11 " ┌───────────────────────────┐
12 " │ Directory Browser │
13 " ├───────────────────────────┤
14 " │┌─────────────────────────┐│
15 " ││ ││
16 " │└─────────────────────────┘│
17 " │┌─────────────────┐ │
18 " ││../ │ │
19 " ││build/ │ │
20 " ││examples/ │ │
21 " ││extra/ │ │
22 " ││ivy/ │ │
23 " ││lib/ │ │
24 " ││other/ │ │
25 " ││src/ │ │
26 " ││tmp/ │ │
27 " ││ │ │
28 " │└─────────────────┘ │
29 " │ Cancel Open│
30 " └───────────────────────────┘
31 "
32 " parameters:
33 " attrs: optional Dictionary
34 " title: title (default Directory Browser)
35 " dir: initial directory (default cwdir)
36 " ------------------------------------------------------------
37 " forms#dialog#directorybrowser#Make: {{{1
38 function! forms#dialog#directorybrowser#Make(...)
39
40 function! DBMakeChoices(dir)
41 let files = split(globpath(a:dir, "*"), '\n')
42 let dotfiles = split(globpath(a:dir, ".*"), '\n')
43 let dlist = []
44 let cnt = 1
45 for dotfile in dotfiles
46 let index = strridx(dotfile, '/')+1
47 let dirname = strpart(dotfile, index, len(dotfile)-index)
48 if isdirectory(dotfile) && dirname != '.'
49 call add(dlist, [dirname.'/', cnt])
50 endif
51
52 let cnt += 1
53 endfor
54 for file in files
55 let index = strridx(file, '/')+1
56 let dirname = strpart(file, index, len(file)-index)
57 if isdirectory(file)
58 call add(dlist, [dirname.'/', cnt])
59 endif
60
61 let cnt += 1
62 endfor
63 return dlist
64 endfunction
65
66 let title = 'Directory Browser'
67 let initial_dir = getcwd()
68 if a:0 != 0
69 if ! type(a:1) == g:self#DICTIONARY_TYPE
70 throw "Error: forms#dialog#directorybrowser#Make bad argument type: " .string(a:1)
71 endif
72 let dict = a:1
73 if has_key(dict, 'title')
74 let title = dict['title']
75 endif
76 if has_key(dict, 'dir')
77 let initial_dir = dict['dir']
78 endif
79 endif
80
81 let attrs = { 'text': title }
82 let titleLabel = forms#newLabel(attrs)
83
84 function! DBVLFAction(...) dict
85 call self.acceptbutton.doSelect()
86 endfunction
87 let dnaction = forms#newAction({ 'execute': function("DBVLFAction")})
88
89 let esize = 25
90 let fn = forms#newVariableLengthField({
91 \ 'tag': 'editor',
92 \ 'size': esize,
93 \ 'on_selection_action': dnaction
94 \ })
95 function! fn.purpose() dict
96 return [
97 \ "Use editor ot enter a filename.",
98 \ " Enter <CR> to accept the entered filename."
99 \ ]
100 endfunction
101 let fnbox = forms#newBox({'body': fn})
102
103 function! DBAction(...) dict
104 let slist = self.slist
105 let fn = self.fn
106 let pos = slist.__selections[0][0]
107 let [dirname,_] = slist.__choices[pos]
108
109 let dir = self.dir
110 let dir = simplify(dir . '/' . dirname)
111 let self.dir = dir
112 let choices = DBMakeChoices(dir)
113 let attrs = {
114 \ 'tag': 'selection',
115 \ 'mode': 'single',
116 \ 'pos': 0,
117 \ 'choices': choices,
118 \ 'size': slist.__size,
119 \ 'on_selection_action': self
120 \ }
121 call slist.reinit(attrs)
122 call fn.setText(dir)
123 let fn.__pos = len(dir)
124 call fn.adjustWinStart()
125 endfunction
126 let dbaction = forms#newAction({ 'execute': function("DBAction")})
127 let dbaction.fn = fn
128
129 let dir = getcwd()
130
131 call fn.setText(initial_dir)
132 let fn.__pos = len(initial_dir)
133 call fn.adjustWinStart()
134
135 let choices = DBMakeChoices(initial_dir)
136
137 " SelectList
138 let ssize = 10
139 let attrs = {
140 \ 'tag': 'selection',
141 \ 'mode': 'single',
142 \ 'choices': choices,
143 \ 'size': ssize,
144 \ 'on_selection_action': dbaction
145 \ }
146 let slist = forms#newSelectList(attrs)
147 function! slist.purpose() dict
148 return [
149 \ "Navigate file system with select list.",
150 \ " Select ../ to move up a directory. Select a directory",
151 \ " name to enter that directory, Select a directory to enter",
152 \ " the directory into the editor."
153 \ ]
154 endfunction
155 let dbaction.slist = slist
156 let dbaction.dir = initial_dir
157 let slistbox = forms#newBox({ 'body': slist} )
158
159 " Bottom HPoly
160 let hspaceB = forms#newHSpace({'size': 1})
161
162 let attrs = { 'text': 'Cancel'}
163 let cancellabel = forms#newLabel(attrs)
164 let cancelbutton = forms#newButton({
165 \ 'tag': 'cancel',
166 \ 'body': cancellabel,
167 \ 'action': g:forms#cancelAction})
168 function! cancelbutton.purpose() dict
169 return [
170 \ "Make no selection, cancel operation."
171 \ ]
172 endfunction
173 let attrs = { 'text': 'Accept'}
174 let acceptlabel = forms#newLabel(attrs)
175 let acceptbutton = forms#newButton({
176 \ 'tag': 'accept',
177 \ 'body': acceptlabel,
178 \ 'action': g:forms#submitAction})
179 function! acceptbutton.purpose() dict
180 return [
181 \ "Accept the directoryname in the editor."
182 \ ]
183 endfunction
184 let dnaction.acceptbutton = acceptbutton
185
186 let hpoly = forms#newHPoly({'children': [
187 \ cancelbutton,
188 \ hspaceB,
189 \ acceptbutton] })
190
191 let vpoly = forms#newVPoly({'children': [
192 \ fnbox,
193 \ slistbox,
194 \ hpoly],
195 \ 'alignments': [[2,'R']]
196 \ })
197 let topvpoly = forms#newVPoly({'children': [
198 \ titleLabel,
199 \ vpoly],
200 \ 'mode': 'light',
201 \ 'alignment': 'C'
202 \ })
203
204 " let b = forms#newBorder({ 'body': vpoly })
205 let bg = forms#newBackground({ 'body': topvpoly })
206 let form = forms#newForm({'body': bg })
207 function! form.purpose() dict
208 return [
209 \ "Navigate the file system and select a directory.",
210 \ " Use editor to enter a directory name or the select list",
211 \ " to navigate the file system and make a directory selection.",
212 \ " Press Cancel to make no selection.",
213 \ " Press Open to make directory selection."
214 \ ]
215 endfunction
216 let results = form.run()
217 if type(results) == g:self#DICTIONARY_TYPE
218 if results.accept
219 let rval = results.editor
220 return rval
221 endif
222 endif
223 return ''
224 endfunction
225
226 " forms#dialog#directorybrowser#MakeTest: {{{1
227 function! forms#dialog#directorybrowser#MakeTest()
228 call forms#AppendInput({'type': 'Sleep', 'time': 5})
229 call forms#AppendInput({'type': 'Exit'})
230 call forms#dialog#directorybrowser#Make()
231 endfunction
232
233 " Modelines: {{{1
234 " ================
235 " vim: ts=4 fdm=marker
Something went wrong with that request. Please try again.