From b60d3d78c0c3b6c9cdca7c3b5deb00e8aaa947b4 Mon Sep 17 00:00:00 2001 From: Tor Berge Saebjoernsen Date: Thu, 4 May 2023 14:05:42 +0200 Subject: [PATCH] clippy stuff --- src/app.rs | 4 +- src/popups/ankimporter.rs | 51 +++++++++-------- src/popups/edit_card.rs | 2 +- src/popups/filepicker.rs | 2 +- src/popups/find_card.rs | 10 ++-- src/popups/newchild.rs | 2 +- src/popups/progress_popup.rs | 10 ++-- src/tabs/import.rs | 6 +- src/tabs/review/logic.rs | 2 +- src/tests/doc/databasetest.rs | 9 ++- src/tests/doc/mod.rs | 88 +++++++++++++++++------------ src/utils/ankitemplate.rs | 22 ++++---- src/utils/card.rs | 2 +- src/utils/incread.rs | 2 +- src/utils/interval.rs | 2 +- src/utils/sql/fetch/mod.rs | 11 +--- src/widgets/cardrater.rs | 10 ++-- src/widgets/cardstatus.rs | 2 +- src/widgets/numeric_input.rs | 3 +- src/widgets/textinput/info.rs | 8 +-- src/widgets/textinput/mod.rs | 18 +++--- src/widgets/textinput/navigation.rs | 4 +- src/widgets/topics.rs | 75 +++++++++++------------- 23 files changed, 173 insertions(+), 172 deletions(-) diff --git a/src/app.rs b/src/app.rs index d0b1850..62b6a44 100644 --- a/src/app.rs +++ b/src/app.rs @@ -364,7 +364,7 @@ pub trait Tab { if let MyKey::KeyPress(pos) = key { self.get_view().cursor = pos; } - let cursor = self.get_cursor().clone(); + let cursor = *self.get_cursor(); match key { MyKey::Nav(dir) => self.navigate(dir), key => self.keyhandler(appdata, key, &cursor), @@ -417,7 +417,7 @@ pub trait Tab { area = chunks[1]; self.transform_area(&mut area); } - let cursor = self.get_cursor().clone(); + let cursor = *self.get_cursor(); self.set_selection(area); self.get_tabdata().view.validate_pos(); // ensures cursor is on a widget; self.render(f, appdata, &cursor); diff --git a/src/popups/ankimporter.rs b/src/popups/ankimporter.rs index 5621a76..9b34d13 100644 --- a/src/popups/ankimporter.rs +++ b/src/popups/ankimporter.rs @@ -72,14 +72,15 @@ impl fmt::Display for Deck { } } use crate::app::{AppData, Tab, TabData, Widget}; -impl<'a> Ankimporter<'a> { - pub fn new() -> Self { + +impl Default for Ankimporter<'_> { + fn default() -> Self { let mut list = StatefulList::::new("".to_string()); list.persistent_highlight = true; let searchterm = Field::default(); let description = Field::default(); - Ankimporter { + Self { prompt: Button::new("Choose anki deck".to_string()), searchterm, description, @@ -89,16 +90,18 @@ impl<'a> Ankimporter<'a> { tabdata: TabData::new("Anki Importing".to_string()), } } +} +impl<'a> Ankimporter<'a> { fn update_desc(&mut self) { if let Some(idx) = self.list.state.selected() { let id = self.list.items[idx].id; - if !self.descmap.contains_key(&id) { + self.descmap.entry(id).or_insert_with(|| { let url = format!("https://ankiweb.net/shared/info/{}", id); let body = reqwest::blocking::get(url).unwrap().text().unwrap(); - let desc = get_description(&body, &id); - self.descmap.insert(id, desc); - } + + get_description(&body, &id) + }); } } @@ -117,13 +120,13 @@ impl<'a> Ankimporter<'a> { let body = reqwest::blocking::get(url).unwrap().text().unwrap(); let splitter: Vec<&str> = body.split("const shared = new anki.SharedList(").collect(); - let foo = splitter[1].to_string(); + let xxx = splitter[1].to_string(); let mut myvec = Vec::::new(); let mut stringstatus = Stringstatus::Beforeint; let mut title = String::new(); let mut intrep = String::new(); - for c in foo.chars() { + for c in xxx.chars() { if c == ';' { break; } @@ -170,11 +173,11 @@ impl<'a> Ankimporter<'a> { } for deck in &myvec { - if !self.descmap.contains_key(&deck.id) { + if let std::collections::hash_map::Entry::Vacant(e) = self.descmap.entry(deck.id) { let url = format!("https://ankiweb.net/shared/info/{}", deck.id); let body = reqwest::blocking::get(url).unwrap().text().unwrap(); let desc = get_description(&body, &deck.id); - self.descmap.insert(deck.id, desc); + e.insert(desc); break; } } @@ -204,7 +207,7 @@ impl<'a> Tab for Ankimporter<'a> { }); let msg = MsgPopup::new(rx, "Unzipping".to_string()); self.set_popup(Box::new(msg)); - self.state = State::Unzipping(name.clone()); + self.state = State::Unzipping(name); } State::Unzipping(name) => { let (tx, rx): (mpsc::SyncSender, Receiver) = @@ -219,7 +222,7 @@ impl<'a> Tab for Ankimporter<'a> { self.state = State::Renaming(name); } State::Renaming(name) => { - let ldc = LoadCards::new(appdata, name.clone()); + let ldc = LoadCards::new(appdata, name); self.set_popup(Box::new(ldc)); self.state = State::Main; } @@ -356,10 +359,10 @@ pub async fn download_deck( while let Some(item) = stream.next().await { let chunk = item - .or(Err(format!("Error while downloading file"))) + .or(Err("Error while downloading file".to_string())) .unwrap(); file.write_all(&chunk) - .or(Err(format!("Error while writing to file"))) + .or(Err("Error while writing to file".to_string())) .unwrap(); let new = std::cmp::min(downloaded + (chunk.len() as u64), total_size); downloaded = new; @@ -373,18 +376,17 @@ pub async fn download_deck( fn extract_download_link(trd: &String) -> String { let pattern = r"(?P(https:.*));".to_string(); let re = Regex::new(&pattern).unwrap(); - let foo = re.captures(&trd).expect(&format!( - "Couldnt find pattern on following string: {}@@", - trd - )); - foo.get(1).unwrap().as_str().to_string() + let xxx = re + .captures(trd) + .unwrap_or_else(|| panic!("Couldnt find pattern on following string: {}@@", trd)); + xxx.get(1).unwrap().as_str().to_string() } -fn get_k_value(pagesource: &String) -> String { +fn get_k_value(pagesource: &str) -> String { let pattern = "k\" value=\"(.*)\"".to_string(); let re = Regex::new(&pattern).unwrap(); - let foo = re.captures(&pagesource).unwrap(); - foo.get(1).unwrap().as_str().to_string() + let xxx = re.captures(pagesource).unwrap(); + xxx.get(1).unwrap().as_str().to_string() } pub fn get_download_link(deckid: u32) -> String { @@ -410,6 +412,5 @@ pub fn get_download_link(deckid: u32) -> String { .unwrap() .text() .unwrap(); - let link = extract_download_link(&result); - return link; + extract_download_link(&result) } diff --git a/src/popups/edit_card.rs b/src/popups/edit_card.rs index c7f1d93..b84a1f7 100644 --- a/src/popups/edit_card.rs +++ b/src/popups/edit_card.rs @@ -5,7 +5,7 @@ use crate::{ utils::{ area::{split_leftright_by_percent, split_updown_by_percent}, card::{CardTypeData, CardView}, - sql::fetch::cards::{get_cardtype, get_cardtypedata}, + sql::fetch::cards::get_cardtypedata, }, widgets::infobox::InfoBox, MyKey, diff --git a/src/popups/filepicker.rs b/src/popups/filepicker.rs index 37f05fd..257dec3 100644 --- a/src/popups/filepicker.rs +++ b/src/popups/filepicker.rs @@ -145,7 +145,7 @@ impl<'a> FilePicker<'a> { if let Some('/') = wtf { break; } - if let None = wtf { + if wtf.is_none() { panic!("oh none"); } } diff --git a/src/popups/find_card.rs b/src/popups/find_card.rs index dc86cc2..b7c8e25 100644 --- a/src/popups/find_card.rs +++ b/src/popups/find_card.rs @@ -162,13 +162,11 @@ impl StatefulList { }); } self.items = matching_cards; - if self.items.len() == 0 { + if self.items.is_empty() { self.state.select(None) - } else { - if let Some(idx) = self.state.selected() { - let new_index = std::cmp::min(idx, self.items.len() - 1); - self.state.select(Some(new_index)); - } + } else if let Some(idx) = self.state.selected() { + let new_index = std::cmp::min(idx, self.items.len() - 1); + self.state.select(Some(new_index)); } } diff --git a/src/popups/newchild.rs b/src/popups/newchild.rs index 9e98093..4c7082b 100644 --- a/src/popups/newchild.rs +++ b/src/popups/newchild.rs @@ -119,7 +119,7 @@ impl<'a> Tab for AddChildWidget<'a> { self.cardview.answer.set_area(answer); } fn render(&mut self, f: &mut Frame, appdata: &AppData, cursor: &Pos) { - self.prompt.render(f, appdata, &cursor); + self.prompt.render(f, appdata, cursor); self.cardview.render(f, appdata, cursor) } diff --git a/src/popups/progress_popup.rs b/src/popups/progress_popup.rs index fd2012b..aee1e71 100644 --- a/src/popups/progress_popup.rs +++ b/src/popups/progress_popup.rs @@ -59,13 +59,11 @@ impl Tab for Progress { self.tabdata.state = PopUpState::Exit; } } + } else if let Some(tab) = std::mem::take(&mut self.next_tab) { + self.tabdata.state = PopUpState::Switch(tab); } else { - if let Some(tab) = std::mem::take(&mut self.next_tab) { - self.tabdata.state = PopUpState::Switch(tab); - } else { - self.popupvalue = PopupValue::Ok; - self.tabdata.state = PopUpState::Exit; - } + self.popupvalue = PopupValue::Ok; + self.tabdata.state = PopUpState::Exit; } } diff --git a/src/tabs/import.rs b/src/tabs/import.rs index 85eb8fd..97de817 100644 --- a/src/tabs/import.rs +++ b/src/tabs/import.rs @@ -1,7 +1,7 @@ use crate::{ app::{AppData, Tab}, popups::{ - ankimporter::Ankimporter, + self, filepicker::{FilePicker, FilePickerPurpose}, menu::{Menu, TraitButton}, }, @@ -9,7 +9,9 @@ use crate::{ impl<'a> Menu<'a> { pub fn new_import_tab() -> Menu<'a> { - let a = |_appdata: &AppData| -> Box { Box::new(Ankimporter::new()) }; + let a = |_appdata: &AppData| -> Box { + Box::>::default() + }; let b = |appdata: &AppData| -> Box { Box::new(Menu::new_anki_users(appdata)) }; let c = |_appdata: &AppData| -> Box { Box::new(FilePicker::new( diff --git a/src/tabs/review/logic.rs b/src/tabs/review/logic.rs index b6712e9..a05eed1 100644 --- a/src/tabs/review/logic.rs +++ b/src/tabs/review/logic.rs @@ -266,7 +266,7 @@ impl<'a> MainReview<'a> { }; let color = modecolor(&self.mode); - let cursor = self.get_cursor().clone(); + let cursor = *self.get_cursor(); self.progress_bar.current = current; self.progress_bar.max = target; self.progress_bar.color = color; diff --git a/src/tests/doc/databasetest.rs b/src/tests/doc/databasetest.rs index 45c0b9c..262d72c 100644 --- a/src/tests/doc/databasetest.rs +++ b/src/tests/doc/databasetest.rs @@ -10,8 +10,7 @@ use rusqlite::Connection; #[cfg(test)] use crate::utils::misc::SpekiPaths; use crate::utils::{ - aliases::Conn, - card::{self, Card, CardType, CardTypeData, FinishedInfo, RecallGrade, Review, UnfinishedInfo}, + card::{Card, CardTypeData, FinishedInfo, RecallGrade, Review, UnfinishedInfo}, interval::{calc_stability, strength_algo}, sql::{fetch::cards::fetch_card, init_db}, }; @@ -25,7 +24,7 @@ fn get_paths() -> SpekiPaths { fn get_conn() -> Arc> { let paths = get_paths(); Arc::new(Mutex::new( - Connection::open(&paths.database).expect("Failed to connect to database."), + Connection::open(paths.database).expect("Failed to connect to database."), )) } @@ -103,8 +102,8 @@ fn dependency_logic() { .answer(answer.clone()) .save_card(&conn); let card = fetch_card(&conn, id1); - assert_eq!(question.clone(), card.question.clone()); - assert_eq!(answer.clone(), card.answer.clone()); + assert_eq!(question, card.question); + assert_eq!(answer, card.answer); assert!(Card::is_resolved(&conn, id1)); let id2 = Card::new(CardTypeData::Unfinished(UnfinishedInfo::default())) .question("what is a capital".to_string()) diff --git a/src/tests/doc/mod.rs b/src/tests/doc/mod.rs index 801026d..de0ba07 100644 --- a/src/tests/doc/mod.rs +++ b/src/tests/doc/mod.rs @@ -1,3 +1,4 @@ +use std::default; #[cfg(test)] use std::sync::{Arc, Mutex}; pub mod databasetest; @@ -30,21 +31,24 @@ pub fn get_appdata() -> AppData { #[test] fn is_last_visrow() { let input_text = "0123456789012".to_string(); - dbg!("AHHH"); - let mut area = Rect::default(); - area.width = 12; - area.height = 12; - let mut txt = Field::new_with_text(input_text.clone(), 0, 10); + let area = Rect { + width: 12, + height: 12, + ..Default::default() + }; + let mut txt = Field::new_with_text(input_text, 0, 10); txt.set_dimensions(area); assert!(txt.is_cursor_last_vis_row()); txt.cursor.column = 9; assert!(!txt.is_cursor_last_vis_row()); let input_text = "0123456789".to_string(); - let mut area = Rect::default(); - area.width = 12; - area.height = 12; - let mut txt = Field::new_with_text(input_text.clone(), 0, 5); + let area = Rect { + width: 12, + height: 12, + ..Default::default() + }; + let mut txt = Field::new_with_text(input_text, 0, 5); txt.set_dimensions(area); assert!(txt.is_cursor_last_vis_row()); } @@ -53,11 +57,13 @@ fn is_last_visrow() { fn vis_up() { let appdata = get_appdata(); let input_text = "0123456789\n012".to_string(); - let mut area = Rect::default(); - area.width = 12; - area.height = 12; + let area = Rect { + width: 12, + height: 12, + ..Default::default() + }; - let mut txt = Field::new_with_text(input_text.clone(), 1, 2); + let mut txt = Field::new_with_text(input_text, 1, 2); txt.set_dimensions(area); txt.keyhandler(&appdata, MyKey::Up); assert_eq!(txt.cursor.column, 2); @@ -65,11 +71,13 @@ fn vis_up() { let appdata = get_appdata(); let input_text = "0123456789012".to_string(); - let mut area = Rect::default(); - area.width = 12; - area.height = 12; + let area = Rect { + width: 12, + height: 12, + ..Default::default() + }; - let mut txt = Field::new_with_text(input_text.clone(), 1, 2); + let mut txt = Field::new_with_text(input_text, 1, 2); txt.set_dimensions(area); txt.keyhandler(&appdata, MyKey::Up); assert_eq!(txt.current_visual_col(), 2); @@ -79,10 +87,13 @@ fn vis_up() { fn vis_down() { let appdata = get_appdata(); let input_text = "0123456789\n012".to_string(); - let mut area = Rect::default(); - area.width = 12; - area.height = 12; - let mut txt = Field::new_with_text(input_text.clone(), 0, 2); + + let area = Rect { + width: 12, + height: 12, + ..Default::default() + }; + let mut txt = Field::new_with_text(input_text, 0, 2); txt.set_dimensions(area); txt.keyhandler(&appdata, MyKey::Down); assert_eq!(txt.cursor.column, 2); @@ -90,10 +101,13 @@ fn vis_down() { let appdata = get_appdata(); let input_text = "0123456789012".to_string(); - let mut area = Rect::default(); - area.width = 12; - area.height = 12; - let mut txt = Field::new_with_text(input_text.clone(), 0, 2); + + let area = Rect { + width: 12, + height: 12, + ..Default::default() + }; + let mut txt = Field::new_with_text(input_text, 0, 2); txt.set_dimensions(area); txt.keyhandler(&appdata, MyKey::Down); assert_eq!(txt.current_visual_col(), 2); @@ -103,10 +117,12 @@ fn vis_down() { fn count_vislines() { let _appdata = get_appdata(); let input_text = "0123456789".to_string(); - let mut area = Rect::default(); - area.width = 12; - area.height = 12; - let mut txt = Field::new_with_text(input_text.clone(), 0, 0); + let area = Rect { + width: 12, + height: 12, + ..Default::default() + }; + let mut txt = Field::new_with_text(input_text, 0, 0); txt.set_dimensions(area); assert_eq!(txt.rowlen, 10); @@ -117,7 +133,7 @@ fn count_vislines() { fn norm_w() { let appdata = get_appdata(); let input_text = "hey there man".to_string(); - let mut txt = Field::new_with_text(input_text.clone(), 0, 0); + let mut txt = Field::new_with_text(input_text, 0, 0); txt.set_normal_mode(); txt.cursor.column = 0; @@ -132,7 +148,7 @@ fn norm_w() { fn norm_b() { let appdata = get_appdata(); let input_text = "hey there man".to_string(); - let mut txt = Field::new_with_text(input_text.clone(), 0, 0); + let mut txt = Field::new_with_text(input_text, 0, 0); txt.set_normal_mode(); txt.cursor.column = 11; @@ -150,7 +166,7 @@ fn norm_b() { fn norm_e() { let appdata = get_appdata(); let input_text = "hey there man".to_string(); - let mut txt = Field::new_with_text(input_text.clone(), 0, 0); + let mut txt = Field::new_with_text(input_text, 0, 0); txt.set_normal_mode(); txt.keyhandler(&appdata, MyKey::Char('e')); @@ -167,7 +183,7 @@ fn norm_e() { fn delete_word_left() { let appdata = get_appdata(); let input_text = "the quick brown fox jumps over the lazy dog".to_string(); - let mut txt = Field::new_with_text(input_text.clone(), 0, 14); + let mut txt = Field::new_with_text(input_text, 0, 14); txt.set_insert_mode(); txt.keyhandler(&appdata, MyKey::Ctrl('w')); @@ -192,7 +208,7 @@ fn delete_word_left() { #[test] fn linebreaks() { let input_text = "hey there!! \n nice \n".to_string(); - let txt = Field::new_with_text(input_text.clone(), 0, 0); + let txt = Field::new_with_text(input_text, 0, 0); assert_eq!(txt.text.len(), 3); } @@ -200,14 +216,14 @@ fn linebreaks() { fn same_return() { let input_text = "hey there!! \n nice \n".to_string(); let txt = Field::new_with_text(input_text.clone(), 0, 0); - assert_eq!(txt.return_text(), input_text.clone()); + assert_eq!(txt.return_text(), input_text); } #[test] fn delete_to_right() { let appdata = get_appdata(); let input_text = "hey there!! \n nice \n".to_string(); - let mut txt = Field::new_with_text(input_text.clone(), 0, 0); + let mut txt = Field::new_with_text(input_text, 0, 0); txt.set_normal_mode(); txt.keyhandler(&appdata, MyKey::Char('D')); diff --git a/src/utils/ankitemplate.rs b/src/utils/ankitemplate.rs index e106625..9ac7c3a 100644 --- a/src/utils/ankitemplate.rs +++ b/src/utils/ankitemplate.rs @@ -221,8 +221,8 @@ impl Template { .split('') .map(|x| { let mut text = x.to_string(); - let audio = extract_audio(&mut text, &folderpath); - let image = extract_image(&mut text, &folderpath); + let audio = extract_audio(&mut text, folderpath); + let image = extract_image(&mut text, folderpath); CardField { text, audio, image } }) .collect(); @@ -279,7 +279,7 @@ impl Template { let replaced = self.note_from_card_index(viewpos).fields[val].clone(); let right = split_by_field[i]; tempstring.push_str(&replaced.text); - tempstring.push_str(&right); + tempstring.push_str(right); } } template = tempstring.clone(); @@ -397,19 +397,19 @@ impl Template { for (i, field) in note.fields.iter().enumerate() { let fieldname = Self::with_braces(model.fields[i].clone()); if let Some(path) = &field.audio { - front_template.match_indices(&fieldname).for_each(|foo| { - frontaudiovec.push((foo.0, path.to_owned())); + front_template.match_indices(&fieldname).for_each(|xxx| { + frontaudiovec.push((xxx.0, path.to_owned())); }); - back_template.match_indices(&fieldname).for_each(|foo| { - backaudiovec.push((foo.0, path.to_owned())); + back_template.match_indices(&fieldname).for_each(|xxx| { + backaudiovec.push((xxx.0, path.to_owned())); }); } if let Some(path) = &field.image { - front_template.match_indices(&fieldname).for_each(|foo| { - frontimagevec.push((foo.0, path.to_owned())); + front_template.match_indices(&fieldname).for_each(|xxx| { + frontimagevec.push((xxx.0, path.to_owned())); }); - back_template.match_indices(&fieldname).for_each(|foo| { - backimagevec.push((foo.0, path.to_owned())); + back_template.match_indices(&fieldname).for_each(|xxx| { + backimagevec.push((xxx.0, path.to_owned())); }); } } diff --git a/src/utils/card.rs b/src/utils/card.rs index baec807..88da72e 100644 --- a/src/utils/card.rs +++ b/src/utils/card.rs @@ -346,7 +346,7 @@ impl<'a> CardView<'a> { card: None, revealed: true, revealbutton: Button::new("Reveal answer".to_string()), - cardrater: CardRater::new(), + cardrater: CardRater::default(), question: Field::new("Question".to_string()), answer: Field::new("Answer".to_string()), dependencies: StatefulList::new("Dependencies".to_string()), diff --git a/src/utils/incread.rs b/src/utils/incread.rs index ec15149..3e0cb10 100644 --- a/src/utils/incread.rs +++ b/src/utils/incread.rs @@ -104,7 +104,7 @@ impl IncRead { } pub fn update_text(&self, conn: &Arc>) { let text = self.source.return_text(); - update_inc_text(&conn, text, self.id, &self.source.cursor).unwrap(); + update_inc_text(conn, text, self.id, &self.source.cursor).unwrap(); } } diff --git a/src/utils/interval.rs b/src/utils/interval.rs index 00ec37f..af0abe0 100644 --- a/src/utils/interval.rs +++ b/src/utils/interval.rs @@ -45,7 +45,7 @@ pub fn calc_strength(conn: &Arc>) { panic! {"wtf {}", &card.question}; } let stability = get_stability(conn, card.id); - passed = time_passed_since_review(&history[(history.len() - 1) as usize]); + passed = time_passed_since_review(&history[(history.len() - 1)]); strength = strength_algo(passed, stability); update_strength(conn, card.id, strength); } diff --git a/src/utils/sql/fetch/mod.rs b/src/utils/sql/fetch/mod.rs index 452bfc3..e65db45 100644 --- a/src/utils/sql/fetch/mod.rs +++ b/src/utils/sql/fetch/mod.rs @@ -1,16 +1,11 @@ use crate::utils::aliases::*; -use crate::utils::ankitemplate::MediaContents; -use crate::utils::card::{ - Card, CardType, CardTypeData, FinishedInfo, PendingInfo, RecallGrade, Review, UnfinishedInfo, -}; +use crate::utils::card::{Card, CardType}; use crate::utils::misc::get_current_unix; //, Topic, Review} use crate::widgets::topics::Topic; -use rusqlite::{Connection, Result, Row, Rows}; -use std::iter::Rev; -use std::path::PathBuf; +use rusqlite::{Connection, Result, Row}; use std::sync::{Arc, Mutex}; -use std::time::{Duration, SystemTime, UNIX_EPOCH}; +use std::time::{ SystemTime, UNIX_EPOCH}; use color_eyre::eyre::Result as PrettyResult; diff --git a/src/widgets/cardrater.rs b/src/widgets/cardrater.rs index 3441701..c4b4bfa 100644 --- a/src/widgets/cardrater.rs +++ b/src/widgets/cardrater.rs @@ -16,14 +16,16 @@ pub struct CardRater { area: Rect, } -impl CardRater { - pub fn new() -> CardRater { - CardRater { +impl Default for CardRater { + fn default() -> Self { + Self { selection: Some(RecallGrade::Decent), area: Rect::default(), } } +} +impl CardRater { fn left(&mut self) { let selection = if let Some(selection) = &self.selection { selection @@ -41,7 +43,7 @@ impl CardRater { fn keypress(&mut self, pos: Pos) { if Self::is_selected(self, &pos) { let area = self.get_area(); - if (area.x..(area.x + (area.width * 1 / 4))).contains(&pos.x) { + if (area.x..(area.x + (area.width / 4))).contains(&pos.x) { self.selection = Some(RecallGrade::None); } else if (area.x..(area.x + (area.width * 2 / 4))).contains(&pos.x) { self.selection = Some(RecallGrade::Failed); diff --git a/src/widgets/cardstatus.rs b/src/widgets/cardstatus.rs index 8afdb0b..92ace85 100644 --- a/src/widgets/cardstatus.rs +++ b/src/widgets/cardstatus.rs @@ -1,4 +1,4 @@ -use crate::utils::{aliases::CardID, card::CardTypeData}; +use crate::utils::card::CardTypeData; use super::infobox::InfoBox; diff --git a/src/widgets/numeric_input.rs b/src/widgets/numeric_input.rs index 8b378e5..c46e85a 100644 --- a/src/widgets/numeric_input.rs +++ b/src/widgets/numeric_input.rs @@ -89,7 +89,6 @@ impl StatefulList { .map(|name| NumItem::new(name.0, name.1)) .collect::>(); - let myself = StatefulList::with_items(title, names); - myself + StatefulList::with_items(title, names) } } diff --git a/src/widgets/textinput/info.rs b/src/widgets/textinput/info.rs index 5d5915f..a14055d 100644 --- a/src/widgets/textinput/info.rs +++ b/src/widgets/textinput/info.rs @@ -49,8 +49,8 @@ impl Field { } pub fn is_cursor_in_view(&mut self) -> bool { let current_line = self.current_abs_visual_line() as u16; - let scroll = self.scroll as u16; - let winheight = self.window_height as u16; + let scroll = self.scroll; + let winheight = self.window_height; (current_line > scroll) && (current_line < (scroll + winheight)) } @@ -65,14 +65,14 @@ impl Field { lines += if actual_rowlen == self.rowlen as usize { 1 } else { - (self.get_rowlen(i) as u16 / (self.rowlen + 0) as u16) as usize + 1 + (self.get_rowlen(i) as u16 / (self.rowlen + 0)) as usize + 1 } } panic!(); } fn get_rowcol(&self, cursor: &CursorPos) -> u16 { - cursor.column as u16 / self.rowlen as u16 + cursor.column as u16 / self.rowlen } pub fn current_rel_visual_line(&self) -> u16 { diff --git a/src/widgets/textinput/mod.rs b/src/widgets/textinput/mod.rs index 8ae599a..da6fd92 100644 --- a/src/widgets/textinput/mod.rs +++ b/src/widgets/textinput/mod.rs @@ -132,7 +132,7 @@ impl Field { self.mode = Mode::Insert; } pub fn set_visual_mode(&mut self) { - self.startselect = Some(self.cursor.clone()); + self.startselect = Some(self.cursor); self.mode = Mode::Visual; } @@ -284,11 +284,11 @@ impl Field { pub fn return_selection(&self) -> Option { if self.selection_exists() { - let start = self.startselect.clone().unwrap(); - let end = self.cursor.clone(); + let start = self.startselect.unwrap(); + let end = self.cursor; let mut splitvec = vec![start, end]; splitvec.sort_by_key(|curse| (curse.row, curse.column)); - let (start, end) = (splitvec[0].clone(), splitvec[1].clone()); + let (start, end) = (splitvec[0], splitvec[1]); if start.row == end.row { let line = self.text[start.row].clone(); let left_bytepos = Self::find_grapheme_bytepos(&line, end.column + 1); @@ -372,11 +372,9 @@ impl Field { } else { self.preferredcol = Some(old_offset); } - } else { - if let Some(prefcol) = self.preferredcol { - let target = self.cursor.column + prefcol - (new_offset); - self.cursor.column = std::cmp::min(target, rowlen); - } + } else if let Some(prefcol) = self.preferredcol { + let target = self.cursor.column + prefcol - (new_offset); + self.cursor.column = std::cmp::min(target, rowlen); } } fn delete_previous_word(&mut self) { @@ -494,7 +492,7 @@ impl Widget for Field { } fn keyhandler(&mut self, _appdata: &AppData, key: MyKey) { - match key.clone() { + match key { MyKey::ScrollUp => self.visual_up(), MyKey::ScrollDown => self.visual_down(), MyKey::ScrollLeft => self.prev(), diff --git a/src/widgets/textinput/navigation.rs b/src/widgets/textinput/navigation.rs index f7366c5..926ee2b 100644 --- a/src/widgets/textinput/navigation.rs +++ b/src/widgets/textinput/navigation.rs @@ -13,7 +13,7 @@ impl Field { let totrowlen = self.current_rowlen() as u16; let currvisual = self.current_visual_col(); let endofline = self.cursor.column + self.rowlen as usize - currvisual - 1; - let themin = std::cmp::min(totrowlen as usize, endofline as usize); + let themin = std::cmp::min(totrowlen as usize, endofline); self.cursor.column = themin; } @@ -22,7 +22,7 @@ impl Field { while i * self.rowlen < self.cursor.column as u16 { i += 1; } - self.cursor.column = ((i as u16 - 1) * self.rowlen as u16) as usize; + self.cursor.column = ((i - 1) * self.rowlen) as usize; } pub fn start_of_line(&mut self) { self.cursor.column = 0; diff --git a/src/widgets/topics.rs b/src/widgets/topics.rs index 7eed96e..7fdbd50 100644 --- a/src/widgets/topics.rs +++ b/src/widgets/topics.rs @@ -56,16 +56,16 @@ pub struct TopicList { impl TopicList { pub fn new(conn: &Arc>) -> TopicList { - let mut foo = TopicList { + let mut xxx = TopicList { state: ListState::default(), - items: get_topics(&conn).unwrap(), + items: get_topics(conn).unwrap(), writing: None, area: Rect::default(), }; - foo.add_kids(); - foo.next(); - foo.sort_topics(); - foo + xxx.add_kids(); + xxx.next(); + xxx.sort_topics(); + xxx } pub fn is_last_sibling(&self, id: u32) -> bool { @@ -99,17 +99,11 @@ impl TopicList { } pub fn get_selected_id(&self) -> Option { - match self.state.selected() { - None => None, - Some(idx) => Some(self.items[idx as usize].id), - } + self.state.selected().map(|idx| self.items[idx].id) } pub fn get_selected_parent(&self) -> Option { - match self.state.selected() { - None => None, - Some(idx) => Some(self.items[idx as usize].parent), - } + self.state.selected().map(|idx| self.items[idx].parent) } pub fn index_from_id(&self, id: TopicID) -> u32 { @@ -147,13 +141,13 @@ impl TopicList { pub fn children_from_id(&self, id: u32) -> Vec { let topic = self.topic_from_id(id); - let mut kids = topic.children.clone(); + let mut kids = topic.children; kids.sort_unstable_by_key(|topid| self.items[self.index_from_id(*topid) as usize].relpos); kids } pub fn uncles_from_id(&self, id: u32) -> Vec { let grandparent = self.grandparent_from_id(id); - let mut uncles = grandparent.children.clone(); + let mut uncles = grandparent.children; uncles.sort_unstable_by_key(|topid| self.items[self.index_from_id(*topid) as usize].relpos); uncles } @@ -208,20 +202,20 @@ impl TopicList { let uncles = self.uncles_from_id(topic.id); let uncle_qty = uncles.len() as u32; - update_topic_parent(&conn, topic.id, parent.parent); - update_topic_relpos(&conn, topic.id, parent.relpos); + update_topic_parent(conn, topic.id, parent.parent); + update_topic_relpos(conn, topic.id, parent.relpos); for i in parent.relpos..uncle_qty { let uncle_id = uncles[i as usize]; let uncle = self.topic_from_id(uncle_id); - update_topic_relpos(&conn, uncle_id, uncle.relpos + 1); + update_topic_relpos(conn, uncle_id, uncle.relpos + 1); } let siblings = self.siblings_from_id(topic.id); let sibling_qty = siblings.len() as u32; for i in (topic.relpos + 1)..sibling_qty { let sibling = self.topic_from_id(siblings[i as usize]); - update_topic_relpos(&conn, siblings[i as usize], sibling.relpos - 1); + update_topic_relpos(conn, siblings[i as usize], sibling.relpos - 1); } } @@ -229,39 +223,39 @@ impl TopicList { let topic = self.topic_from_index(index); for (index, child) in topic.children.iter().enumerate() { - update_topic_parent(&conn, *child, topic.parent); - update_topic_relpos(&conn, *child, topic.relpos + index as u32); + update_topic_parent(conn, *child, topic.parent); + update_topic_relpos(conn, *child, topic.relpos + index as u32); } - delete_topic(&conn, topic.id).unwrap(); - update_card_topic(&conn, topic.id, topic.parent); // all the cards with the deleted topic - // get assigned to the topic above item + delete_topic(conn, topic.id).unwrap(); + update_card_topic(conn, topic.id, topic.parent); // all the cards with the deleted topic + // get assigned to the topic above item let siblings = self.siblings_from_id(topic.id); let siblingqty = siblings.len() as u32; let kidqty = topic.children.len() as u32; for i in (topic.relpos + 1)..(siblingqty) { - update_topic_relpos(&conn, siblings[(i) as usize], i + kidqty - 1); + update_topic_relpos(conn, siblings[(i) as usize], i + kidqty - 1); } } pub fn shift_right(&mut self, conn: &Arc>, index: u32) { let topic = self.topic_from_index(index); let below = self.topic_from_index(index + 1); - update_topic_parent(&conn, topic.id, below.id); - update_topic_relpos(&conn, topic.id, 0); + update_topic_parent(conn, topic.id, below.id); + update_topic_relpos(conn, topic.id, 0); for child_id in below.children { let child = self.topic_from_id(child_id); - update_topic_relpos(&conn, child_id, child.relpos + 1); + update_topic_relpos(conn, child_id, child.relpos + 1); } let siblings = self.siblings_from_id(topic.id); let sibling_qty = siblings.len() as u32; for i in (topic.relpos + 1)..sibling_qty { let sib = self.topic_from_id(siblings[i as usize]); - update_topic_relpos(&conn, sib.id, sib.relpos - 1); + update_topic_relpos(conn, sib.id, sib.relpos - 1); } } @@ -277,9 +271,8 @@ impl TopicList { // if topic is not the last relpos, shift its relpos one down and the below it one up if topic.relpos != sibling_qty - 1 { - update_topic_relpos(&conn, topic.id, topic.relpos + 1); - update_topic_relpos(&conn, below_sibling.id, topic.relpos); - return; + update_topic_relpos(conn, topic.id, topic.relpos + 1); + update_topic_relpos(conn, below_sibling.id, topic.relpos); } } @@ -289,8 +282,8 @@ impl TopicList { return; } let sibling_above = self.sibling_above(topic.id); - update_topic_relpos(&conn, topic.id, topic.relpos - 1); - update_topic_relpos(&conn, sibling_above.id, topic.relpos); + update_topic_relpos(conn, topic.id, topic.relpos - 1); + update_topic_relpos(conn, sibling_above.id, topic.relpos); } fn dfs(&mut self, id: u32, indices: &mut Vec) { @@ -449,21 +442,21 @@ impl Widget for TopicList { if self.items[index as usize].children.is_empty() { return; } - self.shift_right(&appdata.conn, index as u32); + self.shift_right(&appdata.conn, index); self.reload_topics(&appdata.conn); self.state.select(Some((index + 1) as usize)); } Char('J') => { let index = self.state.selected().unwrap() as u32; let topic = self.items[index as usize].clone(); - self.shift_down(&appdata.conn, index as u32); + self.shift_down(&appdata.conn, index); self.reload_topics(&appdata.conn); let new_index = self.index_from_id(topic.id); self.state.select(Some((new_index) as usize)); } Char('K') => { let index = self.state.selected().unwrap(); - let topic = self.items[index as usize].clone(); + let topic = self.items[index].clone(); self.shift_up(&appdata.conn, index as u32); self.reload_topics(&appdata.conn); let new_index = self.index_from_id(topic.id); @@ -485,7 +478,7 @@ impl Widget for TopicList { let children = self.items[parent_index].children.clone(); let sibling_qty = children.len(); new_topic(&appdata.conn, name, parent, sibling_qty as u32).unwrap(); - let id = *(&appdata.conn.lock().unwrap().last_insert_rowid()) as u32; + let id = appdata.conn.lock().unwrap().last_insert_rowid() as u32; self.writing = Some(NewTopic::new(id)); self.reload_topics(&appdata.conn); } @@ -533,8 +526,8 @@ fn topic2string(topic: &Topic, app: &TopicList) -> String { let mut mystring: String = String::new(); if topic.ancestors > 0 { for ancestor in 0..topic.ancestors - 1 { - let foo = app.ancestor_from_id(topic.id, ancestor + 1); - if app.is_last_sibling(foo.id) { + let xxx = app.ancestor_from_id(topic.id, ancestor + 1); + if app.is_last_sibling(xxx.id) { mystring.insert_str(0, " "); } else { mystring.insert_str(0, "│ ");