rustic_print 0.2.1

A versatile Rust library for enhancing console output. It offers a range of features to create a more engaging and informative command-line interface.
Documentation
1
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
//! # Rustic Print v2
//!
//! Rustic Print is a simple library for styling terminal output. It helps you render styled text blocks, interactive prompts, and tables.
//!
//! ## Message Display
//!
//! Most functions accept either a single string or a vector of strings, making it easy to print one-liners or multi-line messages.
//!
//! **Single String Example:**
//! ```rust
//! use rustic_print::RusticPrint;
//!
//! let printer = RusticPrint::new();
//! printer.success("Operation completed successfully!");
//! ```
//!
//! **Multiple Lines Example:**
//! ```rust
//! use rustic_print::RusticPrint;
//!
//! let printer = RusticPrint::new();
//! printer.info(vec![
//!     "Step 1: Initialization complete.",
//!     "Step 2: Processing data.",
//!     "Step 3: Operation finished.",
//! ]);
//! ```
//!
//! ## Tables
//!
//! Easily render tables by providing a vector of header strings and a vector of rows (each row is a vector of string slices).
//!
//! **Example:**
//! ```rust
//! use rustic_print::RusticPrint;
//!
//! let printer = RusticPrint::new();
//! let headers = vec!["Name", "Age", "Occupation"];
//! let rows = vec![
//!     vec!["Alice", "30", "Engineer"],
//!     vec!["Bob", "25", "Designer"],
//!     vec!["Charlie", "35", "Manager"],
//! ];
//! printer.table(headers, rows);
//! ```
//!
//! ## Confirmations
//!
//! Use the `confirm` function to prompt the user with a yes/no question. The default answer is provided as a boolean.
//!
//! **Example:**
//! ```rust
//! use rustic_print::RusticPrint;
//!
//! let printer = RusticPrint::new();
//! let proceed = printer.confirm("Do you want to continue?", true);
//!
//! if proceed {
//!     println!("Continuing...");
//! } else {
//!     println!("Operation cancelled.");
//! }
//! ```
//!
//! ## Interactive Choices
//!
//! The `choice` function displays a list of options and lets the user pick one interactively.
//!
//! **Example:**
//! ```rust
//! use rustic_print::RusticPrint;
//!
//! let printer = RusticPrint::new();
//! let options = ["Option 1", "Option 2", "Option 3"];
//! let selected = printer.choice("Select an option", &options, Some("Option 2"));
//!
//! println!("You selected: {}", selected);
//! ```
//!
//! ## Input with Validation
//!
//! The `ask` function not only prompts for input but can also enforce validation via a provided closure. If the input fails
//! validation, the prompt is repeated until a valid response is entered.
//!
//! **Example:**
//! ```rust
//! use rustic_print::RusticPrint;
//!
//! let printer = RusticPrint::new();
//!
//! // The validator requires the input to be at least 3 characters long.
//! let username = printer.ask(
//!     "Enter your username",
//!     Some("default_user"),
//!     Some(Box::new(|input| {
//!         if input.trim().len() >= 3 {
//!             Ok(())
//!         } else {
//!             Err("Username must be at least 3 characters long.".to_string())
//!         }
//!     }))
//! );
//!
//! println!("Your username is: {}", username);
//! ```
//! ## Available Functions
//!
//! The following functions are available on the [`RusticPrint`] struct. Click any item for more details:
//!
//! - [`RusticPrint::new`] - Create a new `RusticPrint` instance.
//! - [`RusticPrint::block`] - Print a styled text block.
//! - [`RusticPrint::underline_with_char`] - Underline a message with a repeated character.
//! - [`RusticPrint::title`] - Display a title with a styled underline.
//! - [`RusticPrint::section`] - Display a section header.
//! - [`RusticPrint::success`] - Print a success block with custom styling.
//! - [`RusticPrint::caution`] - Print a caution block with custom styling.
//! - [`RusticPrint::error`] - Print an error block with custom styling.
//! - [`RusticPrint::comment`] - Print a comment block prefixed with `//`.
//! - [`RusticPrint::warning`] - Print a warning block.
//! - [`RusticPrint::info`] - Print an informational block.
//! - [`RusticPrint::note`] - Print a note block with a custom prefix.
//! - [`RusticPrint::listing`] - Display a list of items.
//! - [`RusticPrint::text`] - Print wrapped text.
//! - [`RusticPrint::table`] - Render a table with headers and rows.
//! - [`RusticPrint::confirm`] - Prompt for a yes/no confirmation.
//! - [`RusticPrint::ask`] - Prompt for input with optional validation.
//! - [`RusticPrint::choice`] - Present an interactive choice prompt.
//!
//! For more details on each function, please refer to the full [API Reference](https://docs.rs/rustic_print/latest/rustic_print/).

