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

Code before:
            self.panel_width + 2
        } else {
            self.panel_width
        }
    }

    fn print_header_component_indent(&mut self, handle: &mut OutputHandle) -> Result<()> {
        if self.config.style_components.grid() {
            write!(
                handle,
                "{}{}",
                " ".repeat(self.panel_width),
                self.colors
                    .grid
                    .paint(if self.panel_width > 0 { "│ " } else { "" }),
            )
        } else {
            write!(handle, "{}", " ".repeat(self.panel_width))
        }
    }

    fn print_header_component_with_indent(
        &mut self,
        handle: &mut OutputHandle,
        content: &str,
    ) -> Result<()> {
        self.print_header_component_indent(handle)?;
        writeln!(handle, "{content}")
    }

    fn print_header_multiline_component(
        &mut self,
        handle: &mut OutputHandle,
        content: &str,
    ) -> Result<()> {
        let content_width = self.config.term_width - self.get_header_component_indent_length();
        let mut content_graphemes: Vec<&str> = content.graphemes(true).collect();
        while content_graphemes.len() > content_width {
            let (content_line, remaining) = content_graphemes.split_at(content_width);
            self.print_header_component_with_indent(handle, content_line.join("").as_str())?;
            content_graphemes = remaining.iter().cloned().collect();
        }
        self.print_header_component_with_indent(handle, content_graphemes.join("").as_str())
    }

    fn highlight_regions_for_line<'b>(
        &mut self,
        line: &'b str,
    ) -> Result<Vec<(syntect::highlighting::Style, &'b str)>> {
        let highlighter_from_set = match self.highlighter_from_set {
            Some(ref mut highlighter_from_set) => highlighter_from_set,

Review comment (bug):
thanks for taking a look at fixing this. I wonder if it makes any sense to have an optimization that it only splits into graphemes if the width would overflow? Because for short filenames, I think it can just continue to print it wholly without splitting and then rejoining into a string

Code after:
            )
        } else {
            write!(handle, "{}", " ".repeat(self.panel_width))
        }
    }

    fn print_header_component_with_indent(
        &mut self,
        handle: &mut OutputHandle,
        content: &str,
    ) -> Result<()> {
        self.print_header_component_indent(handle)?;
        writeln!(handle, "{content}")
    }

    fn print_header_multiline_component(
        &mut self,
        handle: &mut OutputHandle,
        content: &str,
    ) -> Result<()> {
        let content_width = self.config.term_width - self.get_header_component_indent_length();
        if content.chars().count() <= content_width {
            return self.print_header_component_with_indent(handle, content);
        }

        let mut content_graphemes: Vec<&str> = content.graphemes(true).collect();
        while content_graphemes.len() > content_width {
            let (content_line, remaining) = content_graphemes.split_at(content_width);
            self.print_header_component_with_indent(handle, content_line.join("").as_str())?;
            content_graphemes = remaining.iter().cloned().collect();
        }
        self.print_header_component_with_indent(handle, content_graphemes.join("").as_str())
    }

    fn highlight_regions_for_line<'b>(
        &mut self,
        line: &'b str,
    ) -> Result<Vec<(syntect::highlighting::Style, &'b str)>> {
        let highlighter_from_set = match self.highlighter_from_set {
            Some(ref mut highlighter_from_set) => highlighter_from_set,
            _ => return Ok(vec![(EMPTY_SYNTECT_STYLE, line)]),
        };

        // skip syntax highlighting on long lines
        let too_long = line.len() > 1024 * 16;

        let for_highlighting: &str = if too_long { "\n" } else { line };

        let mut highlighted_line = highlighter_from_set
            .highlighter
            .highlight_line(for_highlighting, highlighter_from_set.syntax_set)?;