summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-08-01 22:07:04 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2024-08-01 22:07:04 +0200
commit1e1eb95926f556e666bc20355327abd24d264858 (patch)
treee40b68da80b984355baf68a36983d78bb2ec63dd
parent5d15bed762c4c699bebc9b5f5e302fa61785ed51 (diff)
minor cleanup
-rw-r--r--src/app.rs3
-rw-r--r--src/config/evalsto.rs9
-rw-r--r--src/config/mod.rs2
-rw-r--r--src/config/theme/mod.rs2
-rw-r--r--src/config/theme/sheetview.rs2
-rw-r--r--src/config/theme/style.rs6
-rw-r--r--src/sheet/cell.rs4
-rw-r--r--src/sheet/mod.rs6
-rw-r--r--src/sheet/register.rs4
-rw-r--r--src/widgets/logview.rs3
-rw-r--r--src/widgets/luaeditor/buffer.rs10
-rw-r--r--src/widgets/luaeditor/mod.rs25
-rw-r--r--src/widgets/luaeditor/treesitter.rs4
-rw-r--r--src/widgets/sheetview/mod.rs47
-rw-r--r--src/widgets/statusbar.rs12
15 files changed, 79 insertions, 60 deletions
diff --git a/src/app.rs b/src/app.rs
index a215859..0f740e8 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -12,6 +12,7 @@ use ratatui::{
prelude::*,
};
+#[derive(Default)]
pub struct App {
exit: bool,
view: SheetView,
@@ -34,7 +35,7 @@ impl App {
if let Err(e) = lua::source(&config::constants::USER_RC_PATH) {
self.exit = true;
tui::restore()?;
- println!("{}", e.to_string());
+ println!("{}", e);
}
while !self.exit {
diff --git a/src/config/evalsto.rs b/src/config/evalsto.rs
index 10b2dd1..eae9b2d 100644
--- a/src/config/evalsto.rs
+++ b/src/config/evalsto.rs
@@ -62,3 +62,12 @@ where
}
}
}
+
+impl<T, A> Default for EvalTo<T, A>
+where
+ T: Default,
+{
+ fn default() -> Self {
+ Self::Value(T::default())
+ }
+}
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 6e88306..c5131a6 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -8,7 +8,7 @@ pub mod theme;
pub mod constants;
pub mod evalsto;
-#[derive(Debug)]
+#[derive(Debug, Default)]
pub struct GlobalConfig {
pub theme: Theme,
}
diff --git a/src/config/theme/mod.rs b/src/config/theme/mod.rs
index 35101c5..75f65a0 100644
--- a/src/config/theme/mod.rs
+++ b/src/config/theme/mod.rs
@@ -8,7 +8,7 @@ use super::DUMMY_CONFIG;
pub mod style;
pub mod sheetview;
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, Default)]
pub struct Theme {
pub sheetview: SheetViewTheme,
}
diff --git a/src/config/theme/sheetview.rs b/src/config/theme/sheetview.rs
index 30d801e..edc7cc3 100644
--- a/src/config/theme/sheetview.rs
+++ b/src/config/theme/sheetview.rs
@@ -8,7 +8,7 @@ use crate::{
use super::style::Style;
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Default)]
pub struct SheetViewTheme {
pub cursor: EvalTo<Style, CellRef>,
pub selection: EvalTo<Style, CellRef>,
diff --git a/src/config/theme/style.rs b/src/config/theme/style.rs
index 6e8d161..87cd068 100644
--- a/src/config/theme/style.rs
+++ b/src/config/theme/style.rs
@@ -3,7 +3,7 @@ use std::str::FromStr;
use mlua::{FromLua, IntoLua};
use ratatui::style::{Color, Styled, Stylize};
-#[derive(Clone, Copy, Debug)]
+#[derive(Clone, Copy, Debug, Default)]
pub struct Style {
pub fg: Option<Color>,
pub bg: Option<Color>,
@@ -102,7 +102,7 @@ impl<'lua> FromLua<'lua> for Style {
return Ok(style)
}
- return Err(mlua::Error::runtime("could not parse style"));
+ Err(mlua::Error::runtime("could not parse style"))
}
}
@@ -114,6 +114,6 @@ impl<'lua> IntoLua<'lua> for Style {
let table = lua.create_table()?;
table.set("fg", self.fg.map(|s| s.to_string()).into_lua(lua)?)?;
table.set("bg", self.bg.map(|s| s.to_string()).into_lua(lua)?)?;
- Ok(table.into_lua(lua)?)
+ table.into_lua(lua)
}
}
diff --git a/src/sheet/cell.rs b/src/sheet/cell.rs
index 9608b93..3b18bde 100644
--- a/src/sheet/cell.rs
+++ b/src/sheet/cell.rs
@@ -89,6 +89,10 @@ pub struct CellRef {
}
impl CellRef {
+ /// # Safety
+ ///
+ /// This function shall only be called by the Sheet instance itsself or
+ /// if it has been assured that the `SheetId` is valid.
pub unsafe fn new(sheet_id: SheetId, row: usize, column: usize, cell: Cell) -> Self {
Self {
sheet_id,
diff --git a/src/sheet/mod.rs b/src/sheet/mod.rs
index 11f95c9..f67d55e 100644
--- a/src/sheet/mod.rs
+++ b/src/sheet/mod.rs
@@ -30,9 +30,9 @@ impl Sheet {
}
}
- pub fn get_cell<'a>(&'a self, row: usize, column: usize) -> Option<&'a Cell> {
+ pub fn get_cell(&self, row: usize, column: usize) -> Option<&Cell> {
if let Some(r) = self.rows.get(row) {
- if let Some(_) = r.get(column) {
+ if r.get(column).is_some() {
return Some(&self.rows[row][column]);
}
}
@@ -57,7 +57,7 @@ impl Sheet {
}
pub fn width(&self) -> usize {
- self.rows.get(0).map(|r| r.len()).unwrap_or(1)
+ self.rows.first().map(|r| r.len()).unwrap_or(1)
}
pub fn set_width(&mut self, mut width: usize) {
diff --git a/src/sheet/register.rs b/src/sheet/register.rs
index aa5245b..4fca3a3 100644
--- a/src/sheet/register.rs
+++ b/src/sheet/register.rs
@@ -21,7 +21,7 @@ impl Register {
id
}
- pub fn get<'a>(id: SheetId) -> Option<Arc<RwLock<Sheet>>> {
+ pub fn get(id: SheetId) -> Option<Arc<RwLock<Sheet>>> {
let register = REGISTER.read().unwrap();
if id < register.len() {
@@ -45,7 +45,7 @@ impl UserData for Register {
});
methods.add_function_mut("get", |lua, id: SheetId| {
- if let Some(_) = Register::get(id) {
+ if Register::get(id).is_some() {
let luaref = SheetLuaRef::new(id);
if let Ok(ud) = lua.create_userdata(luaref) {
Ok(Value::UserData(ud))
diff --git a/src/widgets/logview.rs b/src/widgets/logview.rs
index c5b6432..35c7bed 100644
--- a/src/widgets/logview.rs
+++ b/src/widgets/logview.rs
@@ -4,6 +4,7 @@ use crate::lua::iobuffer::iobuffer;
use super::statusbar::StatusBar;
+#[derive(Default)]
pub struct LogView {
bar: StatusBar,
}
@@ -29,7 +30,7 @@ impl Widget for &mut LogView {
buffer.get()
};
- let mut line_area = inner_area.clone();
+ let mut line_area = inner_area;
line_area.height = 1;
line_area.y = inner_area.y + inner_area.height - 1;
diff --git a/src/widgets/luaeditor/buffer.rs b/src/widgets/luaeditor/buffer.rs
index 3f59b6c..1af0c08 100644
--- a/src/widgets/luaeditor/buffer.rs
+++ b/src/widgets/luaeditor/buffer.rs
@@ -18,7 +18,7 @@ impl Buffer {
fn refresh_line_max(&mut self) {
self.cursor
- .set_x_max(self.lines[self.cursor.y() as usize].len())
+ .set_x_max(self.lines[self.cursor.y()].len())
}
fn refresh_buffer_max(&mut self) {
@@ -39,7 +39,7 @@ impl Buffer {
s.starts_with(&(str.clone() + "if "))
|| s.starts_with(&(str.clone() + "for "))
|| s.starts_with(&(str.clone() + "while "))
- || (s.starts_with(&str) && s.contains("function(") && s.ends_with(")"))
+ || (s.starts_with(&str) && s.contains("function(") && s.ends_with(')'))
})
.count();
@@ -66,7 +66,7 @@ impl Buffer {
let l = a.trim();
let insert_end = (l.starts_with("if ") && l.ends_with(" then"))
|| ((l.starts_with("for ") || l.starts_with("while ")) && l.ends_with(" do"))
- || (l.contains("function(") && l.ends_with(")"));
+ || (l.contains("function(") && l.ends_with(')'));
let extra_indent = if insert_end { 1 } else { 0 };
@@ -126,11 +126,11 @@ impl Buffer {
}
pub fn current_line(&self) -> &String {
- self.line(self.cursor.y() as usize).unwrap()
+ self.line(self.cursor.y()).unwrap()
}
fn current_line_mut(&mut self) -> &mut String {
- self.line_mut(self.cursor.y() as usize).unwrap()
+ self.line_mut(self.cursor.y()).unwrap()
}
pub fn cursor(&self) -> &Cursor {
diff --git a/src/widgets/luaeditor/mod.rs b/src/widgets/luaeditor/mod.rs
index 98af9e5..939a65f 100644
--- a/src/widgets/luaeditor/mod.rs
+++ b/src/widgets/luaeditor/mod.rs
@@ -97,10 +97,10 @@ impl Widget for &mut LuaEditor {
let highlights = treesitter::highlighter_split(text.as_bytes(), &self.highlight_config);
let nr_width = (self.buffer.lines().len().to_string().len() + 1).max(4) as u16;
- let mut text_area = inner_area.clone();
+ let mut text_area = inner_area;
text_area.x += nr_width;
- let mut span_area = text_area.clone();
+ let mut span_area = text_area;
let mut current_line = 0;
for (hl, group) in highlights.iter() {
@@ -112,7 +112,7 @@ impl Widget for &mut LuaEditor {
current_line += 1;
} else if current_line >= self.scroll {
- let group = group.replace("\t", " ");
+ let group = group.replace('\t', " ");
let span = group.to_span();
if inner_area.contains(span_area.into()) {
@@ -124,7 +124,7 @@ impl Widget for &mut LuaEditor {
}
for i in self.scroll..self.buffer.lines().len() {
- let mut nr_area = span_area.clone();
+ let mut nr_area = span_area;
nr_area.x = inner_area.x;
nr_area.width = nr_width - 1;
nr_area.y = inner_area.y + (i - self.scroll) as u16;
@@ -137,7 +137,7 @@ impl Widget for &mut LuaEditor {
(i + 1).to_line().right_aligned().render(nr_area, buf);
}
- let mut cursor_area = text_area.clone();
+ let mut cursor_area = text_area;
cursor_area.width = 1;
cursor_area.height = 1;
cursor_area.y += self.buffer.cursor().y() as u16;
@@ -146,7 +146,7 @@ impl Widget for &mut LuaEditor {
let (first, _) = self
.buffer
.current_line()
- .split_at(self.buffer.cursor().x() as usize);
+ .split_at(self.buffer.cursor().x());
for c in first.chars() {
if c == '\t' {
@@ -166,7 +166,7 @@ impl Widget for &mut LuaEditor {
self.buffer
.current_line()
.chars()
- .nth(self.buffer.cursor().x() as usize)
+ .nth(self.buffer.cursor().x())
.map(|c| if c == '\t' { ' ' } else { c })
.unwrap_or(' ')
.to_span()
@@ -175,3 +175,14 @@ impl Widget for &mut LuaEditor {
}
}
}
+
+impl Default for LuaEditor {
+ fn default() -> Self {
+ Self {
+ highlight_config: treesitter::new_highlight_configuration(),
+ bar: StatusBar::default(),
+ scroll: 0,
+ buffer: Buffer::default(),
+ }
+ }
+}
diff --git a/src/widgets/luaeditor/treesitter.rs b/src/widgets/luaeditor/treesitter.rs
index 1a4dc04..1474434 100644
--- a/src/widgets/luaeditor/treesitter.rs
+++ b/src/widgets/luaeditor/treesitter.rs
@@ -1,6 +1,6 @@
use tree_sitter_highlight::{Highlight, HighlightConfiguration, HighlightEvent, Highlighter};
-static HIGHLIGHT_NAMES: [&'static str; 29] = [
+static HIGHLIGHT_NAMES: [&str; 29] = [
"attribute",
"boolean",
"comment",
@@ -71,7 +71,7 @@ pub fn highlighter_split<'a>(
splits.push((current, "\n"));
}
- if !group.ends_with("\n") {
+ if !group.ends_with('\n') {
splits.pop();
}
}
diff --git a/src/widgets/sheetview/mod.rs b/src/widgets/sheetview/mod.rs
index 6ef8125..f81edab 100644
--- a/src/widgets/sheetview/mod.rs
+++ b/src/widgets/sheetview/mod.rs
@@ -9,7 +9,7 @@ use ratatui::{
};
use crate::{
- config::{theme::style, GlobalConfig},
+ config::GlobalConfig,
lua,
sheet::{
cell::Cell,
@@ -23,7 +23,9 @@ use super::{luaeditor::LuaEditor, statusbar::StatusBar};
const DEFAULT_COLUMN_WIDTH: u16 = 10;
+#[derive(Default)]
enum SheetViewMode {
+ #[default]
Normal,
Insert,
Visual,
@@ -34,6 +36,7 @@ enum SheetViewMode {
EditorError(String),
}
+#[derive(Default)]
pub struct SheetView {
bar: StatusBar,
sheet: SheetId,
@@ -153,7 +156,7 @@ impl SheetView {
KeyCode::Char('h') => self.move_cursor_by((0, -1)),
KeyCode::Char('l') => self.move_cursor_by((0, 1)),
KeyCode::Char('v') => {
- self.selection_anchor = Some(self.cursor.clone());
+ self.selection_anchor = Some(self.cursor);
self.mode = SheetViewMode::Visual
}
KeyCode::Char('s') => {
@@ -175,7 +178,7 @@ impl SheetView {
let lock = Register::get(self.sheet).unwrap();
let mut sheet = lock.write().unwrap();
- if let Some(_) = self.selection_anchor {
+ if self.selection_anchor.is_some() {
self.selection().iter().map(|(r, c)| (*r, *c)).collect()
} else {
vec![self.cursor]
@@ -235,12 +238,10 @@ impl SheetView {
KeyCode::Enter => {
if let Err(error) = lua::get().load(self.bar.input().unwrap_or("")).exec() {
self.mode = SheetViewMode::CommandError(error.to_string())
+ } else if self.selection_anchor.is_some() {
+ self.mode = SheetViewMode::Visual;
} else {
- if self.selection_anchor.is_some() {
- self.mode = SheetViewMode::Visual;
- } else {
- self.mode = SheetViewMode::Normal;
- }
+ self.mode = SheetViewMode::Normal;
}
self.bar.set_input_mode(false);
@@ -271,7 +272,7 @@ impl SheetView {
let mut cells = Vec::new();
- if let Some(_) = self.selection_anchor {
+ if self.selection_anchor.is_some() {
cells = self
.selection()
.iter()
@@ -311,26 +312,18 @@ impl SheetView {
}
fn is_editor_visible(&self) -> bool {
- match self.mode {
- SheetViewMode::Script
- | SheetViewMode::Processing(_)
- | SheetViewMode::EditorError(_) => true,
- _ => false,
- }
+ matches!(
+ self.mode,
+ SheetViewMode::Script | SheetViewMode::Processing(_) | SheetViewMode::EditorError(_),
+ )
}
fn is_error_window_visible(&self) -> bool {
- match self.mode {
- SheetViewMode::EditorError(_) => true,
- _ => false,
- }
+ matches!(self.mode, SheetViewMode::EditorError(_))
}
fn is_progress_visible(&self) -> bool {
- match self.mode {
- SheetViewMode::Processing(_) => true,
- _ => false,
- }
+ matches!(self.mode, SheetViewMode::Processing(_))
}
fn areas(&self, area: Rect) -> (Rect, Rect, Rect, Rect, Rect) {
@@ -428,19 +421,19 @@ impl Widget for &mut SheetView {
theme
.cursor
.get(cell_ref, &lua::get())
- .unwrap_or(style::Style::new())
+ .unwrap_or_default()
.apply(cell.to_line())
} else if self.is_in_selection(cell_pos_y, cell_pos_x) {
theme
.selection
.get(cell_ref, &lua::get())
- .unwrap_or(style::Style::new())
+ .unwrap_or_default()
.apply(cell.to_line())
} else {
theme
.cell
.get(cell_ref, &lua::get())
- .unwrap_or(style::Style::new())
+ .unwrap_or_default()
.apply(cell.to_line())
};
@@ -537,7 +530,7 @@ impl Widget for &mut SheetView {
)
.percent(progress as u16);
- Clear::default().render(progress_area, buf);
+ Clear.render(progress_area, buf);
gauge.render(progress_area, buf);
}
}
diff --git a/src/widgets/statusbar.rs b/src/widgets/statusbar.rs
index 60b58a6..c8d52e5 100644
--- a/src/widgets/statusbar.rs
+++ b/src/widgets/statusbar.rs
@@ -6,7 +6,7 @@ use ratatui::{
widgets::Widget,
};
-#[derive(Clone)]
+#[derive(Clone, Default)]
pub struct StatusBar {
left: String,
left_style: Style,
@@ -125,11 +125,11 @@ impl StatusBar {
}
pub fn input(&self) -> Option<&str> {
- self.input.as_ref().map(|s| s.as_str())
+ self.input.as_deref()
}
pub fn area(&self, rect: Rect) -> Rect {
- let mut inner = rect.clone();
+ let mut inner = rect;
inner.height -= 1;
inner
}
@@ -162,9 +162,9 @@ impl StatusBar {
area.y += area.height - 1;
area.height = 1;
- let mut left = area.clone();
- let mut right = area.clone();
- let mut middle = area.clone();
+ let mut left = area;
+ let mut right = area;
+ let mut middle = area;
left.width = self.left.len() as u16;
right.width = self.right.len() as u16;