pub mod block_options;
mod messages;
pub mod style_options;
pub mod table;

use crate::block_options::BlockOptions;
use crate::messages::Messages;
use crate::style_options::StyleOptions;
use crate::table::Table;
use crossterm::event::KeyModifiers;
use crossterm::style::{style, Print, PrintStyledContent};
use crossterm::{
    event,
    event::{read, Event, KeyCode},
    queue,
    style::{Color, ResetColor, SetBackgroundColor, SetForegroundColor, Stylize},
    terminal,
    terminal::{disable_raw_mode, enable_raw_mode},
};
use std::error::Error;
use std::io::{self, stdout, Write};
use std::time::Duration;
use textwrap::{fill, Options};

pub struct RusticPrint {}

impl RusticPrint {
    /// Creates a new instance of `RusticPrint`.
    ///
    /// # Returns
    ///
    /// A new `RusticPrint` instance.
    pub fn new() -> RusticPrint {
        RusticPrint {}
    }

    /// Prints a block of text using the provided messages and block options.
    ///
    /// This function converts the input into `Messages` and delegates rendering to the internal
    /// `render_block` function. It uses default block options if none are specified.
    ///
    /// # Arguments
    ///
    /// * `messages` - The message(s) to be printed; can be a single string or multiple strings.
    /// * `block_options` - Customization options for the block, including styling and prefix.
    ///
    /// # Panics
    ///
    /// Panics if rendering the block fails.
    pub fn block<T>(&self, messages: T, block_options: BlockOptions) -> ()
    where
        T: Into<Messages>,
    {
        self.render_block(messages, block_options)
            .expect("Failed to render b");
    }

