Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions src/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -27,7 +28,11 @@ pub fn print_lines_lol<I: Iterator<Item = S>, S: AsRef<str>>(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");
}
}
}

Expand Down Expand Up @@ -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!();
Expand All @@ -183,27 +192,44 @@ 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");
}

// Reset the foreground color
print!("\x1b[39m");
if c.prompt_mode {
print!("\\]");
}
}
}

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);
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16, ParseIntError> = matches.value_of("width")
.unwrap_or("")
Expand All @@ -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),
};

Expand Down Expand Up @@ -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")
Expand Down