Repository: alacritty/alacritty
File: alacritty_terminal/src/term/mod.rs (Rust)

Code before:
        trace!("Saving cursor position");

        self.grid.saved_cursor = self.grid.cursor;
    }

    #[inline]
    fn restore_cursor_position(&mut self) {
        trace!("Restoring cursor position");

        self.grid.cursor = self.grid.saved_cursor;
    }

    #[inline]
    fn clear_line(&mut self, mode: ansi::LineClearMode) {
        trace!("Clearing line: {:?}", mode);

        let cursor = self.grid.cursor;
        let num_cols = self.grid.num_cols();
        let selection_range = self.selection.as_ref().and_then(|s| s.to_range(&self));
        let buffer_cursor = self.grid.buffer_cursor();

        let cols = match mode {
            ansi::LineClearMode::Right => {
                let row = &mut self.grid[cursor.point.line];
                let cols = cursor.point.col..num_cols;
                for cell in &mut row[cols.clone()] {
                    cell.reset(&cursor.template);
                }
                cols
            },
            ansi::LineClearMode::Left => {
                let row = &mut self.grid[cursor.point.line];
                let cols = Column(0)..Column(cursor.point.col.0 + 1);
                for cell in &mut row[cols.clone()] {
                    cell.reset(&cursor.template);
                }
                cols
            },
            ansi::LineClearMode::All => {
                let row = &mut self.grid[cursor.point.line];
                let cols = Column(0)..num_cols;
                for cell in &mut row[cols.clone()] {
                    cell.reset(&cursor.template);
                }
                cols
            },
        };

        if let Some(true) = selection_range
            .as_ref()
            .map(|range| range.contains_cols_on_buffer_line(buffer_cursor.line, cols))

Review comment (refactor):
From here on out all branches basically do the same thing. So if we need to compute the cols anyways and return them, we can probably extract everything other than just the cols computation to after the match?

If `contains_cols_on_buffer_line` takes a reference to a range, we should also be able to get rid of the `clone`s? Even though the clones are extremely cheap of course.

Code after:
        self.grid.saved_cursor = self.grid.cursor;
    }

    #[inline]
    fn restore_cursor_position(&mut self) {
        trace!("Restoring cursor position");

        self.grid.cursor = self.grid.saved_cursor;
    }

    #[inline]
    fn clear_line(&mut self, mode: ansi::LineClearMode) {
        trace!("Clearing line: {:?}", mode);

        let cursor = self.grid.cursor;
        match mode {
            ansi::LineClearMode::Right => {
                let row = &mut self.grid[cursor.point.line];
                for cell in &mut row[cursor.point.col..] {
                    cell.reset(&cursor.template);
                }
            },
            ansi::LineClearMode::Left => {
                let row = &mut self.grid[cursor.point.line];
                for cell in &mut row[..=cursor.point.col] {
                    cell.reset(&cursor.template);
                }
            },
            ansi::LineClearMode::All => {
                let row = &mut self.grid[cursor.point.line];
                for cell in &mut row[..] {
                    cell.reset(&cursor.template);
                }
            },
        }

        let cursor_buffer_line = self.grid.cursor_buffer_point().line;
        self.selection = self
            .selection
            .take()
            .filter(|s| !s.intersects_range(cursor_buffer_line..=cursor_buffer_line));
    }

    /// Set the indexed color value.
    #[inline]
    fn set_color(&mut self, index: usize, color: Rgb) {
        trace!("Setting color[{}] = {:?}", index, color);
        self.colors[index] = color;
        self.color_modified[index] = true;
    }