    /// Renders a text block with the specified messages and block options.
    ///
    /// This is an internal helper function that:
    /// - Determines terminal width and wrapping.
    /// - Handles padding and styling.
    /// - Wraps the text appropriately.
    ///
    /// # Arguments
    ///
    /// * `message` - The message content, convertible into `Messages`.
    /// * `block_options` - Options that define the block's styling, prefix, and padding.
    ///
    /// # Returns
    ///
    /// A `Result` indicating success or failure in rendering the block.
    fn render_block<T>(&self, message: T, block_options: BlockOptions) -> Result<(), Box<dyn Error>>
    where
        T: Into<Messages>,
    {
        let message = message.into();
        let mut stdout = stdout();

        // Determine terminal width (default to 120 if unavailable) and cap the wrap width.
        let term_width = terminal::size().unwrap_or((120, 0)).0 as usize;
        let mut wrap_width = if term_width > 120 { 120 } else { term_width };
        if cfg!(windows) {
            wrap_width = wrap_width.saturating_sub(1);
        }

        // Start with an empty line.
        queue!(stdout, Print("\n"))?;

        // Prepare effective prefix (default to a single space if empty)
        let mut prefix = block_options.prefix.clone();
        if prefix.is_empty() {
            prefix = " ".to_string();
        }

        // Print top padding if enabled.
        if block_options.padding {
            print_padding_line(&mut stdout, wrap_width, &block_options, &prefix)?;
        }

        // Prepare indent strings.
        let block_type = block_options.block_type.clone().unwrap_or_default();
        let initial_indent = if !block_type.is_empty() {
            format!("{}[{}] ", prefix, block_type)
        } else {
            prefix.clone()
        };
        let subsequent_indent = format!(
            "{}{}",
            prefix,
            " ".repeat(initial_indent.len().saturating_sub(prefix.len()))
        );

        // Convert the message into a vector of strings.
        let messages_vec: Vec<String> = match message {
            Messages::Single(ref msg) => vec![msg.clone()],
            Messages::Multiple(ref msgs) => msgs.clone(),
        };

        for (i, msg) in messages_vec.iter().enumerate() {
            if i > 0 {
                print_padding_line(&mut stdout, wrap_width, &block_options, &prefix)?;
            }

            // For the first message, use the full initial indent; for others, use the subsequent indent.
            let effective_options = if i == 0 {
                Options::new(wrap_width)
                    .initial_indent(&initial_indent)
                    .subsequent_indent(&subsequent_indent)
            } else {
                Options::new(wrap_width)
                    .initial_indent(&subsequent_indent)
                    .subsequent_indent(&subsequent_indent)
            };

            // Wrap and print each line of the message.
            for line in fill(msg, &effective_options).lines() {
                styled_print_line(&mut stdout, line, wrap_width, &block_options)?;
            }
        }

        // Print bottom padding if enabled.
        if block_options.padding {
            print_padding_line(&mut stdout, wrap_width, &block_options, &prefix)?;
        }

        queue!(stdout, Print("\n"))?;
        stdout.flush()?;
        Ok(())
    }

    /// Renders a message with an underline composed of a repeated character.
    ///
    /// The function applies optional styling to both the message and its underline.
    ///
    /// # Arguments
    ///
    /// * `message` - The text to be underlined.
    /// * `underline_char` - The character used to create the underline.
    /// * `style_options` - Optional styling to apply to the text and underline.
    ///
    /// # Returns
    ///
    /// A `Result` indicating success or any encountered error.
    fn render_underline_with_char(
        &self,
        message: &str,
        underline_char: char,
        style_options: Option<StyleOptions>,
    ) -> Result<(), Box<dyn Error>> {
        let mut stdout = stdout();

        let mut message = style(message);
        let mut underline = style(underline_char.to_string().repeat(message.to_string().len()));

        if let Some(style_options) = style_options {
            if let Some(foreground) = style_options.foreground {
                message = message.with(foreground);
                underline = underline.with(foreground);
                queue!(stdout, SetForegroundColor(foreground))?;
            }
            if let Some(background) = style_options.background {
                message = message.on(background);
                underline = underline.on(background);
                queue!(stdout, SetBackgroundColor(background))?;
            }
        }

        queue!(
            stdout,
            PrintStyledContent(message),
            ResetColor,
            Print("\n"),
            PrintStyledContent(underline),
            ResetColor,
            Print("\n")
        )?;

        queue!(stdout, Print("\n"))?;
        stdout.flush()?;

        Ok(())
    }

    /// Underlines the given message with a repeated character.
    ///
    /// This function wraps around `render_underline_with_char` and panics if rendering fails.
    ///
    /// # Arguments
    ///
    /// * `message` - The text to underline.
    /// * `underline_char` - The character used to form the underline.
    /// * `style_options` - Optional styling parameters.
    pub fn underline_with_char(
        &self,
        message: &str,
        underline_char: char,
        style_options: Option<StyleOptions>,
    ) {
        self.render_underline_with_char(message, underline_char, style_options)
            .expect("Failed to render underline");
    }

    /// Displays a title by underlining the provided message with '=' characters.
    ///
    /// The title is styled with a dark green foreground.
    ///
    /// # Arguments
    ///
    /// * `message` - The title text.
    pub fn title(&self, message: &str) {
        self.underline_with_char(
            message,
            '=',
            Some(StyleOptions {
                foreground: Some(Color::DarkGreen),
                background: None,
            }),
        );
    }

