diff --git a/src/cat.rs b/src/cat.rs index c9c38f6..2613166 100644 --- a/src/cat.rs +++ b/src/cat.rs @@ -16,6 +16,7 @@ pub struct Control { pub background_mode: bool, pub dialup_mode: bool, pub print_color: bool, + pub prompt_mode: bool, pub terminal_width_plus_one: u16, } @@ -27,7 +28,11 @@ pub fn print_lines_lol, S: AsRef>(lines: I, c: &mut C print_chars_lol(line.as_ref().chars().chain(Some('\n')), c, false); } if c.print_color { - print!("\x1b[39m"); + if c.prompt_mode { + print!("\\[\x1b[39m\\]"); + } else { + print!("\x1b[39m"); + } } } @@ -166,7 +171,11 @@ fn handle_newline( // the end of the program // We reset the background here because otherwise it bleeds all the way to the next line if c.background_mode { - print!("\x1b[49m"); + if c.prompt_mode { + print!("\\[\x1b[49m\\]"); + } else { + print!("\x1b[49m"); + } } } println!(); @@ -183,6 +192,9 @@ fn handle_newline( fn reset_colors(c: &Control) { if c.print_color { + if c.prompt_mode { + print!("\\["); + } // Reset the background color if c.background_mode { print!("\x1b[49m"); @@ -190,6 +202,9 @@ fn reset_colors(c: &Control) { // Reset the foreground color print!("\x1b[39m"); + if c.prompt_mode { + print!("\\]"); + } } } @@ -197,13 +212,24 @@ fn colored_print(character: char, c: &mut Control) { if c.background_mode { let bg = get_color_tuple(c); let fg = calc_fg_color(bg); - print!( - "\x1b[38;2;{};{};{};48;2;{};{};{}m{}", - fg.0, fg.1, fg.2, bg.0, bg.1, bg.2, character - ); + if c.prompt_mode { + print!( + "\\[\x1b[38;2;{};{};{};48;2;{};{};{}m\\]{}", + fg.0, fg.1, fg.2, bg.0, bg.1, bg.2, character + ); + } else { + print!( + "\x1b[38;2;{};{};{};48;2;{};{};{}m{}", + fg.0, fg.1, fg.2, bg.0, bg.1, bg.2, character + ); + } } else { let fg = get_color_tuple(c); - print!("\x1b[38;2;{};{};{}m{}", fg.0, fg.1, fg.2, character); + if c.prompt_mode { + print!("\\[\x1b[38;2;{};{};{}m\\]{}", fg.0, fg.1, fg.2, character); + } else { + print!("\x1b[38;2;{};{};{}m{}", fg.0, fg.1, fg.2, character); + } } } diff --git a/src/main.rs b/src/main.rs index f2e3ac8..871960c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,6 +55,7 @@ fn parse_cli_args(filename: &mut String) -> cat::Control { let print_color = matches.is_present("force-color") || std::io::stdout().is_terminal(); + let prompt_mode = matches.is_present("prompt-mode"); // If the terminal width is passed, use that. Else, get the size of the terminal. Else, use 0 (no overflow) let terminal_width: Result = matches.value_of("width") .unwrap_or("") @@ -77,6 +78,7 @@ fn parse_cli_args(filename: &mut String) -> cat::Control { background_mode: matches.is_present("background"), dialup_mode: matches.is_present("dialup"), print_color: print_color, + prompt_mode: prompt_mode, terminal_width_plus_one: terminal_width.wrapping_add(1), }; @@ -153,6 +155,13 @@ fn lolcat_clap_app() -> App<'static, 'static> { .help("Force color - Print escape sequences even if the output is not a terminal") .takes_value(false), ) + .arg( + Arg::with_name("prompt-mode") + .short("P") + .long("prompt-mode") + .help("Prompt mode - Adds bash non-printing character escape sequences to color codes") + .takes_value(false), + ) .arg( Arg::with_name("width") .long("terminal-width")