Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pop-os/cosmic-term
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Apr 26, 2024
2 parents f720e63 + 3ccfa45 commit 4cec088
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 218 deletions.
10 changes: 5 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,11 @@ impl Config {
let color_schemes = self.color_schemes(color_scheme_kind);
let mut color_scheme_names =
Vec::<(String, ColorSchemeId)>::with_capacity(color_schemes.len());
for (color_scheme_id, color_scheme) in color_schemes.iter() {
for (color_scheme_id, color_scheme) in color_schemes {
let mut name = color_scheme.name.clone();

let mut copies = 1;
while color_scheme_names.iter().find(|x| x.0 == name).is_some() {
while color_scheme_names.iter().any(|x| x.0 == name) {
copies += 1;
name = format!("{} ({})", color_scheme.name, copies);
}
Expand All @@ -322,17 +322,17 @@ impl Config {
}

pub fn opacity_ratio(&self) -> f32 {
(self.opacity as f32) / 100.0
f32::from(self.opacity) / 100.0
}

// Get a sorted and adjusted for duplicates list of profile names and ids
pub fn profile_names(&self) -> Vec<(String, ProfileId)> {
let mut profile_names = Vec::<(String, ProfileId)>::with_capacity(self.profiles.len());
for (profile_id, profile) in self.profiles.iter() {
for (profile_id, profile) in &self.profiles {
let mut name = profile.name.clone();

let mut copies = 1;
while profile_names.iter().find(|x| x.0 == name).is_some() {
while profile_names.iter().any(|x| x.0 == name) {
copies += 1;
name = format!("{} ({})", profile.name, copies);
}
Expand Down
2 changes: 1 addition & 1 deletion src/localize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ pub fn localize() {
let requested_languages = i18n_embed::DesktopLanguageRequester::requested_languages();

if let Err(error) = localizer.select(&requested_languages) {
eprintln!("Error while loading language for App List {}", error);
eprintln!("Error while loading language for App List {error}");
}
}
269 changes: 124 additions & 145 deletions src/main.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn context_menu<'a>(
entity: segmented_button::Entity,
) -> Element<'a, Message> {
let find_key = |action: &Action| -> String {
for (key_bind, key_action) in key_binds.iter() {
for (key_bind, key_action) in key_binds {
if action == key_action {
return key_bind.to_string();
}
Expand Down
28 changes: 10 additions & 18 deletions src/mouse_reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct MouseReporter {
}

impl MouseReporter {
fn button_number(&self, button: Button) -> Option<u8> {
fn button_number(button: Button) -> Option<u8> {
match button {
Button::Left => Some(0),
Button::Middle => Some(1),
Expand All @@ -36,10 +36,10 @@ impl MouseReporter {
) -> Option<Vec<u8>> {
//Buttons are handle slightly different between normal and sgr
//for normal/utf8 the button release is always reported as button 3
let Some(mut button) = (match event {
let mut button = (match event {
Event::Mouse(MouseEvent::ButtonPressed(b)) => {
self.button = Some(b);
self.button_number(b)
Self::button_number(b)
}
Event::Mouse(MouseEvent::ButtonReleased(_b)) => {
self.button = None;
Expand All @@ -59,14 +59,10 @@ impl MouseReporter {
//character, Cb).
//For example, motion into cell x,y with button 1 down is reported as
//CSI M @ CxCy ( @ = 32 + 0 (button 1) + 32 (motion indicator) ).
self.button
.and_then(|button| self.button_number(button))
.map(|b| b + 32)
self.button.and_then(Self::button_number).map(|b| b + 32)
}
_ => None,
}) else {
return None;
};
})?;

if modifiers.shift() {
button += 4;
Expand Down Expand Up @@ -127,16 +123,16 @@ impl MouseReporter {
x: u32,
y: u32,
) -> Option<Vec<u8>> {
let Some((button_no, event_code)) = (match event {
let (button_no, event_code) = (match event {
Event::Mouse(MouseEvent::ButtonPressed(button)) => {
//Button pressed is reported as button 0,1,2 and event code M
self.button = Some(button);
Some((self.button_number(button), "M"))
Some((Self::button_number(button), "M"))
}
Event::Mouse(MouseEvent::ButtonReleased(button)) => {
//Button pressed is reported as button 0,1,2 and event code m
self.button = None;
Some((self.button_number(button), "m"))
Some((Self::button_number(button), "m"))
}
Event::Mouse(MouseEvent::CursorMoved { .. }) => {
//Button pressed is reported as button 32 + 0,1,2 and event code M
Expand All @@ -148,12 +144,10 @@ impl MouseReporter {
self.last_movment_y = Some(y);
}
self.button
.map(|button| (self.button_number(button).map(|b| b + 32), "M"))
.map(|button| (Self::button_number(button).map(|b| b + 32), "M"))
}
_ => None,
}) else {
return None;
};
})?;

if let Some(mut button_no) = button_no {
if modifiers.shift() {
Expand All @@ -174,7 +168,6 @@ impl MouseReporter {

#[allow(clippy::too_many_arguments)]
pub fn report_sgr_mouse_wheel_scroll(
&self,
terminal: &Terminal,
term_cell_width: f32,
term_cell_height: f32,
Expand Down Expand Up @@ -217,7 +210,6 @@ impl MouseReporter {
//Emulate mouse wheel scroll with up/down arrows. Using mouse spec uses
//scroll-back and scroll-forw actions, which moves whole windows like page up/page down.
pub fn report_mouse_wheel_as_arrows(
&self,
terminal: &Terminal,
term_cell_width: f32,
term_cell_height: f32,
Expand Down
27 changes: 13 additions & 14 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,25 @@ fn convert_color(colors: &Colors, color: Color) -> cosmic_text::Color {
let rgb = match color {
Color::Named(named_color) => match colors[named_color] {
Some(rgb) => rgb,
None => match named_color {
NamedColor::Background => {
None => {
if named_color == NamedColor::Background {
// Allow using an unset background
return cosmic_text::Color(WINDOW_BG_COLOR.load(Ordering::SeqCst));
}
_ => {
} else {
log::warn!("missing named color {:?}", named_color);
Rgb::default()
}
},
}
},
Color::Spec(rgb) => rgb,
Color::Indexed(index) => match colors[index as usize] {
Some(rgb) => rgb,
None => {
Color::Indexed(index) => {
if let Some(rgb) = colors[index as usize] {
rgb
} else {
log::warn!("missing indexed color {}", index);
Rgb::default()
}
},
}
};
cosmic_text::Color::rgb(rgb.r, rgb.g, rgb.b)
}
Expand Down Expand Up @@ -437,9 +437,8 @@ impl Terminal {
}
}

let search_regex = match &mut self.search_regex_opt {
Some(some) => some,
None => return,
let Some(search_regex) = &mut self.search_regex_opt else {
return;
};

// Determine search origin
Expand Down Expand Up @@ -856,7 +855,7 @@ impl Terminal {
let term_lock = self.term.lock();
let mode = term_lock.mode();
if mode.contains(TermMode::SGR_MOUSE) {
self.mouse_reporter.report_sgr_mouse_wheel_scroll(
MouseReporter::report_sgr_mouse_wheel_scroll(
self,
self.size().cell_width,
self.size().cell_height,
Expand All @@ -866,7 +865,7 @@ impl Terminal {
y,
);
} else {
self.mouse_reporter.report_mouse_wheel_as_arrows(
MouseReporter::report_mouse_wheel_as_arrows(
self,
self.size().cell_width,
self.size().cell_height,
Expand Down
60 changes: 28 additions & 32 deletions src/terminal_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,9 @@ where
// Calculate layout lines
terminal.with_buffer(|buffer| {
let mut layout_lines = 0;
for line in buffer.lines.iter() {
match line.layout_opt() {
Some(layout) => layout_lines += layout.len(),
None => (),
for line in &buffer.lines {
if let Some(layout) = line.layout_opt() {
layout_lines += layout.len()
}
}

Expand Down Expand Up @@ -241,7 +240,7 @@ where
let state = tree.state.downcast_ref::<State>();

let cosmic_theme = theme.cosmic();
let scrollbar_w = cosmic_theme.spacing.space_xxs as f32;
let scrollbar_w = f32::from(cosmic_theme.spacing.space_xxs);

let view_position = layout.position() + [self.padding.left, self.padding.top].into();
let view_w = cmp::min(viewport.width as i32, layout.bounds().width as i32)
Expand Down Expand Up @@ -278,12 +277,12 @@ where
..Default::default()
},
Color::new(
background_color.r() as f32 / 255.0,
background_color.g() as f32 / 255.0,
background_color.b() as f32 / 255.0,
f32::from(background_color.r()) / 255.0,
f32::from(background_color.g()) / 255.0,
f32::from(background_color.b()) / 255.0,
match self.opacity {
Some(opacity) => opacity,
None => background_color.a() as f32 / 255.0,
None => f32::from(background_color.a()) / 255.0,
},
),
);
Expand Down Expand Up @@ -329,10 +328,10 @@ where
) {
let cosmic_text_to_iced_color = |color: cosmic_text::Color| {
Color::new(
color.r() as f32 / 255.0,
color.g() as f32 / 255.0,
color.b() as f32 / 255.0,
color.a() as f32 / 255.0,
f32::from(color.r()) / 255.0,
f32::from(color.g()) / 255.0,
f32::from(color.b()) / 255.0,
f32::from(color.a()) / 255.0,
)
};

Expand Down Expand Up @@ -370,8 +369,7 @@ where
}

if !metadata.flags.is_empty() {
let style_line_height =
(self.glyph_font_size / 10.0).max(2.0).min(16.0);
let style_line_height = (self.glyph_font_size / 10.0).clamp(2.0, 16.0);

let line_color = cosmic_text_to_iced_color(metadata.underline_color);

Expand Down Expand Up @@ -488,7 +486,7 @@ where
view_position,
metadata_set,
};
for glyph in run.glyphs.iter() {
for glyph in run.glyphs {
bg_rect.update(glyph, renderer, state.is_focused);
}
bg_rect.fill(renderer, state.is_focused);
Expand Down Expand Up @@ -606,7 +604,7 @@ where
modifiers,
..
}) if state.is_focused => {
for (key_bind, _) in self.key_binds.iter() {
for key_bind in self.key_binds.keys() {
if key_bind.matches(modifiers, &Key::Named(named)) {
return Status::Captured;
}
Expand Down Expand Up @@ -705,8 +703,7 @@ where
match named {
Named::Backspace => {
let code = if modifiers.control() { "\x08" } else { "\x7f" };
terminal
.input_scroll(format!("{}{}", alt_prefix, code).as_bytes().to_vec());
terminal.input_scroll(format!("{alt_prefix}{code}").as_bytes().to_vec());
status = Status::Captured;
}
Named::Enter => {
Expand Down Expand Up @@ -735,8 +732,7 @@ where
}
Named::Tab => {
let code = if modifiers.shift() { "\x1b[Z" } else { "\x09" };
terminal
.input_scroll(format!("{}{}", alt_prefix, code).as_bytes().to_vec());
terminal.input_scroll(format!("{alt_prefix}{code}").as_bytes().to_vec());
status = Status::Captured;
}
_ => {}
Expand All @@ -751,7 +747,7 @@ where
key,
..
}) if state.is_focused => {
for (key_bind, _) in self.key_binds.iter() {
for key_bind in self.key_binds.keys() {
if key_bind.matches(modifiers, &key) {
return Status::Captured;
}
Expand Down Expand Up @@ -1064,9 +1060,9 @@ fn shade(color: cosmic_text::Color, is_focused: bool) -> cosmic_text::Color {
} else {
let shade = 0.92;
cosmic_text::Color::rgba(
(color.r() as f32 * shade) as u8,
(color.g() as f32 * shade) as u8,
(color.b() as f32 * shade) as u8,
(f32::from(color.r()) * shade) as u8,
(f32::from(color.g()) * shade) as u8,
(f32::from(color.b()) * shade) as u8,
color.a(),
)
}
Expand Down Expand Up @@ -1106,8 +1102,8 @@ pub struct State {

impl State {
/// Creates a new [`State`].
pub fn new() -> State {
State {
pub fn new() -> Self {
Self {
modifiers: Modifiers::empty(),
click: None,
dragging: None,
Expand Down Expand Up @@ -1142,7 +1138,7 @@ meta 0b100000 (32)
caps_lock 0b1000000 (64)
num_lock 0b10000000 (128)
*/
fn calculate_modifier_number(state: &mut State) -> u8 {
fn calculate_modifier_number(state: &State) -> u8 {
let mut mod_no = 0;
if state.modifiers.shift() {
mod_no |= 1;
Expand All @@ -1162,10 +1158,10 @@ fn calculate_modifier_number(state: &mut State) -> u8 {
#[inline(always)]
fn csi(code: &str, suffix: &str, modifiers: u8) -> Option<Vec<u8>> {
if modifiers == 1 {
Some(format!("\x1B[{}{}", code, suffix).as_bytes().to_vec())
Some(format!("\x1B[{code}{suffix}").as_bytes().to_vec())
} else {
Some(
format!("\x1B[{};{}{}", code, modifiers, suffix)
format!("\x1B[{code};{modifiers}{suffix}")
.as_bytes()
.to_vec(),
)
Expand All @@ -1175,8 +1171,8 @@ fn csi(code: &str, suffix: &str, modifiers: u8) -> Option<Vec<u8>> {
#[inline(always)]
fn ss3(code: &str, modifiers: u8) -> Option<Vec<u8>> {
if modifiers == 1 {
Some(format!("\x1B\x4F{}", code).as_bytes().to_vec())
Some(format!("\x1B\x4F{code}").as_bytes().to_vec())
} else {
Some(format!("\x1B[1;{}{}", modifiers, code).as_bytes().to_vec())
Some(format!("\x1B[1;{modifiers}{code}").as_bytes().to_vec())
}
}
4 changes: 2 additions & 2 deletions src/terminal_theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ impl ColorDerive {
fn color_adj(rgb: Rgb, saturation_adj: f32, lightness_adj: f32) -> Rgb {
let mut okhsl = Self::rgb_to_okhsl(rgb);

okhsl.saturation = (okhsl.saturation + saturation_adj).max(0.0).min(1.0);
okhsl.lightness = (okhsl.lightness + lightness_adj).max(0.0).min(1.0);
okhsl.saturation = (okhsl.saturation + saturation_adj).clamp(0.0, 1.0);
okhsl.lightness = (okhsl.lightness + lightness_adj).clamp(0.0, 1.0);

Self::okhsl_to_rgb(okhsl)
}
Expand Down

0 comments on commit 4cec088

Please sign in to comment.