    /// Displays a section header by underlining the message with '-' characters.
    ///
    /// The section header is styled with a dark green foreground.
    ///
    /// # Arguments
    ///
    /// * `message` - The section header text.
    pub fn section(&self, message: &str) {
        self.underline_with_char(
            message,
            '-',
            Some(StyleOptions {
                foreground: Some(Color::DarkGreen),
                background: None,
            }),
        );
    }

    /// Prints a success block with black text on a green background.
    ///
    /// The block is labeled with "OK" and includes padding.
    ///
    /// # Arguments
    ///
    /// * `messages` - The message(s) to display, convertible into `Messages`.
    ///
    /// # Panics
    ///
    /// Panics if rendering the success block fails.
    pub fn success<T>(&self, messages: T)
    where
        T: Into<Messages>,
    {
        self.render_block(
            messages,
            BlockOptions {
                style: Some(StyleOptions {
                    foreground: Some(Color::Black),
                    background: Some(Color::DarkGreen),
                }),
                block_type: Some("OK".to_string()),
                padding: true,
                ..Default::default()
            },
        )
        .expect("Failed to print success block");
    }

    /// Prints a caution block with grey text on a dark red background.
    ///
    /// The block is labeled "CAUTION" and uses a custom prefix along with padding.
    ///
    /// # Arguments
    ///
    /// * `messages` - The message(s) to display, convertible into `Messages`.
    ///
    /// # Panics
    ///
    /// Panics if rendering the caution block fails.
    pub fn caution<T>(&self, messages: T)
    where
        T: Into<Messages>,
    {
        self.render_block(
            messages,
            BlockOptions {
                style: Some(StyleOptions {
                    foreground: Some(Color::Grey),
                    background: Some(Color::DarkRed),
                }),
                block_type: Some("CAUTION".to_string()),
                prefix: " ! ".to_string(),
                padding: true,
                ..Default::default()
            },
        )
        .expect("Failed to print caution block");
    }

    /// Prints an error block with grey text on a dark red background.
    ///
    /// The block is labeled "ERROR" and includes padding.
    ///
    /// # Arguments
    ///
    /// * `messages` - The error message(s) to display, convertible into `Messages`.
    ///
    /// # Panics
    ///
    /// Panics if rendering the error block fails.
    pub fn error<T>(&self, messages: T)
    where
        T: Into<Messages>,
    {
        self.render_block(
            messages,
            BlockOptions {
                style: Some(StyleOptions {
                    foreground: Some(Color::Grey),
                    background: Some(Color::DarkRed),
                }),
                block_type: Some("ERROR".to_string()),
                prefix: " ".to_string(),
                padding: true,
                ..Default::default()
            },
        )
        .expect("Failed to print error block");
    }

    /// Prints a comment block prefixed with "//".
    ///
    /// Useful for displaying comments or annotations.
    ///
    /// # Arguments
    ///
    /// * `messages` - The comment message(s), convertible into `Messages`.
    ///
    /// # Panics
    ///
    /// Panics if rendering the comment block fails.
    pub fn comment<T>(&self, messages: T)
    where
        T: Into<Messages>,
    {
        self.render_block(
            messages,
            BlockOptions {
                prefix: " // ".to_string(),
                ..Default::default()
            },
        )
        .expect("Failed to print comment block");
    }

    /// Prints a warning block with black text on a dark yellow background.
    ///
    /// The block is labeled "WARNING" and includes padding.
    ///
    /// # Arguments
    ///
    /// * `messages` - The warning message(s) to display, convertible into `Messages`.
    ///
    /// # Panics
    ///
    /// Panics if rendering the warning block fails.
    pub fn warning<T>(&self, messages: T)
    where
        T: Into<Messages>,
    {
        self.render_block(
            messages,
            BlockOptions {
                style: Some(StyleOptions {
                    foreground: Some(Color::Black),
                    background: Some(Color::DarkYellow),
                }),
                block_type: Some("WARNING".to_string()),
                padding: true,
                ..Default::default()
            },
        )
        .expect("Failed to print comment block");
    }

