UNPKG

4.28 kBJavaScriptView Raw
1#!/usr/bin/env node
2/*
3 Terminal Kit
4
5 Copyright (c) 2009 - 2020 Cédric Ronvel
6
7 The MIT License (MIT)
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in all
17 copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 SOFTWARE.
26*/
27
28"use strict" ;
29
30
31
32var termkit = require( 'terminal-kit' ) ;
33var term = termkit.terminal ;
34var path = require( 'path' ) ;
35
36
37
38if ( 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
46var screen , image , filler , move , maxScale , SB ,
47 url = process.argv[ 2 ] ;
48
49if ( 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}
56else {
57 SB = termkit.ScreenBuffer ;
58 filler = { attr: {
59 color: 'black' ,
60 bgColor: 'black' ,
61 } } ;
62}
63
64
65// Can't depend on minimist just for a sample code, so we had to parse the command line by ourself
66if ( process.argv[ 3 ] === '-m' ) {
67 move = true ;
68 maxScale = process.argv[ 4 ] || 2 ;
69}
70else {
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
83if ( ! 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
96async 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 ) ; // Perform term.scrollDown()
122 image.y += offset ;
123 image.draw() ;
124 stats = screen.draw( { delta: true } ) ; // This only redraws new lines on the top
125 //console.error( stats ) ;
126 break ;
127 case 'DOWN' :
128 offset = Math.round( term.height / 20 ) ;
129 screen.vScroll( - offset , true ) ; // Perform term.scrollUp()
130 image.y += - offset ;
131 image.draw() ;
132 stats = screen.draw( { delta: true } ) ; // This only redraws new lines on the bottom
133 //console.error( stats ) ;
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
157function redraw() {
158 var stats ;
159
160 screen.fill( filler ) ;
161 image.draw() ;
162 stats = screen.draw( { delta: true } ) ;
163 //console.error( stats ) ;
164}
165
166
167
168function terminate() {
169 term.hideCursor( false ) ;
170 //term.applicationKeypad( false ) ;
171 term.styleReset() ;
172 term.resetScrollingRegion() ;
173 term.moveTo( term.width , term.height ) ;
174 term( '\n' ) ;
175 term.processExit() ;
176}
177
178
179
180loadImage() ;
181