Compare commits

..

2 Commits

Author SHA1 Message Date
Lukas Wölfer
a8c22061f0 style: linting 2026-01-24 09:22:51 +01:00
Lukas Wölfer
d9eaeb1848 style: Using clippy lints 2026-01-24 09:05:22 +01:00
3 changed files with 52 additions and 50 deletions

View File

@@ -6,3 +6,21 @@ edition = "2024"
[dependencies]
crossterm = "0.29"
ratatui = { version = "0.30", features = ["crossterm"] }
[lints.clippy]
pedantic = "warn"
unused_trait_names = "warn"
missing_docs_in_private_items = "warn"
empty_structs_with_brackets = "warn"
let_underscore_must_use = "warn"
arbitrary_source_item_ordering = "warn"
as_conversions = "warn"
default_constructed_unit_structs = "warn"
equatable_if_let = "warn"
indexing_slicing = "warn"
missing_assert_message = "warn"
panic_in_result_fn = "warn"
redundant_closure_for_method_calls = "warn"
str_to_string = "warn"
used_underscore_binding = "warn"
wildcard_enum_match_arm = "warn"

View File

@@ -1,16 +1,14 @@
use std::io::{self, Read};
use std::time::Duration;
mod terminal_guard;
use core::time::Duration;
use crossterm::event::{self, Event, KeyCode};
use crossterm::execute;
use crossterm::terminal::{EnterAlternateScreen, enable_raw_mode};
use ratatui::Terminal;
use ratatui::backend::CrosstermBackend;
use ratatui::layout::{Constraint, Direction, Layout};
use ratatui::style::Style;
use ratatui::widgets::{Block, Borders, List, ListItem, ListState};
mod terminal_guard;
use std::io::{self, Read as _};
use terminal_guard::TerminalModeGuard;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -22,14 +20,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
return Ok(());
}
let lines: Vec<String> = input.lines().map(|s| s.to_string()).collect();
let lines: Vec<String> = input
.lines()
.map(std::string::ToString::to_string)
.collect();
let mut marked = vec![false; lines.len()];
enable_raw_mode()?;
let mut stdout = std::io::stdout();
execute!(stdout, EnterAlternateScreen)?;
let mut _mode_guard = TerminalModeGuard::new();
let mode_guard = TerminalModeGuard;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
@@ -42,17 +43,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
loop {
terminal.draw(|f| {
let size = f.area();
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(0)].as_ref())
.split(size);
let items: Vec<ListItem> = lines
.iter()
.enumerate()
.map(|(i, l)| {
let prefix = if marked[i] { 'x' } else { ' ' };
ListItem::new(format!("[{prefix}] {l}"))
.zip(marked.iter())
.map(|(text, marked)| {
let prefix = if *marked { 'x' } else { ' ' };
ListItem::new(format!("[{prefix}] {text}"))
})
.collect();
@@ -64,7 +61,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
)
.highlight_style(Style::default().bold().yellow().on_dark_gray());
f.render_stateful_widget(list, chunks[0], &mut state);
f.render_stateful_widget(list, size, &mut state);
})?;
if event::poll(Duration::from_millis(100))? {
@@ -72,29 +69,25 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Event::Key(key) => match key.code {
KeyCode::Char('q') => break,
KeyCode::Up => {
if let Some(i) = state.selected()
&& i > 0
{
state.select(Some(i - 1));
if let Some(i) = state.selected() {
state.select(Some(i.saturating_sub(1)));
}
}
KeyCode::Down => {
if let Some(i) = state.selected()
&& i + 1 < lines.len()
{
state.select(Some(i + 1));
if let Some(i) = state.selected() {
state.select(Some((i + 1).min(lines.len() - 1)));
}
}
KeyCode::Char(' ') => {
if let Some(i) = state.selected()
&& i < marked.len()
{
assert!(i < marked.len());
if !marked[i] {
if let Some(i) = state.selected() {
let Some(marked_cell) = marked.get_mut(i) else {
return Err("Index out of bounds".into());
};
if !*marked_cell {
let next = lines.len().min(i + 1);
state.select(Some(next));
}
marked[i] = !marked[i];
*marked_cell = !*marked_cell;
}
}
KeyCode::Char('c') if key.modifiers.contains(event::KeyModifiers::CONTROL) => {
@@ -103,7 +96,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
_ => {}
},
Event::Mouse(s) => {
if let event::MouseEventKind::Down(event::MouseButton::Left) = s.kind {
if s.kind == event::MouseEventKind::Down(event::MouseButton::Left) {
let area = terminal.size()?;
if s.row > area.height && s.row < area.height + area.height {
let idx = (s.row - area.height - 1) as usize;
@@ -118,7 +111,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}
_mode_guard.cleanup();
drop(mode_guard);
terminal.show_cursor()?;
Ok(())

View File

@@ -1,29 +1,20 @@
//! A guard that restores the terminal state on drop.
use crossterm::cursor::Show;
use crossterm::execute;
use crossterm::terminal::{LeaveAlternateScreen, disable_raw_mode};
pub struct TerminalModeGuard {
active: bool,
}
/// A guard that ensures the terminal is restored to its original state
/// when dropped.
#[derive(Default, Debug)]
pub struct TerminalModeGuard;
impl TerminalModeGuard {
pub fn new() -> Self {
Self { active: true }
}
impl TerminalModeGuard {}
pub fn cleanup(&mut self) {
if !self.active {
return;
}
impl Drop for TerminalModeGuard {
#[allow(clippy::let_underscore_must_use, reason = "We want to ignore errors during cleanup.")]
fn drop(&mut self) {
let _ = disable_raw_mode();
let mut stdout = std::io::stdout();
let _ = execute!(stdout, LeaveAlternateScreen, Show);
self.active = false;
}
}
impl Drop for TerminalModeGuard {
fn drop(&mut self) {
self.cleanup();
}
}