    /// Prints an informational block with green text.
    ///
    /// The block is labeled "INFO" and includes padding.
    ///
    /// # Arguments
    ///
    /// * `messages` - The informational message(s) to display, convertible into `Messages`.
    ///
    /// # Panics
    ///
    /// Panics if rendering the info block fails.
    pub fn info<T>(&self, messages: T)
    where
        T: Into<Messages>,
    {
        self.render_block(
            messages,
            BlockOptions {
                style: Some(StyleOptions {
                    foreground: Some(Color::Green),
                    background: None,
                }),
                block_type: Some("INFO".to_string()),
                padding: true,
                ..Default::default()
            },
        )
        .expect("Failed to print info block");
    }

    /// Prints a note block with dark yellow text.
    ///
    /// The block is labeled "NOTE", uses a custom prefix, and does not include padding.
    ///
    /// # Arguments
    ///
    /// * `messages` - The note message(s) to display, convertible into `Messages`.
    ///
    /// # Panics
    ///
    /// Panics if rendering the note block fails
    pub fn note<T>(&self, messages: T)
    where
        T: Into<Messages>,
    {
        self.render_block(
            messages,
            BlockOptions {
                style: Some(StyleOptions {
                    foreground: Some(Color::DarkYellow),
                    background: None,
                }),
                block_type: Some("NOTE".to_string()),
                prefix: " ! ".to_string(),
                ..Default::default()
            },
        )
        .expect("Failed to print info block");
    }

    /// Displays a list of items, each preceded by an asterisk.
    ///
    /// # Arguments
    ///
    /// * `items` - A vector of items that implement the `Display` trait.
    ///
    /// # Panics
    ///
    /// Panics if printing any of the items fails.
    pub fn listing<T>(&self, items: Vec<T>)
    where
        T: std::fmt::Display,
    {
        let mut stdout = stdout();
        for (_, item) in items.iter().enumerate() {
            queue!(stdout, Print(format!("* {}\n", item))).expect("Failed to queue print");
        }

        stdout.flush().expect("Failed to flush stdout");
    }

    /// Prints text with automatic wrapping based on the terminal width.
    ///
    /// The text is indented with a single space on each line.
    ///
    /// # Arguments
    ///
    /// * `message` - The text to print, which implements the `Display` trait.
    ///
    /// # Panics
    ///
    /// Panics if printing the text fails.
    pub fn text<T>(&self, message: T)
    where
        T: std::fmt::Display,
    {
        let mut stdout = stdout();

        let term_width = terminal::size().unwrap_or((120, 0)).0 as usize;
        let options = Options::new(term_width.clone() - 1)
            .initial_indent(" ")
            .subsequent_indent(" ");

        for line in fill(&*message.to_string(), options).lines() {
            queue!(stdout, Print(line), Print("\n")).expect("Failed to queue print");
        }

        stdout.flush().expect("Failed to flush stdout");
    }

    /// Prints a table with the specified headers and rows.
    ///
    /// Internally, this function creates a `Table` instance and calls its `print_table` method.
    ///
    /// # Arguments
    ///
    /// * `headers` - A vector of string slices representing the table headers.
    /// * `rows` - A vector of rows, where each row is a vector of string slices.
    pub fn table(&self, headers: Vec<&str>, rows: Vec<Vec<&str>>) {
        let table = Table::new(headers, rows);
        table.print_table();
    }

