in crates/alacritty_terminal/src/grid/resize.rs [261:398]
fn shrink_columns(&mut self, reflow: bool, columns: usize) {
self.columns = columns;
// Remove the linewrap special case, by moving the cursor outside of the grid.
if self.cursor.input_needs_wrap && reflow {
self.cursor.input_needs_wrap = false;
self.cursor.point.column += 1;
}
let mut new_raw = Vec::with_capacity(self.raw.len());
let mut buffered: Option<Vec<T>> = None;
let mut rows = self.raw.take_all();
for (i, mut row) in rows.drain(..).enumerate().rev() {
// Append lines left over from the previous row.
if let Some(buffered) = buffered.take() {
// Add a column for every cell added before the cursor, if it goes beyond the new
// width it is then later reflown.
let cursor_buffer_line = self.lines - self.cursor.point.line.0 as usize - 1;
if i == cursor_buffer_line {
self.cursor.point.column += buffered.len();
}
row.append_front(buffered);
}
loop {
// Remove all cells which require reflowing.
let mut wrapped = match row.shrink(columns) {
Some(wrapped) if reflow => wrapped,
_ => {
let cursor_buffer_line = self.lines - self.cursor.point.line.0 as usize - 1;
if reflow && i == cursor_buffer_line && self.cursor.point.column > columns {
// If there are empty cells before the cursor, we assume it is explicit
// whitespace and need to wrap it like normal content.
Vec::new()
} else {
// Since it fits, just push the existing line without any reflow.
new_raw.push(row);
break;
}
},
};
// Insert spacer if a wide char would be wrapped into the last column.
if row.len() >= columns && row[Column(columns - 1)].flags().contains(ShellFlags::WIDE_CHAR) {
let mut spacer = T::default();
spacer.flags_mut().insert(ShellFlags::LEADING_WIDE_CHAR_SPACER);
let wide_char = mem::replace(&mut row[Column(columns - 1)], spacer);
wrapped.insert(0, wide_char);
}
// Remove wide char spacer before shrinking.
let len = wrapped.len();
if len > 0 && wrapped[len - 1].flags().contains(ShellFlags::LEADING_WIDE_CHAR_SPACER) {
if len == 1 {
row[Column(columns - 1)].flags_mut().insert(ShellFlags::WRAPLINE);
new_raw.push(row);
break;
} else {
// Remove the leading spacer from the end of the wrapped row.
wrapped[len - 2].flags_mut().insert(ShellFlags::WRAPLINE);
wrapped.truncate(len - 1);
}
}
new_raw.push(row);
// Set line as wrapped if cells got removed.
if let Some(cell) = new_raw.last_mut().and_then(|r| r.last_mut()) {
cell.flags_mut().insert(ShellFlags::WRAPLINE);
}
if wrapped
.last()
.is_some_and(|c| c.flags().contains(ShellFlags::WRAPLINE) && i >= 1)
&& wrapped.len() < columns
{
// Make sure previous wrap flag doesn't linger around.
if let Some(cell) = wrapped.last_mut() {
cell.flags_mut().remove(ShellFlags::WRAPLINE);
}
// Add removed cells to start of next row.
buffered = Some(wrapped);
break;
} else {
// Reflow cursor if a line below it is deleted.
let cursor_buffer_line = self.lines - self.cursor.point.line.0 as usize - 1;
if (i == cursor_buffer_line && self.cursor.point.column < columns) || i < cursor_buffer_line {
self.cursor.point.line = max(self.cursor.point.line - 1, Line(0));
}
// Reflow the cursor if it is on this line beyond the width.
if i == cursor_buffer_line && self.cursor.point.column >= columns {
// Since only a single new line is created, we subtract only `columns`
// from the cursor instead of reflowing it completely.
self.cursor.point.column -= columns;
}
// Make sure new row is at least as long as new width.
let occ = wrapped.len();
if occ < columns {
wrapped.resize_with(columns, T::default);
}
row = Row::from_vec(wrapped, occ);
if i < self.display_offset {
// Since we added a new line, rotate up the viewport.
self.display_offset += 1;
}
}
}
}
// Reverse iterator and use it as the new grid storage.
let mut reversed: Vec<Row<T>> = new_raw.drain(..).rev().collect();
reversed.truncate(self.max_scroll_limit + self.lines);
self.raw.replace_inner(reversed);
// Reflow the primary cursor, or clamp it if reflow is disabled.
if !reflow {
self.cursor.point.column = min(self.cursor.point.column, Column(columns - 1));
} else if self.cursor.point.column == columns
&& !self[self.cursor.point.line][Column(columns - 1)]
.flags()
.contains(ShellFlags::WRAPLINE)
{
self.cursor.input_needs_wrap = true;
self.cursor.point.column -= 1;
} else {
self.cursor.point = self.cursor.point.grid_clamp(self, Boundary::Cursor);
}
// Clamp the saved cursor to the grid.
self.saved_cursor.point.column = min(self.saved_cursor.point.column, Column(columns - 1));
}