File tree Expand file tree Collapse file tree 2 files changed +32
-10
lines changed
Expand file tree Collapse file tree 2 files changed +32
-10
lines changed Original file line number Diff line number Diff line change 11# File Downloading
22
33WeTTY supports file downloads by printing terminal escape sequences between a
4- base64 encoded file.
4+ base64 encoded file. The name of the downloaded file can optionally be provided,
5+ also base64 encoded, before the encoded file contents with a ` : ` separating them.
56
67The terminal escape sequences used are ` ^[[5i ` and ` ^[[4i ` (VT100 for "enter
78auto print" and "exit auto print" respectively -
@@ -13,8 +14,14 @@ To take advantage add the following bash function to your `.bashrc`
1314function wetty-download() {
1415 file=${1:-/ dev/ stdin}
1516
16- if [[ -f $file || $file == " /dev/stdin" ]]; then
17- printf " \033[5i" $( cat $file | base64 -w 0) " \033[4i"
17+ nameprefix=" "
18+ if [[ -f " $file " ]]; then
19+ nameprefix=" $( basename " $file " | base64 -w 0) :"
20+ fi
21+
22+
23+ if [[ -f " $file " || " $file " == " /dev/stdin" ]]; then
24+ printf " \033[5i" $nameprefix $( cat " $file " | base64 -w 0) " \033[4i"
1825 else
1926 echo " $file does not appear to be a file"
2027 fi
@@ -27,7 +34,7 @@ You are then able to download files via WeTTY!
2734wetty-download my-pdf-file.pdf
2835```
2936
30- or you can still use the classic style:
37+ or you can still use the classic style:
3138
3239``` bash
3340$ cat my-pdf-file.pdf | wetty-download
Original file line number Diff line number Diff line change @@ -7,7 +7,11 @@ const DEFAULT_FILE_END = '\u001b[4i';
77type OnCompleteFile = ( bufferCharacters : string ) => void ;
88
99function onCompleteFile ( bufferCharacters : string ) : void {
10+ let fileNameBase64 ;
1011 let fileCharacters = bufferCharacters ;
12+ if ( bufferCharacters . includes ( ":" ) ) {
13+ [ fileNameBase64 , fileCharacters ] = bufferCharacters . split ( ":" ) ;
14+ }
1115 // Try to decode it as base64, if it fails we assume it's not base64
1216 try {
1317 fileCharacters = window . atob ( fileCharacters ) ;
@@ -34,12 +38,23 @@ function onCompleteFile(bufferCharacters: string): void {
3438 mimeType = 'text/plain' ;
3539 fileExt = 'txt' ;
3640 }
37- const fileName = `file-${ new Date ( )
38- . toISOString ( )
39- . split ( '.' ) [ 0 ]
40- . replace ( / - / g, '' )
41- . replace ( 'T' , '' )
42- . replace ( / : / g, '' ) } ${ fileExt ? `.${ fileExt } ` : '' } `;
41+ let fileName ;
42+ try {
43+ if ( fileNameBase64 !== undefined ) {
44+ fileName = window . atob ( fileNameBase64 ) ;
45+ }
46+ } catch ( err ) {
47+ // Filename wasn't base64-encoded so let's ignore it
48+ }
49+
50+ if ( fileName === undefined ) {
51+ fileName = `file-${ new Date ( )
52+ . toISOString ( )
53+ . split ( '.' ) [ 0 ]
54+ . replace ( / - / g, '' )
55+ . replace ( 'T' , '' )
56+ . replace ( / : / g, '' ) } ${ fileExt ? `.${ fileExt } ` : '' } `;
57+ }
4358
4459 const blob = new Blob ( [ new Uint8Array ( bytes . buffer ) ] , {
4560 type : mimeType ,
You can’t perform that action at this time.
0 commit comments