refactor: reuse shortcut sanitizer and scrollbar styling

This commit is contained in:
objz committed 2026-10-01 14:29:24 +02:00
1 parent 7e7e5c0c31
commit 22a3eebe78
8 files changed
+25 -87

No files matched your search

+1 -1
View File
@@ -225,7 +225,7 @@ fn quote_windows_arg(value: &str) -> String {
quoted quoted
} }
fn sanitize(name: &str) -> String { pub(crate) fn sanitize(name: &str) -> String {
name.chars() name.chars()
.map(|c| { .map(|c| {
if c.is_alphanumeric() || c == '-' || c == '_' { if c.is_alphanumeric() || c == '-' || c == '_' {
+2 -15
View File
@@ -126,7 +126,7 @@ fn rewrite_linux_desktop_entries(_data_dir: &Path, _instances_dir: &Path) {
}; };
for entry in entries.flatten() { for entry in entries.flatten() {
let name = entry.file_name(); let name = entry.file_name();
let sanitized = sanitize(&name.to_string_lossy()); let sanitized = crate::instance::desktop::sanitize(&name.to_string_lossy());
let old = apps_dir.join(format!("mcl-{sanitized}.desktop")); let old = apps_dir.join(format!("mcl-{sanitized}.desktop"));
let new = apps_dir.join(format!("rmcl-{sanitized}.desktop")); let new = apps_dir.join(format!("rmcl-{sanitized}.desktop"));
if old.exists() if old.exists()
@@ -159,7 +159,7 @@ fn rewrite_native_desktop_shortcuts(_desktop_dir: &Path, _instances_dir: &Path)
let display = entry.file_name().to_string_lossy().into_owned(); let display = entry.file_name().to_string_lossy().into_owned();
// mcl named shortcut files with the sanitized form of the // mcl named shortcut files with the sanitized form of the
// instance name ("My Pack" -> "My_Pack"), so look those up. // instance name ("My Pack" -> "My_Pack"), so look those up.
let sanitized = sanitize(&display); let sanitized = crate::instance::desktop::sanitize(&display);
let path = _desktop_dir.join(format!("Minecraft - {sanitized}.{ext}")); let path = _desktop_dir.join(format!("Minecraft - {sanitized}.{ext}"));
if !path.exists() { if !path.exists() {
continue; continue;
@@ -174,19 +174,6 @@ fn rewrite_native_desktop_shortcuts(_desktop_dir: &Path, _instances_dir: &Path)
} }
} }
#[allow(dead_code)]
fn sanitize(name: &str) -> String {
name.chars()
.map(|c| {
if c.is_alphanumeric() || c == '-' || c == '_' {
c
} else {
'_'
}
})
.collect()
}
#[cfg(test)] #[cfg(test)]
#[path = "tests/migrate.rs"] #[path = "tests/migrate.rs"]
mod tests; mod tests;
+2 -12
View File
@@ -221,7 +221,7 @@ impl App {
layout::{Alignment, Margin}, layout::{Alignment, Margin},
style::{Modifier, Style}, style::{Modifier, Style},
text::Line, text::Line,
widgets::{Block, Clear, Paragraph, Scrollbar, ScrollbarOrientation}, widgets::{Block, Clear, Paragraph},
}; };
let theme = THEME.as_ref(); let theme = THEME.as_ref();
@@ -298,17 +298,7 @@ impl App {
height: overlay.height.saturating_sub(2), height: overlay.height.saturating_sub(2),
}; };
frame.render_stateful_widget( frame.render_stateful_widget(
Scrollbar::default() widgets::scrollbar(theme.text_dim()),
.orientation(ScrollbarOrientation::VerticalRight)
.begin_symbol(Some("\u{25b2}"))
.style(
Style::default()
.fg(theme.text_dim())
.add_modifier(Modifier::BOLD),
)
.thumb_symbol("\u{2551}")
.track_symbol(Some(""))
.end_symbol(Some("\u{25bc}")),
scrollbar_area, scrollbar_area,
&mut self.log_overlay_scrollbar, &mut self.log_overlay_scrollbar,
); );
+2 -12
View File
@@ -12,7 +12,7 @@ use ratatui::{
layout::Rect, layout::Rect,
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
text::{Line, Span, Text}, text::{Line, Span, Text},
widgets::{Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState}, widgets::{Paragraph, ScrollbarState},
}; };
use ratatui_image::{CropOptions, Resize, StatefulImage, protocol::StatefulProtocol}; use ratatui_image::{CropOptions, Resize, StatefulImage, protocol::StatefulProtocol};
use tui_widget_list::{ListBuilder, ListState as TuiListState, ListView}; use tui_widget_list::{ListBuilder, ListState as TuiListState, ListView};
@@ -2439,17 +2439,7 @@ pub fn render(
height: list_area.height.saturating_sub(2), height: list_area.height.saturating_sub(2),
}; };
frame.render_stateful_widget( frame.render_stateful_widget(
Scrollbar::default() crate::tui::widgets::scrollbar(theme.text_dim()),
.orientation(ScrollbarOrientation::VerticalRight)
.begin_symbol(Some("\u{25b2}"))
.style(
Style::default()
.fg(theme.text_dim())
.add_modifier(Modifier::BOLD),
)
.thumb_symbol("\u{2551}")
.track_symbol(Some(""))
.end_symbol(Some("\u{25bc}")),
scrollbar_area, scrollbar_area,
&mut state.scrollbar_state, &mut state.scrollbar_state,
); );
+2 -12
View File
@@ -8,7 +8,7 @@ use ratatui::{
layout::Rect, layout::Rect,
style::{Modifier, Style}, style::{Modifier, Style},
text::{Line, Span, Text}, text::{Line, Span, Text},
widgets::{Block, Borders, Scrollbar, ScrollbarOrientation, ScrollbarState}, widgets::{Block, Borders, ScrollbarState},
}; };
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{Arc, LazyLock, Mutex}; use std::sync::{Arc, LazyLock, Mutex};
@@ -384,17 +384,7 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut S
frame.render_stateful_widget(list, area, &mut state.list_state); frame.render_stateful_widget(list, area, &mut state.list_state);
frame.render_stateful_widget( frame.render_stateful_widget(
Scrollbar::default() super::scrollbar(theme.text_dim()),
.orientation(ScrollbarOrientation::VerticalRight)
.begin_symbol(Some("\u{25b2}"))
.style(
Style::default()
.fg(theme.text_dim())
.add_modifier(Modifier::BOLD),
)
.thumb_symbol("\u{2551}")
.track_symbol(Some(""))
.end_symbol(Some("\u{25bc}")),
scrollbar_area, scrollbar_area,
&mut state.scrollbar_state, &mut state.scrollbar_state,
); );
+3 -23
View File
@@ -10,7 +10,7 @@ use ratatui::{
layout::{Constraint, Layout, Rect}, layout::{Constraint, Layout, Rect},
style::{Modifier, Style}, style::{Modifier, Style},
text::{Line, Span}, text::{Line, Span},
widgets::{Block, Borders, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState}, widgets::{Block, Borders, Paragraph, ScrollbarState},
}; };
use tui_widget_list::{ListBuilder, ListState as TuiListState, ListView}; use tui_widget_list::{ListBuilder, ListState as TuiListState, ListView};
@@ -550,17 +550,7 @@ fn render_list(frame: &mut Frame, area: Rect, state: &mut LogsState, is_focused:
height: inner.height.saturating_sub(2), height: inner.height.saturating_sub(2),
}; };
frame.render_stateful_widget( frame.render_stateful_widget(
Scrollbar::default() super::scrollbar(theme.accent()),
.orientation(ScrollbarOrientation::VerticalRight)
.begin_symbol(Some("\u{25b2}"))
.style(
Style::default()
.fg(theme.accent())
.add_modifier(Modifier::BOLD),
)
.thumb_symbol("\u{2551}")
.track_symbol(Some(""))
.end_symbol(Some("\u{25bc}")),
scrollbar_area, scrollbar_area,
&mut state.scrollbar_state, &mut state.scrollbar_state,
); );
@@ -639,17 +629,7 @@ fn render_viewer(
height: area.height.saturating_sub(2), height: area.height.saturating_sub(2),
}; };
frame.render_stateful_widget( frame.render_stateful_widget(
Scrollbar::default() super::scrollbar(theme.accent()),
.orientation(ScrollbarOrientation::VerticalRight)
.begin_symbol(Some("\u{25b2}"))
.style(
Style::default()
.fg(theme.accent())
.add_modifier(Modifier::BOLD),
)
.thumb_symbol("\u{2551}")
.track_symbol(Some(""))
.end_symbol(Some("\u{25bc}")),
scrollbar_area, scrollbar_area,
&mut state.viewer_scrollbar_state, &mut state.viewer_scrollbar_state,
); );
+11
View File
@@ -6,6 +6,7 @@ use crossterm::event::KeyEvent;
use ratatui::{ use ratatui::{
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
text::{Line, Span}, text::{Line, Span},
widgets::{Scrollbar, ScrollbarOrientation},
}; };
pub mod account; pub mod account;
@@ -47,6 +48,16 @@ pub(crate) fn status_badge(label: impl Into<String>, color: Color) -> Span<'stat
Span::styled(format!(" {} ", label.into()), status_badge_style(color)) Span::styled(format!(" {} ", label.into()), status_badge_style(color))
} }
pub(crate) fn scrollbar(color: Color) -> Scrollbar<'static> {
Scrollbar::default()
.orientation(ScrollbarOrientation::VerticalRight)
.begin_symbol(Some("\u{25b2}"))
.style(Style::default().fg(color).add_modifier(Modifier::BOLD))
.thumb_symbol("\u{2551}")
.track_symbol(Some(""))
.end_symbol(Some("\u{25bc}"))
}
pub trait WidgetKey { pub trait WidgetKey {
fn handle_key(&mut self, key_event: &KeyEvent); fn handle_key(&mut self, key_event: &KeyEvent);
} }
+2 -12
View File
@@ -11,7 +11,7 @@ use ratatui::{
layout::{Constraint, Layout, Rect}, layout::{Constraint, Layout, Rect},
style::{Modifier, Style}, style::{Modifier, Style},
text::Span, text::Span,
widgets::{Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState}, widgets::{Paragraph, ScrollbarState},
}; };
use ratatui_image::{Resize, StatefulImage, protocol::StatefulProtocol}; use ratatui_image::{Resize, StatefulImage, protocol::StatefulProtocol};
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
@@ -445,17 +445,7 @@ pub fn render(frame: &mut Frame, area: Rect, state: &mut ScreenshotsState, is_fo
height: area.height.saturating_sub(2), height: area.height.saturating_sub(2),
}; };
frame.render_stateful_widget( frame.render_stateful_widget(
Scrollbar::default() super::scrollbar(theme.text_dim()),
.orientation(ScrollbarOrientation::VerticalRight)
.begin_symbol(Some("\u{25b2}"))
.style(
Style::default()
.fg(theme.text_dim())
.add_modifier(Modifier::BOLD),
)
.thumb_symbol("\u{2551}")
.track_symbol(Some(""))
.end_symbol(Some("\u{25bc}")),
scrollbar_area, scrollbar_area,
&mut state.scrollbar_state, &mut state.scrollbar_state,
); );