Repository: sxyazi/yazi
File: yazi-plugin/src/external/highlighter.rs (Rust)

Code before:
			buf_line.push(byte);
			buf_char.push(byte);

			if let Ok(char) = String::from_utf8(buf_char.clone()) {
				if ",.; ".contains(&char) {
					last_break_char_idx = i + 1
				}

				let buf_line_width = String::from_utf8_lossy(&buf_line).width();
				if buf_line_width < width {
					buf_char.clear();
					continue;
				}

				if last_break_char_idx == 0 {
					// no spaces in line, break right here
					match buf_line_width.cmp(&width) {
						std::cmp::Ordering::Equal => {
							resulted_lines.push(buf_line.clone());
							buf_line.clear();
							last_break_idx = i + 1;
						}
						std::cmp::Ordering::Greater => {
							let take_bytes = buf_line.len() - buf_char.len();
							buf_line = buf_line[..take_bytes].to_vec();
							resulted_lines.push(buf_line.clone());
							buf_line.clear();
							buf_line.extend_from_slice(&buf_char);
							last_break_idx = i - take_bytes + 1;
						}
						_ => {}
					}
				} else {
					let break_idx = last_break_char_idx - last_break_idx;
					resulted_lines.push(buf_line[..break_idx].to_vec());
					buf_line = if last_break_char_idx == buf_line.len() {
						vec![]
					} else {
						buf_line[break_idx..].to_vec()
					};
					last_break_idx = last_break_char_idx;
				}
				last_break_char_idx = 0;
				buf_char.clear();
			}
		}
		if !buf_line.is_empty() && String::from_utf8_lossy(&buf_line) != "\n" {
			resulted_lines.push(buf_line);
		}

		resulted_lines

Review comment (suggestion):
Please remove all `.clone()` and use `mem::take()` instead.

Code after:
		if color.a == 0 {
			// Themes can specify one of the user-configurable terminal colors by
			// encoding them as #RRGGBBAA with AA set to 00 (transparent) and RR set
			// to the 8-bit color palette number. The built-in themes ansi, base16,
			// and base16-256 use this.
			Some(match color.r {
				// For the first 8 colors, use the Color enum to produce ANSI escape
				// sequences using codes 30-37 (foreground) and 40-47 (background).
				// For example, red foreground is \x1b[31m. This works on terminals
				// without 256-color support.
				0x00 => ratatui::style::Color::Black,
				0x01 => ratatui::style::Color::Red,
				0x02 => ratatui::style::Color::Green,
				0x03 => ratatui::style::Color::Yellow,
				0x04 => ratatui::style::Color::Blue,
				0x05 => ratatui::style::Color::Magenta,
				0x06 => ratatui::style::Color::Cyan,
				0x07 => ratatui::style::Color::White,
				// For all other colors, use Fixed to produce escape sequences using
				// codes 38;5 (foreground) and 48;5 (background). For example,
				// bright red foreground is \x1b[38;5;9m. This only works on
				// terminals with 256-color support.
				//
				// TODO: When ansi_term adds support for bright variants using codes
				// 90-97 (foreground) and 100-107 (background), we should use those
				// for values 0x08 to 0x0f and only use Fixed for 0x10 to 0xff.
				n => ratatui::style::Color::Indexed(n),
			})
		} else if color.a == 1 {
			// Themes can specify the terminal's default foreground/background color
			// (i.e. no escape sequence) using the encoding #RRGGBBAA with AA set to
			// 01. The built-in theme ansi uses this.
			None
		} else {
			Some(ratatui::style::Color::Rgb(color.r, color.g, color.b))
		}
	}
}