Repository: sharkdp/bat
File: src/preprocessor.rs (Rust)

Code before:
                {
                    output.push(c)
                }
                // everything else
                chr => {
                    if use_caret {
                        // Replace LF, CR, NUL, BEL, BS, ESC using caret notation
                        match chr {
                            // line feed
                            '\x0A' => {
                                output.push_str("^J\x0A");
                                line_idx = 0;
                            }
                            // carriage return
                            '\x0D' => output.push_str("^M"),
                            // null
                            '\x00' => output.push_str("^@"),
                            // bell
                            '\x07' => output.push_str("^G"),
                            // backspace
                            '\x08' => output.push_str("^H"),
                            // escape
                            '\x1B' => output.push_str("^["),

                            // everything else
                            c => output.push_str(&c.escape_unicode().collect::<String>())
                        }
                    } else {
                        // Replace LF, CR, NUL, BEL, BS, ESC using unicode control pictures
                        match chr {
                            // line feed
                            '\x0A' => {
                                output.push_str("␊\x0A");
                                line_idx = 0;
                            }
                            // carriage return
                            '\x0D' => output.push('␍'),
                            // null
                            '\x00' => output.push('␀'),
                            // bell
                            '\x07' => output.push('␇'),
                            // backspace
                            '\x08' => output.push('␈'),
                            // escape
                            '\x1B' => output.push('␛'),

                            // everything else
                            c => output.push_str(&c.escape_unicode().collect::<String>())
                        }
                    }
                }

Review comment (refactor):
To reduce the diff and levels of indentation I would prefer if you added `if use_caret` to each existing character arm instead is of doing the "refactoring" in the current PR

Code after:
                    }
                }
                // line feed
                '\x0A' => {
                    output.push_str(match nonprintable_notation {
                        NonprintableNotation::Caret => "^J\x0A",
                        NonprintableNotation::Unicode => "␊\x0A",
                    });
                    line_idx = 0;
                }
                // carriage return
                '\x0D' => output.push_str(match nonprintable_notation {
                    NonprintableNotation::Caret => "^M",
                    NonprintableNotation::Unicode => "␍",
                }),
                // null
                '\x00' => output.push_str(match nonprintable_notation {
                    NonprintableNotation::Caret => "^@",
                    NonprintableNotation::Unicode => "␀",
                }),
                // bell
                '\x07' => output.push_str(match nonprintable_notation {
                    NonprintableNotation::Caret => "^G",
                    NonprintableNotation::Unicode => "␇",
                }),
                // backspace
                '\x08' => output.push_str(match nonprintable_notation {
                    NonprintableNotation::Caret => "^H",
                    NonprintableNotation::Unicode => "␈",
                }),
                // escape
                '\x1B' => output.push_str(match nonprintable_notation {
                    NonprintableNotation::Caret => "^[",
                    NonprintableNotation::Unicode => "␛",
                }),
                // printable ASCII
                c if c.is_ascii_alphanumeric()
                    || c.is_ascii_punctuation()
                    || c.is_ascii_graphic() =>
                {
                    output.push(c)
                }
                // everything else
                c => output.push_str(&c.escape_unicode().collect::<String>()),
            }
        } else {
            write!(output, "\\x{:02X}", input[idx]).ok();
            idx += 1;
        }
    }