style: Using clippy lints
This commit is contained in:
@@ -6,3 +6,12 @@ edition = "2024"
|
||||
[dependencies]
|
||||
crossterm = "0.29"
|
||||
ratatui = { version = "0.30", features = ["crossterm"] }
|
||||
|
||||
[lints.clippy]
|
||||
pedantic = "warn"
|
||||
nursery = "warn"
|
||||
restriction = "warn"
|
||||
unused_trait_names = "warn"
|
||||
missing_docs_in_private_items = "warn"
|
||||
empty_structs_with_brackets = "warn"
|
||||
let_underscore_must_use = "warn"
|
||||
26
src/main.rs
26
src/main.rs
@@ -1,6 +1,3 @@
|
||||
use std::io::{self, Read};
|
||||
use std::time::Duration;
|
||||
|
||||
use crossterm::event::{self, Event, KeyCode};
|
||||
use crossterm::execute;
|
||||
use crossterm::terminal::{EnterAlternateScreen, enable_raw_mode};
|
||||
@@ -9,7 +6,8 @@ use ratatui::backend::CrosstermBackend;
|
||||
use ratatui::layout::{Constraint, Direction, Layout};
|
||||
use ratatui::style::Style;
|
||||
use ratatui::widgets::{Block, Borders, List, ListItem, ListState};
|
||||
|
||||
use std::io::{self, Read as _};
|
||||
use core::time::Duration;
|
||||
mod terminal_guard;
|
||||
use terminal_guard::TerminalModeGuard;
|
||||
|
||||
@@ -29,7 +27,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut stdout = std::io::stdout();
|
||||
execute!(stdout, EnterAlternateScreen)?;
|
||||
|
||||
let mut _mode_guard = TerminalModeGuard::new();
|
||||
let mut _mode_guard = TerminalModeGuard::default();
|
||||
|
||||
let backend = CrosstermBackend::new(stdout);
|
||||
let mut terminal = Terminal::new(backend)?;
|
||||
@@ -72,23 +70,17 @@ 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()
|
||||
{
|
||||
if let Some(i) = state.selected() {
|
||||
assert!(i < marked.len());
|
||||
if !marked[i] {
|
||||
let next = lines.len().min(i + 1);
|
||||
@@ -118,7 +110,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
}
|
||||
|
||||
_mode_guard.cleanup();
|
||||
drop(_mode_guard);
|
||||
terminal.show_cursor()?;
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -2,28 +2,18 @@ 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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user