    /// Prompts the user for confirmation with a yes/no question.
    ///
    /// The function enters raw mode, displays the question with default highlighting,
    /// and processes keyboard input until the user confirms with Enter.
    ///
    /// # Arguments
    ///
    /// * `question` - The question to present to the user.
    /// * `default` - The default answer if the user provides no input.
    ///
    /// # Returns
    ///
    /// Returns `true` if the user confirms (yes), otherwise `false`.
    pub fn confirm(&self, question: &str, default: bool) -> bool {
        let mut stdout = io::stdout();
        enable_raw_mode().expect("Failed to enable raw mode");

        let default_answer = if default { "yes" } else { "no" };

        // Using the Stylize trait to color the prompt.
        print!(
            "{} (yes/no) [{}]:\r\n > ",
            question.green(),
            default_answer.yellow()
        );
        stdout.flush().expect("Failed to flush stdout");

        let mut input = String::new();

        loop {
            if let Event::Key(key_event) = read().expect("Failed to read event") {
                match key_event.code {
                    KeyCode::Char(c) => {
                        print!("{}", c);
                        input.push(c);
                    }
                    KeyCode::Enter => {
                        println!();
                        break;
                    }
                    KeyCode::Backspace => {
                        if !input.is_empty() {
                            input.pop();
                            print!("\x08 \x08"); // Visual backspace.
                        }
                    }
                    _ => {}
                }
                stdout.flush().expect("Failed to flush stdout");
            }
        }

        disable_raw_mode().expect("Failed to disable raw mode");
        println!();

        if input.trim().is_empty() {
            default
        } else if input.trim().eq_ignore_ascii_case("yes") || input.trim().eq_ignore_ascii_case("y")
        {
            true
        } else {
            false
        }
    }

    /// Prompts the user with a question and returns the response.
    ///
    /// If a default value is provided and the user enters nothing, the default is used.
    /// An optional validator function can be supplied to validate the user's input.
    ///
    /// # Arguments
    ///
    /// * `question` - The question to display.
    /// * `default` - An optional default answer.
    /// * `validator` - An optional closure that validates the input and returns `Ok(())`
    ///   if valid, or an error message otherwise.
    ///
    /// # Returns
    ///
    /// Returns the user's input as a `String`.
    ///
    /// # Panics
    ///
    /// Panics if reading from stdin fails.
    pub fn ask(
        &self,
        question: &str,
        default: Option<&str>,
        validator: Option<Box<dyn Fn(&str) -> Result<(), String>>>,
    ) -> String {
        let mut stdout = io::stdout();

        loop {
            Self::ask_question(question, default);
            stdout.flush().expect("Failed to flush stdout");

            let mut input = String::new();
            io::stdin()
                .read_line(&mut input)
                .expect("Failed to read line");
            let input = input.trim();

            // Use the default value if the user provides no input.
            let answer = if input.is_empty() {
                default.unwrap_or("").to_string()
            } else {
                input.to_string()
            };

            // Validate the answer if a validator was provided.
            if let Some(ref validate) = validator {
                match validate(&answer) {
                    Ok(_) => return answer,
                    Err(err) => {
                        println!("{}", err.red());
                    }
                }
            } else {
                return answer;
            }
        }
    }

    /// Displays a prompt for the user, including an optional default value.
    ///
    /// This is a helper function for the `ask` method.
    ///
    /// # Arguments
    ///
    /// * `question` - The question to display.
    /// * `default_text` - An optional default text to show.
    fn ask_question(question: &str, default_text: Option<&str>) {
        let default_text = if let Some(dt) = default_text {
            format!(" [{}]", dt.dark_green())
        } else {
            String::new()
        };

        print!("{}{}:\n> ", question.dark_green(), default_text);
    }

