@@ -14,7 +14,7 @@ One of JLine's most powerful features is the ability to print text above the cur
14
14
15
15
The simplest way to print above the current line is to use the ` printAbove ` method of the ` LineReader ` class:
16
16
17
- ``` java
17
+ ``` java title="PrintAboveExample.java"
18
18
import org.jline.reader.LineReader ;
19
19
import org.jline.reader.LineReaderBuilder ;
20
20
import org.jline.terminal.Terminal ;
@@ -26,19 +26,20 @@ public class PrintAboveExample {
26
26
LineReader reader = LineReaderBuilder . builder()
27
27
.terminal(terminal)
28
28
.build();
29
-
29
+
30
30
// Start a background thread to print messages
31
31
new Thread (() - > {
32
32
try {
33
33
for (int i = 0 ; i < 10 ; i++ ) {
34
34
Thread . sleep(1000 );
35
+ // highlight-next-line
35
36
reader. printAbove(" Notification #" + i);
36
37
}
37
38
} catch (Exception e) {
38
39
e. printStackTrace();
39
40
}
40
41
}). start();
41
-
42
+
42
43
// Read input normally
43
44
while (true ) {
44
45
String line = reader. readLine(" prompt> " );
@@ -54,7 +55,7 @@ In this example, notifications will appear above the input line, and the user ca
54
55
55
56
For more control, you can use the ` PrintAboveWriter ` class:
56
57
57
- ``` java
58
+ ``` java title="PrintAboveWriterExample.java"
58
59
import org.jline.reader.LineReader ;
59
60
import org.jline.reader.LineReaderBuilder ;
60
61
import org.jline.terminal.Terminal ;
@@ -72,24 +73,26 @@ public class PrintAboveWriterExample {
72
73
LineReader reader = LineReaderBuilder . builder()
73
74
.terminal(terminal)
74
75
.build();
75
-
76
+
77
+ // highlight-start
76
78
// Create a PrintAboveWriter
77
- PrintWriter writer = new PrintAboveWriter (reader. getTerminal(),
79
+ PrintWriter writer = new PrintAboveWriter (reader. getTerminal(),
78
80
reader:: printAbove);
79
-
81
+ // highlight-end
82
+
80
83
// Start a background thread to print messages
81
84
new Thread (() - > {
82
85
try {
83
86
for (int i = 0 ; i < 10 ; i++ ) {
84
87
Thread . sleep(1000 );
85
-
88
+
86
89
// Create a styled message
87
90
AttributedStringBuilder asb = new AttributedStringBuilder ();
88
91
asb. style(AttributedStyle . DEFAULT. foreground(AttributedStyle . GREEN ))
89
92
.append(" Notification #" )
90
93
.append(String . valueOf(i))
91
94
.style(AttributedStyle . DEFAULT );
92
-
95
+
93
96
// Print the message above the current line
94
97
writer. println(asb. toAnsi(terminal));
95
98
writer. flush();
@@ -98,7 +101,7 @@ public class PrintAboveWriterExample {
98
101
e. printStackTrace();
99
102
}
100
103
}). start();
101
-
104
+
102
105
// Read input normally
103
106
while (true ) {
104
107
String line = reader. readLine(" prompt> " );
@@ -126,7 +129,7 @@ JLine's Status feature allows you to display persistent status information at th
126
129
127
130
### Basic Status Usage
128
131
129
- ``` java
132
+ ``` java title="StatusExample.java" showLineNumbers
130
133
import org.jline.reader.LineReader ;
131
134
import org.jline.reader.LineReaderBuilder ;
132
135
import org.jline.terminal.Terminal ;
@@ -141,19 +144,21 @@ public class StatusExample {
141
144
LineReader reader = LineReaderBuilder . builder()
142
145
.terminal(terminal)
143
146
.build();
144
-
147
+
145
148
// Create a Status instance
146
149
Status status = Status . getStatus(terminal);
147
150
if (status != null ) {
151
+ // highlight-start
148
152
// Update the status line
149
153
status. update(new AttributedStringBuilder ()
150
154
.style(AttributedStyle . DEFAULT. foreground(AttributedStyle . BLUE ))
151
155
.append(" Connected to server | " )
152
156
.style(AttributedStyle . DEFAULT. foreground(AttributedStyle . GREEN ))
153
157
.append(" 3 tasks running" )
154
158
.toAttributedString());
159
+ // highlight-end
155
160
}
156
-
161
+
157
162
// Read input normally
158
163
while (true ) {
159
164
String line = reader. readLine(" prompt> " );
@@ -175,7 +180,7 @@ new Thread(() -> {
175
180
while (true ) {
176
181
Thread . sleep(2000 );
177
182
taskCount = (taskCount + 1 ) % 10 ;
178
-
183
+
179
184
if (status != null ) {
180
185
status. update(new AttributedStringBuilder ()
181
186
.style(AttributedStyle . DEFAULT. foreground(AttributedStyle . BLUE ))
@@ -225,7 +230,7 @@ Tailtips provide contextual hints or suggestions that appear after the cursor. T
225
230
226
231
### Basic Tailtips Usage
227
232
228
- ``` java
233
+ ``` java title="TailtipExample.java" showLineNumbers
229
234
import org.jline.reader.LineReader ;
230
235
import org.jline.reader.LineReaderBuilder ;
231
236
import org.jline.reader.impl.DefaultParser ;
@@ -238,17 +243,19 @@ public class TailtipExample {
238
243
public static void main (String [] args ) throws Exception {
239
244
Terminal terminal = TerminalBuilder . builder(). build();
240
245
DefaultParser parser = new DefaultParser ();
241
-
246
+
242
247
LineReader reader = LineReaderBuilder . builder()
243
248
.terminal(terminal)
244
249
.parser(parser)
250
+ // highlight-next-line
245
251
.variable(LineReader . TAILTIP_ENABLED , true )
246
252
.build();
247
-
253
+
248
254
// Read input with tailtips
249
255
while (true ) {
250
- String line = reader. readLine(" prompt> " , null ,
251
- (String ) null , null ,
256
+ // error-start
257
+ String line = reader. readLine(" prompt> " , null ,
258
+ (String ) null , null ,
252
259
s - > {
253
260
// This function provides the tailtip based on current input
254
261
if (s. startsWith(" help" )) {
@@ -264,7 +271,8 @@ public class TailtipExample {
264
271
}
265
272
return null ;
266
273
});
267
-
274
+ // error-end
275
+
268
276
System . out. println(" You entered: " + line);
269
277
}
270
278
}
@@ -285,13 +293,13 @@ commandHelp.put("list", "[pattern] - List available resources");
285
293
286
294
// Read input with command-specific tailtips
287
295
while (true ) {
288
- String line = reader. readLine(" prompt> " , null ,
289
- (String ) null , null ,
296
+ String line = reader. readLine(" prompt> " , null ,
297
+ (String ) null , null ,
290
298
s - > {
291
299
// Extract the command part
292
300
String [] parts = s. split(" \\ s+" , 2 );
293
301
String cmd = parts[0 ];
294
-
302
+
295
303
// Look up help for this command
296
304
String help = commandHelp. get(cmd);
297
305
if (help != null ) {
@@ -302,7 +310,7 @@ while (true) {
302
310
}
303
311
return null ;
304
312
});
305
-
313
+
306
314
System . out. println(" You entered: " + line);
307
315
}
308
316
```
@@ -314,19 +322,19 @@ You can provide more sophisticated tailtips based on the current parsing context
314
322
``` java
315
323
// Read input with context-aware tailtips
316
324
while (true ) {
317
- String line = reader. readLine(" prompt> " , null ,
318
- (String ) null , null ,
325
+ String line = reader. readLine(" prompt> " , null ,
326
+ (String ) null , null ,
319
327
s - > {
320
328
try {
321
329
// Parse the current line
322
330
ParsedLine pl = parser. parse(s, s. length());
323
331
String word = pl. word();
324
332
List<String > words = pl. words();
325
-
333
+
326
334
// Command-specific help based on context
327
335
if (words. size() >= 1 ) {
328
336
String cmd = words. get(0 );
329
-
337
+
330
338
if (cmd. equals(" connect" )) {
331
339
if (words. size() == 1 ) {
332
340
// Just the command
@@ -348,7 +356,7 @@ while (true) {
348
356
}
349
357
return null ;
350
358
});
351
-
359
+
352
360
System . out. println(" You entered: " + line);
353
361
}
354
362
```
@@ -376,16 +384,16 @@ public class InteractiveExample {
376
384
public static void main (String [] args ) throws Exception {
377
385
Terminal terminal = TerminalBuilder . builder(). build();
378
386
DefaultParser parser = new DefaultParser ();
379
-
387
+
380
388
LineReader reader = LineReaderBuilder . builder()
381
389
.terminal(terminal)
382
390
.parser(parser)
383
391
.variable(LineReader . TAILTIP_ENABLED , true )
384
392
.build();
385
-
393
+
386
394
// Set up PrintAboveWriter
387
395
PrintWriter writer = new PrintAboveWriter (terminal, reader:: printAbove);
388
-
396
+
389
397
// Set up Status
390
398
Status status = Status . getStatus(terminal);
391
399
if (status != null ) {
@@ -394,29 +402,29 @@ public class InteractiveExample {
394
402
.append(" Ready" )
395
403
.toAttributedString());
396
404
}
397
-
405
+
398
406
// Command help for tailtips
399
407
Map<String , String > commandHelp = new HashMap<> ();
400
408
commandHelp. put(" help" , " [command] - Display help for command" );
401
409
commandHelp. put(" connect" , " <host> <port> - Connect to server" );
402
410
commandHelp. put(" disconnect" , " - Disconnect from server" );
403
411
commandHelp. put(" list" , " [pattern] - List available resources" );
404
-
412
+
405
413
// Start a background thread for notifications
406
414
new Thread (() - > {
407
415
try {
408
416
for (int i = 0 ; i < 10 ; i++ ) {
409
417
Thread . sleep(3000 );
410
-
418
+
411
419
// Print notification above
412
420
AttributedStringBuilder asb = new AttributedStringBuilder ();
413
421
asb. style(AttributedStyle . DEFAULT. foreground(AttributedStyle . GREEN ))
414
422
.append(" System notification #" )
415
423
.append(String . valueOf(i));
416
-
424
+
417
425
writer. println(asb. toAnsi(terminal));
418
426
writer. flush();
419
-
427
+
420
428
// Update status
421
429
if (status != null ) {
422
430
status. update(new AttributedStringBuilder ()
@@ -431,16 +439,16 @@ public class InteractiveExample {
431
439
e. printStackTrace();
432
440
}
433
441
}). start();
434
-
442
+
435
443
// Main input loop with tailtips
436
444
while (true ) {
437
- String line = reader. readLine(" prompt> " , null ,
438
- (String ) null , null ,
445
+ String line = reader. readLine(" prompt> " , null ,
446
+ (String ) null , null ,
439
447
s - > {
440
448
// Extract the command part
441
449
String [] parts = s. split(" \\ s+" , 2 );
442
450
String cmd = parts[0 ];
443
-
451
+
444
452
// Look up help for this command
445
453
String help = commandHelp. get(cmd);
446
454
if (help != null ) {
@@ -451,9 +459,9 @@ public class InteractiveExample {
451
459
}
452
460
return null ;
453
461
});
454
-
462
+
455
463
System . out. println(" You entered: " + line);
456
-
464
+
457
465
// Update status based on command
458
466
if (status != null ) {
459
467
status. update(new AttributedStringBuilder ()
0 commit comments