88" on Windows or Cygwin there may be a performance hit :-(
99
1010" "
11+ " Encode string to Base64 via `system` call
1112"
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
1238" See: {tests} tests/units/autoload_proompter_base64.vader
1339function ! proompter#base64#EncodeString (string ) abort
1440 if ! len (a: string )
1541 throw ' No string value'
1642 endif
43+
1744 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
1952endfunction
2053
2154" "
55+ " Decode string from Base64 via `system` call
56+ "
57+ " Parameter: {string} `string` Input to pipe to `base64` command
2258"
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
2382" See: {tests} tests/units/autoload_proompter_base64.vader
2483function ! proompter#base64#DecodeString (string ) abort
2584 if ! len (a: string )
2685 throw ' No string value'
2786 endif
87+
2888 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
3096endfunction
3197
3298" "
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
33106"
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
34130" See: {tests} tests/units/autoload_proompter_base64.vader
35131function ! proompter#base64#EncodeFile (path ) abort
36132 if ! len (a: path )
@@ -42,14 +138,59 @@ function! proompter#base64#EncodeFile(path) abort
42138 endif
43139
44140 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
46148endfunction
47149
48150" "
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
49186" TODO: implement `flags` parser to have similar behavior to `writefile`
50187"
51188" See: {tests} tests/units/autoload_proompter_base64.vader
52189function ! proompter#base64#DecodeToFile (string , path , flags = ' ' ) abort
190+ if ! len (a: string )
191+ throw ' No string value'
192+ endif
193+
53194 if ! len (a: path )
54195 throw ' No path value'
55196 endif
@@ -60,7 +201,13 @@ function! proompter#base64#DecodeToFile(string, path, flags = '') abort
60201
61202 let l: path = shellescape (a: path )
62203 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
64211endfunction
65212
66213" vim: expandtab
0 commit comments