    /// Presents a multiple-choice question to the user and returns the selected option.
    ///
    /// The function displays a list of choices, allows navigation via arrow keys, and handles
    /// input validation. It employs raw mode for interactive input.
    ///
    /// # Arguments
    ///
    /// * `question` - The prompt question.
    /// * `choices` - A slice of choices to select from.
    /// * `default` - An optional default choice.
    ///
    /// # Returns
    ///
    /// Returns the selected choice as a `String`.
    ///
    /// # Panics
    ///
    /// Panics if reading events or flushing output fails.
    pub fn choice(&self, question: &str, choices: &[&str], default: Option<&str>) -> String {
        loop {
            // Re-render the entire question block.
            let mut stdout = stdout();
            if default.is_some() {
                print!("{} [{}]:\n", question.green(), default.unwrap().green());
            } else {
                print!("{}:\n", question.green());
            }
            for (i, choice) in choices.iter().enumerate() {
                println!("  [{}] {}", i.to_string().green(), choice);
            }
            // Print the prompt line.
            print!("> ");
            stdout.flush().unwrap();
            // Save the current cursor row for the prompt.
            let (_, prompt_row) = crossterm::cursor::position().unwrap();

            // Enable raw mode for interactive input.
            enable_raw_mode().expect("Failed to enable raw mode");

            // Initialize the input buffer and selection.
            let mut input_buffer = String::new();
            // If the default text exactly matches one of the choices, use its index; otherwise, default to 0.
            let mut selected_index = choices
                .iter()
                .position(|&c| c == default.unwrap_or(""))
                .unwrap_or(0);

            // Immediately display the default selection.
            if default.is_some() {
                print!("{}", choices[selected_index]);
            }

            stdout.flush().unwrap();

            // Inner loop: process key events.
            loop {
                if event::poll(Duration::from_millis(500)).unwrap() {
                    match event::read().unwrap() {
                        Event::Key(key_event) => {
                            match key_event.code {
                                KeyCode::Enter => break,
                                // Tab completes the suggestion.
                                KeyCode::Tab => {
                                    input_buffer = choices[selected_index].to_string();
                                }
                                KeyCode::Up => {
                                    selected_index = if selected_index == 0 {
                                        choices.len() - 1
                                    } else {
                                        selected_index - 1
                                    };
                                    input_buffer.clear();
                                }
                                KeyCode::Down => {
                                    selected_index = (selected_index + 1) % choices.len();
                                    input_buffer.clear();
                                }
                                KeyCode::Char(c) => {
                                    if c == 'c'
                                        && key_event.modifiers.contains(KeyModifiers::CONTROL)
                                    {
                                        disable_raw_mode().unwrap();
                                        std::process::exit(0);
                                    }

                                    input_buffer.push(c);

                                    // If the buffer parses as a valid index, update selection.
                                    if let Ok(idx) = input_buffer.parse::<usize>() {
                                        if idx < choices.len() {
                                            selected_index = idx;
                                        }
                                    }
                                    // Otherwise, if the text starts any choice (case-insensitive), update selection.
                                    for (i, &choice) in choices.iter().enumerate() {
                                        if choice
                                            .to_lowercase()
                                            .starts_with(&input_buffer.to_lowercase())
                                        {
                                            selected_index = i;
                                            break;
                                        }
                                    }
                                }
                                KeyCode::Backspace => {
                                    input_buffer.pop();
                                }
                                _ => {}
                            }
                            // Update the prompt line.
                            use crossterm::{
                                cursor::MoveTo,
                                queue,
                                style::{
                                    Color, ResetColor, SetBackgroundColor, SetForegroundColor,
                                },
                                terminal::{Clear, ClearType},
                            };
                            queue!(
                                stdout,
                                MoveTo(2, prompt_row),
                                Clear(ClearType::UntilNewLine)
                            )
                            .unwrap();

                            if input_buffer.is_empty() {
                                // If nothing has been typed, display the full default/suggestion in normal style.
                                print!("{}", choices[selected_index]);
                            } else {
                                let suggestion = choices[selected_index];
                                // If the suggestion begins with the user's input (case-insensitive)
                                if suggestion
                                    .to_lowercase()
                                    .starts_with(&input_buffer.to_lowercase())
                                {
                                    let remainder = &suggestion[input_buffer.len()..];
                                    // Print the user's input as usual.
                                    print!("{}", input_buffer);
                                    // Print the remainder with grey background and white foreground.
                                    queue!(
                                        stdout,
                                        SetForegroundColor(Color::White),
                                        SetBackgroundColor(Color::Grey)
                                    )
                                    .unwrap();
                                    print!("{}", remainder);
                                    // Reset styling.
                                    queue!(stdout, ResetColor).unwrap();
                                } else {
                                    print!("{}", input_buffer);
                                }
                            }
                            stdout.flush().unwrap();
                        }
                        _ => {}
                    }
                }
            } // end inner input loop

            // Disable raw mode.
            disable_raw_mode().expect("Failed to disable raw mode");
            println!();

            // Determine the final selection.
            let final_choice = if input_buffer.is_empty() {
                choices[selected_index]
            } else if let Ok(idx) = input_buffer.parse::<usize>() {
                if idx < choices.len() {
                    choices[idx]
                } else {
                    ""
                }
            } else {
                let mut found = "";
                for &choice in choices {
                    if choice.to_lowercase() == input_buffer.to_lowercase() {
                        found = choice;
                        break;
                    }
                }
                found
            };

            // If the selection is invalid, render an error block and restart.
            if final_choice.is_empty() {
                self.error(format!(
                    "Invalid selection: \"{}\". Please enter a valid index or choice.",
                    input_buffer
                ));
                continue;
            } else if selected_index > choices.len() {
                self.error(format!(
                    "Invalid selection: \"{}\". Please enter a valid index or choice.",
                    input_buffer
                ));
                continue;
            } else {
                return final_choice.to_string();
            }
        }
    }
}

