11/// usr/bin/env jbang "$0" "$@" ; exit $?
22
33// spotless:off
4- //DEPS org.codejive.twinkle:twinkle-terminal-jline :1.0-SNAPSHOT
4+ //DEPS org.codejive.twinkle:twinkle-terminal-aesh :1.0-SNAPSHOT
55//DEPS org.codejive.twinkle:twinkle-shapes:1.0-SNAPSHOT
66//DEPS com.github.lalyos:jfiglet:0.0.9
77// spotless:on
@@ -40,6 +40,16 @@ class BouncingTwinkleDemo {
4040
4141 private static final String URL = "https://github.com/codejive/twinkle" ;
4242
43+ private static final Borders .LineStyle [] borders =
44+ new Borders .LineStyle [] {
45+ Borders .LineStyle .ASCII , Borders .LineStyle .SINGLE , Borders .LineStyle .DOUBLE
46+ };
47+
48+ private static final Borders .CornerStyle [] corners =
49+ new Borders .CornerStyle [] {
50+ Borders .CornerStyle .ASCII , Borders .CornerStyle .ROUND , Borders .CornerStyle .SQUARE
51+ };
52+
4353 private static final Color .BasicColor [] textPalette = {
4454 Color .BasicColor .RED ,
4555 Color .BasicColor .YELLOW ,
@@ -58,7 +68,6 @@ class BouncingTwinkleDemo {
5868
5969 public static void main (String [] args ) throws Exception {
6070 try (Terminal terminal = Terminal .getDefault ()) {
61- // terminal.enterRawMode();
6271 size = terminal .size ();
6372
6473 String text = AnsiTricks .blockify (Sizer .trim (FigletFont .convertOneLine ("TWINKLE" )));
@@ -85,20 +94,18 @@ public static void main(String[] args) throws Exception {
8594 buffers .primary (buffer );
8695
8796 Reader reader = terminal .reader ();
88- while (true ) {
89- // Clear buffer for new frame
97+ while (handleKeys (reader )) {
9098 buffer .resize (size );
91- // buffer.clear();
9299
93100 bounce ();
94101 colorize ();
95102
96- Borders b =
103+ Borders border =
97104 new Borders ()
98105 .style (Style .ofFgColor (Color .BasicColor .GREEN ))
99106 .lineStyle (lineStyle )
100107 .cornerStyle (cornerStyle );
101- b .render (buffer );
108+ border .render (buffer );
102109
103110 Fluent f = writer .fluent ();
104111 f .at (2 , 0 ).markup ("{green}[ {white}%s{green} ]" , size );
@@ -113,8 +120,6 @@ public static void main(String[] args) throws Exception {
113120
114121 fps .update ();
115122 Thread .sleep (currentSleep );
116-
117- if (handleKeys (reader ) == -1 ) break ;
118123 }
119124 } finally {
120125 // Show cursor and clear screen on exit
@@ -129,21 +134,11 @@ public static void main(String[] args) throws Exception {
129134 }
130135 }
131136
132- private static Borders .LineStyle [] borders =
133- new Borders .LineStyle [] {
134- Borders .LineStyle .ASCII , Borders .LineStyle .SINGLE , Borders .LineStyle .DOUBLE
135- };
136-
137- private static Borders .CornerStyle [] corners =
138- new Borders .CornerStyle [] {
139- Borders .CornerStyle .ASCII , Borders .CornerStyle .ROUND , Borders .CornerStyle .SQUARE
140- };
141-
142- private static int handleKeys (Reader reader ) throws IOException {
137+ private static boolean handleKeys (Reader reader ) throws IOException {
143138 int ch = reader .ready () ? reader .read () : -1 ;
144139 while (ch >= 0 ) {
145140 if (ch == 'q' || ch == 'Q' ) {
146- return - 1 ;
141+ return false ;
147142 } else if (ch == 'h' || ch == 'H' ) {
148143 toggleHelp ();
149144 } else if (ch == 'b' || ch == 'B' ) {
@@ -157,7 +152,7 @@ private static int handleKeys(Reader reader) throws IOException {
157152 }
158153 ch = reader .ready () ? reader .read () : -1 ;
159154 }
160- return 0 ;
155+ return true ;
161156 }
162157
163158 private static <T > T cycle (T current , T [] values ) {
@@ -171,30 +166,6 @@ private static <T> T cycle(T current, T[] values) {
171166 return values [(idx + 1 ) % values .length ];
172167 }
173168
174- private static Buffer drawHelp () {
175- Buffer buffer = helpBuffer ;
176- PrintBufferWriter writer = buffer .writer ();
177- Borders b =
178- new Borders ()
179- .lineStyle (lineStyle )
180- .cornerStyle (cornerStyle )
181- .style (Style .ofFgColor (Color .BasicColor .BRIGHT_MAGENTA ));
182- b .render (buffer );
183- writer .fluent ().at (2 , 0 ).markup ("{brightmagenta}{+}[ {white}Help Page{-} ]" ).done ();
184- Fluent help =
185- Fluent .string ()
186- .white ()
187- .text ("q - quit" )
188- .lf ()
189- .text ("h - toggle this help" )
190- .lf ()
191- .text ("b - cycle border style" )
192- .lf ()
193- .text ("s - cycle speeds" );
194- writer .fluent ().at (3 , 2 ).block (help ).done ();
195- return buffer ;
196- }
197-
198169 private static void toggleHelp () {
199170 BufferStack .BufferElement helpElement = buffers .contains (helpBuffer );
200171 if (helpElement == null ) {
@@ -213,6 +184,25 @@ private static void toggleHelp() {
213184 }
214185 }
215186
187+ private static void drawHelp () {
188+ PrintBufferWriter writer = helpBuffer .writer ();
189+ Borders b =
190+ new Borders ()
191+ .lineStyle (lineStyle )
192+ .cornerStyle (cornerStyle )
193+ .style (Style .ofFgColor (Color .BasicColor .BRIGHT_MAGENTA ));
194+ b .render (helpBuffer );
195+ writer .fluent ().at (2 , 0 ).markup ("{brightmagenta}{+}[ {white}Help Page{-} ]" ).done ();
196+ Fluent help =
197+ Fluent .string ()
198+ .white ()
199+ .text ("q - quit\n " )
200+ .text ("h - toggle this help\n " )
201+ .text ("b - cycle border style\n " )
202+ .text ("s - cycle speeds" );
203+ writer .fluent ().at (3 , 2 ).block (help ).done ();
204+ }
205+
216206 private static void handleResize (Size newSize ) {
217207 maxX = Math .max (minX , newSize .width () - textSize .width () - 1 );
218208 maxY = Math .max (minY , newSize .height () - textSize .height () - 1 );
0 commit comments