8
8
" on Windows or Cygwin there may be a performance hit :-(
9
9
10
10
" "
11
+ " Encode string to Base64 via `system` call
11
12
"
13
+ " Parameter: {string} `string` Input to pipe to `base64` command
14
+ "
15
+ " Throws: 'No string value' if input is zero length
16
+ " Throws: following format string when `v:shell_error` is non-zero
17
+ "
18
+ " ```
19
+ " Failed command: printf "%s" '<string>' | base64 --wrap=0
20
+ " Exit status: <number>
21
+ " ```
22
+ "
23
+ " Example: input
24
+ "
25
+ " ```vim
26
+ " echo proompter#base64#EncodeString('Howdy reader!')
27
+ " ```
28
+ "
29
+ " Example: result
30
+ "
31
+ " ```
32
+ " SG93ZHkgcmVhZGVyIQ==
33
+ " ```
34
+ "
35
+ " See: {docs} :help system()
36
+ " See: {docs} :help shellescape()
37
+ " See: {docs} :help v:shell_error
12
38
" See: {tests} tests/units/autoload_proompter_base64.vader
13
39
function ! proompter#base64#EncodeString (string ) abort
14
40
if ! len (a: string )
15
41
throw ' No string value'
16
42
endif
43
+
17
44
let l: string = shellescape (a: string )
18
- return system (' printf "%s" ' . l: string . ' | base64 --wrap=0' )
45
+ let l: command = ' printf "%s" ' . l: string . ' | base64 --wrap=0'
46
+
47
+ let l: result = system (l: command )
48
+ if v: shell_error
49
+ throw " Failed command: " . l: command . " \n \t Exit status: " . v: shell_error
50
+ endif
51
+ return l: result
19
52
endfunction
20
53
21
54
" "
55
+ " Decode string from Base64 via `system` call
56
+ "
57
+ " Parameter: {string} `string` Input to pipe to `base64` command
22
58
"
59
+ " Throws: 'No string value' if input is zero length
60
+ " Throws: following format string when `v:shell_error` is non-zero
61
+ "
62
+ " ```
63
+ " Failed command: printf "%s" '<string>' | base64 --decode
64
+ " Exit status: <number>
65
+ " ```
66
+ "
67
+ " Example: input
68
+ "
69
+ " ```vim
70
+ " echo proompter#base64#EncodeString('SG93ZHkgcmVhZGVyIQ==')
71
+ " ```
72
+ "
73
+ " Example: result
74
+ "
75
+ " ```
76
+ " Howdy reader!
77
+ " ```
78
+ "
79
+ " See: {docs} :help system()
80
+ " See: {docs} :help shellescape()
81
+ " See: {docs} :help v:shell_error
23
82
" See: {tests} tests/units/autoload_proompter_base64.vader
24
83
function ! proompter#base64#DecodeString (string ) abort
25
84
if ! len (a: string )
26
85
throw ' No string value'
27
86
endif
87
+
28
88
let l: string = shellescape (a: string )
29
- return system (' printf "%s" ' . l: string . ' | base64 --decode' )
89
+ let l: command = ' printf "%s" ' . l: string . ' | base64 --decode'
90
+
91
+ let l: result = system (l: command )
92
+ if v: shell_error
93
+ throw " Failed command: " . l: command . " \n \t Exit status: " . v: shell_error
94
+ endif
95
+ return l: result
30
96
endfunction
31
97
32
98
" "
99
+ " Encode file at `path` via `system` call to `base64`
100
+ "
101
+ " Parameter: {string} `path` Input to file for `base64` to encode
102
+ "
103
+ " Throws: 'No path value' if input is zero length
104
+ " Throws: 'Cannot read file -> <path>' when file cannot be read
105
+ " Throws: following format string when `v:shell_error` is non-zero
33
106
"
107
+ " ```
108
+ " Failed command: printf "%s" '<string>' | base64 --decode
109
+ " Exit status: <number>
110
+ " ```
111
+ "
112
+ " Example: create and pass input file
113
+ "
114
+ " ```vim
115
+ " let path = '/tmp/test.txt'
116
+ " call writefile(['Howdy reader!'], path)
117
+ " echo proompter#base64#EncodeFile(path)
118
+ " ```
119
+ "
120
+ " Example: result
121
+ "
122
+ " ```
123
+ " SG93ZHkgcmVhZGVyIQo=
124
+ " ```
125
+ "
126
+ " See: {docs} :help filereadable()
127
+ " See: {docs} :help system()
128
+ " See: {docs} :help shellescape()
129
+ " See: {docs} :help v:shell_error
34
130
" See: {tests} tests/units/autoload_proompter_base64.vader
35
131
function ! proompter#base64#EncodeFile (path ) abort
36
132
if ! len (a: path )
@@ -42,14 +138,59 @@ function! proompter#base64#EncodeFile(path) abort
42
138
endif
43
139
44
140
let l: path = shellescape (a: path )
45
- return system (' base64 --wrap=0 ' . l: path )
141
+ let l: command = ' base64 --wrap=0 ' . l: path
142
+
143
+ let l: result = system (l: command )
144
+ if v: shell_error
145
+ throw " Failed command: " . l: command . " \n \t Exit status: " . v: shell_error
146
+ endif
147
+ return l: result
46
148
endfunction
47
149
48
150
" "
151
+ " Decode `string` to file at `path` via `system` call to `base64`
152
+ "
153
+ " Parameter: {string} `string` Encoded string to pipe to `base64`
154
+ " Parameter: {string} `path` File for `base64` to save decoded results to
155
+ " Parameter: {string} `flags` TODO
156
+ "
157
+ " Throws: 'No string value' if input is zero length
158
+ " Throws: 'No path value' if input is zero length
159
+ " Throws: 'File already exists -> <path>' when file cannot be overwritten
160
+ " Throws: following format string when `v:shell_error` is non-zero
161
+ "
162
+ " ```
163
+ " Failed command: printf "%s" '<string>' | base64 --decode > <path>
164
+ " Exit status: <number>
165
+ " ```
166
+ "
167
+ " Example: create and pass input file
168
+ "
169
+ " ```vim
170
+ " let path = '/tmp/decode.txt'
171
+ " call proompter#base64#DecodeToFile('SG93ZHkgcmVhZGVyIQo=', path)
172
+ " !cat /tmp/decode.txt
173
+ " ```
174
+ "
175
+ " Example: result
176
+ "
177
+ " ```
178
+ " Howdy reader!
179
+ " ```
180
+ "
181
+ " See: {docs} :help filereadable()
182
+ " See: {docs} :help system()
183
+ " See: {docs} :help shellescape()
184
+ " See: {docs} :help v:shell_error
185
+ " See: {tests} tests/units/autoload_proompter_base64.vader
49
186
" TODO: implement `flags` parser to have similar behavior to `writefile`
50
187
"
51
188
" See: {tests} tests/units/autoload_proompter_base64.vader
52
189
function ! proompter#base64#DecodeToFile (string , path , flags = ' ' ) abort
190
+ if ! len (a: string )
191
+ throw ' No string value'
192
+ endif
193
+
53
194
if ! len (a: path )
54
195
throw ' No path value'
55
196
endif
@@ -60,7 +201,13 @@ function! proompter#base64#DecodeToFile(string, path, flags = '') abort
60
201
61
202
let l: path = shellescape (a: path )
62
203
let l: string = shellescape (a: string )
63
- return system (' printf "%s" ' . l: string . ' | base64 --decode > ' . l: path )
204
+ let l: command = ' printf "%s" ' . l: string . ' | base64 --decode > ' . l: path
205
+
206
+ let l: result = system (l: command )
207
+ if v: shell_error
208
+ throw " Failed command: " . l: command . " \n \t Exit status: " . v: shell_error
209
+ endif
210
+ return l: result
64
211
endfunction
65
212
66
213
" vim: expandtab
0 commit comments