/// Prints a styled padding line that includes the provided prefix.
///
/// The line is constructed by concatenating the prefix and spaces to fill the wrap width.
/// Styling (background) is applied if specified in `block_options.style`.
///
/// # Arguments
///
/// * `stdout` - The mutable writer for stdout.
/// * `wrap_width` - The width used to compute the padding.
/// * `block_options` - Options that may contain style information.
/// * `prefix` - The prefix to include at the beginning of the line.
///
/// # Returns
///
/// A `Result` indicating success or any encountered error.
fn print_padding_line(
    stdout: &mut impl Write,
    wrap_width: usize,
    block_options: &BlockOptions,
    prefix: &str,
) -> Result<(), Box<dyn Error>> {
    let line = if wrap_width > prefix.len() {
        format!("{}{}", prefix, " ".repeat(wrap_width - prefix.len()))
    } else {
        prefix.to_string()
    };

    if let Some(style_cfg) = &block_options.style {
        let mut styled_line = style(line);

        if let Some(bg) = style_cfg.background {
            styled_line = styled_line.on(bg);
        }

        if let Some(fg) = style_cfg.foreground {
            styled_line = styled_line.with(fg);
        }

        queue!(
            stdout,
            PrintStyledContent(styled_line),
            ResetColor,
            Print("\r\n")
        )?;
        return Ok(());
    }
    queue!(stdout, Print(line), Print("\r\n"))?;
    Ok(())
}

/// Styles and prints a single line with appropriate end padding.
///
/// The line is padded with spaces to ensure it spans the full `wrap_width`, and styling (background
/// and foreground colors) is applied if specified in `block_options`.
///
/// # Arguments
///
/// * `stdout` - The mutable writer for stdout.
/// * `line` - The text line to be styled and printed.
/// * `wrap_width` - The total width for the line (used for padding).
/// * `block_options` - Options that may contain styling information.
///
/// # Returns
///
/// A `Result` indicating success or any encountered error.
fn styled_print_line(
    stdout: &mut impl Write,
    line: &str,
    wrap_width: usize,
    block_options: &BlockOptions,
) -> Result<(), Box<dyn Error>> {
    let end_padding = " ".repeat(wrap_width.saturating_sub(line.len()));
    let mut styled = style(format!("{}{}", line, end_padding));
    if let Some(style_cfg) = &block_options.style {
        if let Some(bg) = style_cfg.background {
            styled = styled.on(bg);
        }
        if let Some(fg) = style_cfg.foreground {
            styled = styled.with(fg);
        }
    }
    queue!(stdout, PrintStyledContent(styled), ResetColor, Print("\n"))?;
    Ok(())
}