Skip to content

Commit bd17655

Browse files
committed
cargo fmt/clippy fixes
1 parent d0f49ba commit bd17655

File tree

4 files changed

+115
-75
lines changed

4 files changed

+115
-75
lines changed

src/app.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,6 @@ impl App {
145145
};
146146
}
147147

148-
149-
150-
151-
152148
pub fn handle_db_response(&mut self, resp: DBResponse) {
153149
match resp {
154150
DBResponse::Schema { tables } => {
@@ -179,9 +175,7 @@ impl App {
179175
.visible_rows_per_page
180176
.min(self.buffer_rows.len())
181177
.max(1);
182-
let mut view_start = self
183-
.global_row_offset
184-
.saturating_sub(self.buffer_offset);
178+
let mut view_start = self.global_row_offset.saturating_sub(self.buffer_offset);
185179
let max_start = self.buffer_rows.len().saturating_sub(cap);
186180
if view_start > max_start {
187181
view_start = max_start;
@@ -448,9 +442,11 @@ impl App {
448442
}
449443
// At bottom of visible window: try to scroll within current buffer first
450444
let buffer_end = self.buffer_offset.saturating_add(self.buffer_rows.len());
451-
if self.global_row_offset
445+
if self
446+
.global_row_offset
452447
.saturating_add(self.sel_row)
453-
.saturating_add(1) < buffer_end
448+
.saturating_add(1)
449+
< buffer_end
454450
{
455451
self.global_row_offset = self.global_row_offset.saturating_add(1);
456452
self.view_start = self.view_start.saturating_add(1);
@@ -494,12 +490,17 @@ impl App {
494490
self.status = "Editing __rowid__ is not supported".into();
495491
return;
496492
}
497-
if let AppMode::Editing { row: erow, col: ecol, .. } = self.mode {
498-
if erow == row && ecol == col {
499-
// Already editing this cell; do not reset buffer or cursor
500-
self.status = "Editing: Enter to save, Esc to cancel".into();
501-
return;
502-
}
493+
if let AppMode::Editing {
494+
row: erow,
495+
col: ecol,
496+
..
497+
} = self.mode
498+
&& erow == row
499+
&& ecol == col
500+
{
501+
// Already editing this cell; do not reset buffer or cursor
502+
self.status = "Editing: Enter to save, Esc to cancel".into();
503+
return;
503504
}
504505
// Capture a stable rowid for this edit session
505506
let rowid = self
@@ -761,8 +762,6 @@ impl App {
761762

762763
// ===== Column width tiers (0 = narrow, 1 = normal, 2 = wide) =====
763764

764-
765-
766765
/// Make the current column narrower by one tier.
767766
pub fn resize_current_column_narrower(&mut self) {
768767
if self.columns.is_empty() {
@@ -827,8 +826,6 @@ impl App {
827826
.and_then(|row| row.get(c))
828827
.map(|s| s.as_str())
829828
}
830-
831-
832829
}
833830

834831
// Simplified grapheme stepping without unicode-segmentation:

src/db.rs

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use anyhow::Result;
22
use crossbeam_channel::{Receiver, Sender};
33
use rusqlite::{Connection, Row, types::ValueRef};
4+
use std::collections::HashMap;
45
use std::fs::File;
56
use std::io::{BufWriter, Write};
6-
use std::collections::HashMap;
77

88
#[derive(Debug, Clone, Copy)]
99
pub enum SortDir {
@@ -101,7 +101,18 @@ pub fn start_db_worker(path: String, req_rx: Receiver<DBRequest>, resp_tx: Sende
101101
filter,
102102
sort_by,
103103
sort_dir,
104-
} => load_table(&conn, &table, page, page_size, offset_override, filter, sort_by, sort_dir),
104+
} => {
105+
let params = LoadTableParams {
106+
table,
107+
page,
108+
page_size,
109+
offset_override,
110+
filter,
111+
sort_by,
112+
sort_dir,
113+
};
114+
load_table(&conn, &params)
115+
}
105116
DBRequest::UpdateCell {
106117
table,
107118
rowid,
@@ -139,16 +150,26 @@ fn load_schema(conn: &Connection) -> Result<Vec<String>> {
139150
Ok(names)
140151
}
141152

142-
fn load_table(
143-
conn: &Connection,
144-
table: &str,
153+
struct LoadTableParams {
154+
table: String,
145155
page: usize,
146156
page_size: usize,
147157
offset_override: Option<usize>,
148158
filter: Option<String>,
149159
sort_by: Option<String>,
150160
sort_dir: Option<SortDir>,
151-
) -> Result<DBResponse> {
161+
}
162+
163+
fn load_table(conn: &Connection, p: &LoadTableParams) -> Result<DBResponse> {
164+
// unpack params
165+
let table = p.table.as_str();
166+
let page = p.page;
167+
let page_size = p.page_size;
168+
let offset_override = p.offset_override;
169+
let filter = p.filter.clone();
170+
let sort_by = p.sort_by.clone();
171+
let sort_dir = p.sort_dir;
172+
152173
// columns
153174
let mut col_stmt = conn.prepare(&format!("PRAGMA table_info({})", ident(table)))?;
154175
let mut columns: Vec<String> = vec!["__rowid__".to_string()];
@@ -405,7 +426,11 @@ fn update_cell(
405426
) -> Result<DBResponse> {
406427
// Fetch previous value for history
407428
let prev_value: Option<String> = {
408-
let sql = format!("SELECT {} FROM {} WHERE rowid = ?1", ident(column), ident(table));
429+
let sql = format!(
430+
"SELECT {} FROM {} WHERE rowid = ?1",
431+
ident(column),
432+
ident(table)
433+
);
409434
let mut stmt_prev = conn.prepare(&sql)?;
410435
stmt_prev
411436
.query_row([rowid], |row| {
@@ -454,31 +479,31 @@ fn undo_last_change(
454479
history: &mut HashMap<String, Vec<Change>>,
455480
table: &str,
456481
) -> Result<DBResponse> {
457-
if let Some(stack) = history.get_mut(table) {
458-
if let Some(change) = stack.pop() {
459-
// Apply reverse update: set column back to previous value
460-
let mut stmt = conn.prepare(&format!(
461-
"UPDATE {} SET {} = ?1 WHERE rowid = ?2",
462-
ident(&change.table),
463-
ident(&change.column),
464-
))?;
465-
let value_param = match change.prev_value {
466-
None => rusqlite::types::Value::Null,
467-
Some(ref s) => parse_value(s),
468-
};
469-
match stmt.execute((value_param, change.rowid)) {
470-
Ok(_) => {
471-
return Ok(DBResponse::CellUpdated {
472-
ok: true,
473-
message: Some("Undo applied".into()),
474-
});
475-
}
476-
Err(e) => {
477-
return Ok(DBResponse::CellUpdated {
478-
ok: false,
479-
message: Some(format!("Undo failed: {}", e)),
480-
});
481-
}
482+
if let Some(stack) = history.get_mut(table)
483+
&& let Some(change) = stack.pop()
484+
{
485+
// Apply reverse update: set column back to previous value
486+
let mut stmt = conn.prepare(&format!(
487+
"UPDATE {} SET {} = ?1 WHERE rowid = ?2",
488+
ident(&change.table),
489+
ident(&change.column),
490+
))?;
491+
let value_param = match change.prev_value {
492+
None => rusqlite::types::Value::Null,
493+
Some(ref s) => parse_value(s),
494+
};
495+
match stmt.execute((value_param, change.rowid)) {
496+
Ok(_) => {
497+
return Ok(DBResponse::CellUpdated {
498+
ok: true,
499+
message: Some("Undo applied".into()),
500+
});
501+
}
502+
Err(e) => {
503+
return Ok(DBResponse::CellUpdated {
504+
ok: false,
505+
message: Some(format!("Undo failed: {}", e)),
506+
});
482507
}
483508
}
484509
}

src/main.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ fn run_app(
284284
use crossterm::event::KeyCode::*;
285285
// Mark dirty only for keys that affect the edit buffer/cursor/state
286286
match key.code {
287-
Enter | Esc | Backspace | Delete | Left | Right | Home | End | Char(_) => {
287+
Enter | Esc | Backspace | Delete | Left | Right | Home | End
288+
| Char(_) => {
288289
dirty = true;
289290
}
290291
_ => {}
@@ -323,15 +324,17 @@ fn handle_key_normal(app: &mut App, code: KeyCode) -> bool {
323324
} else {
324325
app.move_cell_up()
325326
}
326-
},
327+
}
327328
KeyCode::Down => {
328329
if app.focus == app::Focus::Tables {
329330
app.move_table_selection_down()
330331
} else {
331332
app.move_cell_down()
332333
}
333-
},
334-
KeyCode::Tab => { app.toggle_focus(); },
334+
}
335+
KeyCode::Tab => {
336+
app.toggle_focus();
337+
}
335338
KeyCode::Enter => app.load_selected_table_page(0),
336339
KeyCode::PageDown => app.next_page(),
337340
KeyCode::PageUp => app.prev_page(),

src/ui.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ fn draw_help(f: &mut Frame, area: Rect, _app: &App) {
7474
Line::from(
7575
"Editing: e Edit cell | Enter Save | Esc Cancel | Ctrl-d Set NULL | u Undo last change",
7676
),
77-
Line::from("Filter: / Begin filter | Enter Apply | Esc Clear (also in normal mode)"),
77+
Line::from(
78+
"Filter: / Begin filter | Enter Apply | Esc Clear (also in normal mode)",
79+
),
7880
Line::from("Sorting: s Cycle sort by column | S Toggle direction"),
7981
Line::from("Copy: c Copy cell | C Copy row | Ctrl+C Copy page (TSV)"),
8082
Line::from("Autosize: a Autosize column | A Autosize all"),
@@ -94,25 +96,25 @@ fn draw_tables(f: &mut Frame, area: Rect, app: &App) {
9496
.collect();
9597

9698
// Visually indicate focus on the Tables pane by changing border color and title
97-
let title = if app.focus == Focus::Tables { "Tables ◀" } else { "Tables" };
99+
let title = if app.focus == Focus::Tables {
100+
"Tables ◀"
101+
} else {
102+
"Tables"
103+
};
98104
let block = if app.focus == Focus::Tables {
99105
Block::default()
100106
.borders(Borders::ALL)
101107
.border_style(Style::default().fg(Color::Cyan))
102108
.title(title)
103109
} else {
104-
Block::default()
105-
.borders(Borders::ALL)
106-
.title(title)
110+
Block::default().borders(Borders::ALL).title(title)
107111
};
108112

109-
let list = List::new(items)
110-
.block(block)
111-
.highlight_style(
112-
Style::default()
113-
.add_modifier(Modifier::BOLD)
114-
.fg(Color::Yellow),
115-
);
113+
let list = List::new(items).block(block).highlight_style(
114+
Style::default()
115+
.add_modifier(Modifier::BOLD)
116+
.fg(Color::Yellow),
117+
);
116118

117119
f.render_stateful_widget(list, area, &mut list_state(app));
118120
}
@@ -169,7 +171,11 @@ fn draw_data(f: &mut Frame, area: Rect, app: &mut App) {
169171
} else {
170172
"Data".to_string()
171173
};
172-
let title = if app.focus == Focus::Data { format!("{base_title} ◀") } else { base_title };
174+
let title = if app.focus == Focus::Data {
175+
format!("{base_title} ◀")
176+
} else {
177+
base_title
178+
};
173179
let block = if app.focus == Focus::Data {
174180
Block::default()
175181
.borders(Borders::ALL)
@@ -222,9 +228,10 @@ fn draw_data(f: &mut Frame, area: Rect, app: &mut App) {
222228
app.autosize_all_request = false;
223229
app.autosize_col_request = None;
224230
} else if let Some(i) = app.autosize_col_request.take()
225-
&& i < cols {
226-
app.col_abs_widths[i] = measure_column_width(app, i);
227-
}
231+
&& i < cols
232+
{
233+
app.col_abs_widths[i] = measure_column_width(app, i);
234+
}
228235
}
229236
// Table inside inner area
230237
let widths = column_widths(
@@ -244,7 +251,12 @@ fn draw_data(f: &mut Frame, area: Rect, app: &mut App) {
244251
let mut cells = Vec::with_capacity(row.len());
245252
for (c_idx, val) in row.iter().enumerate() {
246253
// Live editing view: render edit buffer with a visible cursor for the editing cell.
247-
let mut cell = if let AppMode::Editing { row: erow, col: ecol, cursor } = app.mode {
254+
let mut cell = if let AppMode::Editing {
255+
row: erow,
256+
col: ecol,
257+
cursor,
258+
} = app.mode
259+
{
248260
if r_idx == erow && c_idx == ecol {
249261
let buf = app.edit_buffer.as_str();
250262
let cur = cursor.min(buf.len());
@@ -259,7 +271,12 @@ fn draw_data(f: &mut Frame, area: Rect, app: &mut App) {
259271
};
260272

261273
// Highlight selection, and use a distinct highlight for the editing cell.
262-
if let AppMode::Editing { row: erow, col: ecol, .. } = app.mode {
274+
if let AppMode::Editing {
275+
row: erow,
276+
col: ecol,
277+
..
278+
} = app.mode
279+
{
263280
if r_idx == erow && c_idx == ecol {
264281
cell = cell.style(Style::default().bg(Color::Yellow).fg(Color::Black));
265282
} else if r_idx == app.sel_row && c_idx == app.sel_col {
@@ -329,8 +346,6 @@ fn column_widths(total_width: u16, cols: usize, tiers: &[u8], abs: &[u16]) -> Ve
329346
.collect()
330347
}
331348

332-
333-
334349
// Measure the width (in characters) required to fully display a column,
335350
// considering both header and current page rows. Adds small padding.
336351
fn measure_column_width(app: &App, col: usize) -> u16 {

0 commit comments

Comments
 (0)