1 | #!/usr/bin/env node
|
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 | "use strict" ;
|
29 |
|
30 |
|
31 |
|
32 | var termkit = require( 'terminal-kit' ) ;
|
33 | var term = termkit.terminal ;
|
34 | var path = require( 'path' ) ;
|
35 |
|
36 |
|
37 |
|
38 | if ( process.argv.length <= 2 ) {
|
39 | term.magenta( "Usage is: ./%s <file-path> [-m] [<max-scale>]\n" , path.basename( process.argv[ 1 ] ) ) ;
|
40 | term.gray( "-m: load inside a ScreenBuffer and interactively move the image\n" ) ;
|
41 | process.exit( 1 ) ;
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 | var screen , image , filler , move , maxScale , SB ,
|
47 | url = process.argv[ 2 ] ;
|
48 |
|
49 | if ( term.support['24bitsColors'] ) {
|
50 | SB = termkit.ScreenBufferHD ;
|
51 | filler = { attr: {
|
52 | color: { r: 0 , g: 0 , b: 0 } ,
|
53 | bgColor: { r: 0 , g: 0 , b: 0 }
|
54 | } } ;
|
55 | }
|
56 | else {
|
57 | SB = termkit.ScreenBuffer ;
|
58 | filler = { attr: {
|
59 | color: 'black' ,
|
60 | bgColor: 'black' ,
|
61 | } } ;
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 | if ( process.argv[ 3 ] === '-m' ) {
|
67 | move = true ;
|
68 | maxScale = process.argv[ 4 ] || 2 ;
|
69 | }
|
70 | else {
|
71 | if ( process.argv[ 4 ] === '-m' ) {
|
72 | move = true ;
|
73 | maxScale = process.argv[ 3 ] || 2 ;
|
74 | }
|
75 | else {
|
76 | move = false ;
|
77 | maxScale = process.argv[ 3 ] || 1 ;
|
78 | }
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 | if ( ! move ) {
|
84 | term.drawImage( url , {
|
85 | shrink: {
|
86 | width: term.width * maxScale ,
|
87 | height: ( term.height - 1 ) * 2 * maxScale
|
88 | }
|
89 | } ) ;
|
90 |
|
91 | return ;
|
92 | }
|
93 |
|
94 |
|
95 |
|
96 | async function loadImage() {
|
97 | image = await SB.loadImage(
|
98 | url ,
|
99 | {
|
100 | terminal: term ,
|
101 | shrink: { width: term.width * maxScale , height: ( term.height - 1 ) * 2 * maxScale }
|
102 | }
|
103 | ) ;
|
104 |
|
105 | screen = SB.create( { dst: term , height: term.height - 1 , noFill: true } ) ;
|
106 | screen.y = 2 ;
|
107 |
|
108 | image.dst = screen ;
|
109 |
|
110 | term.clear() ;
|
111 | term.grabInput() ;
|
112 | term.hideCursor() ;
|
113 |
|
114 | term.on( 'key' , ( key , matches , data ) => {
|
115 |
|
116 | var offset , stats ;
|
117 |
|
118 | switch ( key ) {
|
119 | case 'UP' :
|
120 | offset = Math.round( term.height / 20 ) ;
|
121 | screen.vScroll( offset , true ) ;
|
122 | image.y += offset ;
|
123 | image.draw() ;
|
124 | stats = screen.draw( { delta: true } ) ;
|
125 |
|
126 | break ;
|
127 | case 'DOWN' :
|
128 | offset = Math.round( term.height / 20 ) ;
|
129 | screen.vScroll( - offset , true ) ;
|
130 | image.y += - offset ;
|
131 | image.draw() ;
|
132 | stats = screen.draw( { delta: true } ) ;
|
133 |
|
134 | break ;
|
135 | case 'LEFT' :
|
136 | offset = Math.round( term.width / 20 ) ;
|
137 | image.x += offset ;
|
138 | redraw() ;
|
139 | break ;
|
140 | case 'RIGHT' :
|
141 | offset = Math.round( term.width / 20 ) ;
|
142 | image.x -= offset ;
|
143 | redraw() ;
|
144 | break ;
|
145 | case 'q' :
|
146 | case 'CTRL_C' :
|
147 | terminate() ;
|
148 | break ;
|
149 | }
|
150 | } ) ;
|
151 |
|
152 | redraw() ;
|
153 | term.moveTo( 1 , 1 ).bgWhite.blue.eraseLineAfter( "Arrows keys: move Q/CTRL-C: quit" ) ;
|
154 | }
|
155 |
|
156 |
|
157 | function redraw() {
|
158 | var stats ;
|
159 |
|
160 | screen.fill( filler ) ;
|
161 | image.draw() ;
|
162 | stats = screen.draw( { delta: true } ) ;
|
163 |
|
164 | }
|
165 |
|
166 |
|
167 |
|
168 | function terminate() {
|
169 | term.hideCursor( false ) ;
|
170 |
|
171 | term.styleReset() ;
|
172 | term.resetScrollingRegion() ;
|
173 | term.moveTo( term.width , term.height ) ;
|
174 | term( '\n' ) ;
|
175 | term.processExit() ;
|
176 | }
|
177 |
|
178 |
|
179 |
|
180 | loadImage() ;
|
181 |
|