11// Demonstrates the external break signal feature.
2- // A background thread sets the break signal after 3 seconds,
2+ //
3+ // A background thread sets the break signal every 5 seconds,
34// causing `read_line()` to return `Signal::ExternalBreak` with
4- // the current buffer contents.
5+ // the current buffer contents. The example then resumes editing
6+ // by calling `read_line()` again — the prompt stays on the same
7+ // line thanks to suspended-state preservation.
58//
69// To run:
710// cargo run --example break_signal
@@ -23,15 +26,15 @@ fn main() -> io::Result<()> {
2326 let mut line_editor = Reedline :: create ( ) . with_break_signal ( break_signal. clone ( ) ) ;
2427 let prompt = DefaultPrompt :: default ( ) ;
2528
26- // Spawn a thread that triggers the break signal after 3 seconds
29+ // Spawn a thread that triggers the break signal periodically
2730 let signal = break_signal. clone ( ) ;
28- thread:: spawn ( move || {
29- thread:: sleep ( Duration :: from_secs ( 3 ) ) ;
30- println ! ( "\n [background] Setting break signal in..." ) ;
31+ thread:: spawn ( move || loop {
32+ thread:: sleep ( Duration :: from_secs ( 5 ) ) ;
3133 signal. store ( true , Ordering :: Relaxed ) ;
3234 } ) ;
3335
34- println ! ( "Type something. The break signal will fire in 3 seconds..." ) ;
36+ println ! ( "Type something. The break signal fires every 5 seconds." ) ;
37+ println ! ( "The prompt will stay in place after each ExternalBreak.\n " ) ;
3538
3639 loop {
3740 let sig = line_editor. read_line ( & prompt) ?;
@@ -44,8 +47,13 @@ fn main() -> io::Result<()> {
4447 break Ok ( ( ) ) ;
4548 }
4649 Signal :: ExternalBreak ( buffer) => {
47- println ! ( "\n ExternalBreak received! Buffer contents: {buffer:?}" ) ;
48- break Ok ( ( ) ) ;
50+ // The buffer contents are preserved across the break.
51+ // Simply call read_line() again to let the user continue
52+ // editing — the prompt stays on the same line as long as
53+ // nothing is printed between the break and the next
54+ // read_line() call.
55+ eprintln ! ( "[break] buffer: {buffer:?}" ) ;
56+ continue ;
4957 }
5058 _ => { }
5159 }
0 commit comments