diff --git a/tui/src/components/dashboard/schema_list.rs b/tui/src/components/dashboard/collection_list.rs similarity index 81% rename from tui/src/components/dashboard/schema_list.rs rename to tui/src/components/dashboard/collection_list.rs index 0a8aeb8..d01ab44 100644 --- a/tui/src/components/dashboard/schema_list.rs +++ b/tui/src/components/dashboard/collection_list.rs @@ -15,15 +15,15 @@ use ratatui::{ use reqtui::collection::Collection; #[derive(Debug)] -pub struct SchemaListState { +pub struct CollectionListState { selected: Option, pub(super) items: Vec, scroll: usize, } -impl SchemaListState { +impl CollectionListState { pub fn new(items: Vec) -> Self { - SchemaListState { + CollectionListState { selected: None, items, scroll: 0, @@ -44,15 +44,15 @@ impl SchemaListState { } #[derive(Debug, Clone)] -pub struct SchemaList<'a> { - colors: &'a colors::Colors, +pub struct CollectionList<'cl> { + colors: &'cl colors::Colors, min_col_width: u16, row_height: u16, } -impl<'a> SchemaList<'a> { +impl<'a> CollectionList<'a> { pub fn new(colors: &'a colors::Colors) -> Self { - SchemaList { + CollectionList { colors, min_col_width: 30, row_height: 4, @@ -90,13 +90,18 @@ impl<'a> SchemaList<'a> { fn build_card( &self, - state: &SchemaListState, - schema: &Collection, + state: &CollectionListState, + collection: &Collection, index: usize, ) -> Paragraph<'_> { let lines = vec![ - schema.info.name.clone().fg(self.colors.normal.white).into(), - schema + collection + .info + .name + .clone() + .fg(self.colors.normal.white) + .into(), + collection .info .description .clone() @@ -123,8 +128,8 @@ impl<'a> SchemaList<'a> { } } -impl StatefulWidget for SchemaList<'_> { - type State = SchemaListState; +impl StatefulWidget for CollectionList<'_> { + type State = CollectionListState; fn render(self, size: Rect, buf: &mut Buffer, state: &mut Self::State) { let list_size = Rect::new(size.x, size.y, size.width.saturating_sub(3), size.height); @@ -163,7 +168,7 @@ impl StatefulWidget for SchemaList<'_> { .skip(state.scroll) .take(rects.len()) .enumerate() - .map(|(i, schema)| self.build_card(state, schema, i)) + .map(|(i, collection)| self.build_card(state, collection, i)) .for_each(|card| card.render(rects.pop_front().unwrap(), buf)); scrollbar.render(scrollbar_size, buf, &mut scrollbar_state); @@ -178,7 +183,7 @@ mod tests { use ratatui::{backend::TestBackend, buffer::Cell, Terminal}; use reqtui::collection::types::*; - fn sample_schema() -> Collection { + fn sample_collection() -> Collection { Collection { info: Info { name: String::from("any_name"), @@ -192,10 +197,10 @@ mod tests { #[test] fn test_build_layout() { let colors = colors::Colors::default(); - let schema_list = SchemaList::new(&colors); + let collection_list = CollectionList::new(&colors); let size = Rect::new(0, 0, 31, 10); - let layout = schema_list.build_layout(&size); + let layout = collection_list.build_layout(&size); assert!(!layout.is_empty()); assert_eq!(layout.len(), 2); @@ -204,22 +209,22 @@ mod tests { #[test] fn test_items_per_row() { let colors = colors::Colors::default(); - let schema_list = SchemaList::new(&colors); + let collection_list = CollectionList::new(&colors); let zero_items = Rect::new(0, 0, 30, 10); let one_item = Rect::new(0, 0, 31, 10); - let amount = schema_list.items_per_row(&zero_items); + let amount = collection_list.items_per_row(&zero_items); assert_eq!(amount, 0); - let amount = schema_list.items_per_row(&one_item); + let amount = collection_list.items_per_row(&one_item); assert_eq!(amount, 1); } #[test] fn test_build_card() { let colors = colors::Colors::default(); - let schema_list = SchemaList::new(&colors); - let schemas = vec![Collection { + let collection_list = CollectionList::new(&colors); + let collections = vec![Collection { info: Info { name: String::from("any_name"), description: None, @@ -227,7 +232,7 @@ mod tests { path: "any_path".into(), requests: None, }]; - let state = SchemaListState::new(schemas.clone()); + let state = CollectionListState::new(collections.clone()); let lines = vec![ "any_name".fg(colors.normal.white).into(), @@ -240,7 +245,7 @@ mod tests { .border_style(Style::default().fg(colors.primary.hover)), ); - let card = schema_list.build_card(&state, &schemas[0], 0); + let card = collection_list.build_card(&state, &collections[0], 0); assert_eq!(card, expected); } @@ -248,21 +253,21 @@ mod tests { #[test] fn test_rendering() { let colors = colors::Colors::default(); - let schemas = (0..100).map(|_| sample_schema()).collect::>(); + let collections = (0..100).map(|_| sample_collection()).collect::>(); let backend = TestBackend::new(80, 22); let mut terminal = Terminal::new(backend).unwrap(); let size = terminal.size().unwrap(); let mut frame = terminal.get_frame(); - let mut state = SchemaListState::new(schemas.clone()); - let schema_list = SchemaList::new(&colors); + let mut state = CollectionListState::new(collections.clone()); + let collection_list = CollectionList::new(&colors); for cell in &frame.buffer_mut().content { assert_eq!(cell, &Cell::default()); } - schema_list.render(size, frame.buffer_mut(), &mut state); + collection_list.render(size, frame.buffer_mut(), &mut state); for cell in frame .buffer_mut() diff --git a/tui/src/components/dashboard/dashboard.rs b/tui/src/components/dashboard/dashboard.rs index b68946e..cff4b48 100644 --- a/tui/src/components/dashboard/dashboard.rs +++ b/tui/src/components/dashboard/dashboard.rs @@ -1,8 +1,8 @@ use crate::components::{ confirm_popup::ConfirmPopup, dashboard::{ + collection_list::{CollectionList, CollectionListState}, new_collection_form::{FormFocus, FormState, NewCollectionForm}, - schema_list::{SchemaList, SchemaListState}, }, error_popup::ErrorPopup, overlay::draw_overlay, @@ -24,7 +24,7 @@ use tui_big_text::{BigText, PixelSize}; #[derive(Debug, PartialEq)] struct DashboardLayout { - schemas_pane: Rect, + collections_pane: Rect, hint_pane: Rect, help_popup: Rect, title_pane: Rect, @@ -34,12 +34,12 @@ struct DashboardLayout { } #[derive(Debug)] -pub struct CollectionList<'a> { +pub struct CollectionDashboard<'a> { layout: DashboardLayout, - schemas: Vec, + collections: Vec, - list: SchemaList<'a>, - list_state: SchemaListState, + list: CollectionList<'a>, + list_state: CollectionListState, form_state: FormState, colors: &'a colors::Colors, filter: String, @@ -58,22 +58,25 @@ enum PaneFocus { Filter, } -impl<'a> CollectionList<'a> { +impl<'a> CollectionDashboard<'a> { pub fn new( size: Rect, colors: &'a colors::Colors, - schemas: Vec, + collections: Vec, ) -> anyhow::Result { - let mut list_state = SchemaListState::new(schemas.clone()); - schemas.is_empty().not().then(|| list_state.select(Some(0))); + let mut list_state = CollectionListState::new(collections.clone()); + collections + .is_empty() + .not() + .then(|| list_state.select(Some(0))); - Ok(CollectionList { + Ok(CollectionDashboard { list_state, form_state: FormState::default(), colors, layout: build_layout(size), - schemas, - list: SchemaList::new(colors), + collections, + list: CollectionList::new(colors), filter: String::new(), command_sender: None, error_message: String::default(), @@ -88,7 +91,7 @@ impl<'a> CollectionList<'a> { fn filter_list(&mut self) { self.list_state.set_items( - self.schemas + self.collections .clone() .into_iter() .filter(|s| s.info.name.contains(&self.filter)) @@ -136,12 +139,14 @@ impl<'a> CollectionList<'a> { .then(|| { self.list_state .selected() - .and_then(|i| self.schemas.get(i)) - .expect("user should never be allowed to select a non existing schema") + .and_then(|i| self.collections.get(i)) + .expect( + "user should never be allowed to select a non existing collection", + ) }) - .map(|schema| { - tracing::debug!("selected schema: {}", schema.info.name); - Command::SelectCollection(schema.clone()) + .map(|collection| { + tracing::debug!("selected collection: {}", collection.info.name); + Command::SelectCollection(collection.clone()) })); } KeyCode::Char('d') => { @@ -170,7 +175,7 @@ impl<'a> CollectionList<'a> { .map(|i| { usize::min( self.list_state.items.len() - 1, - i + self.list.items_per_row(&self.layout.schemas_pane), + i + self.list.items_per_row(&self.layout.collections_pane), ) }) .or(Some(0)), @@ -183,7 +188,9 @@ impl<'a> CollectionList<'a> { self.list_state .selected() .map(|i| { - i.saturating_sub(self.list.items_per_row(&self.layout.schemas_pane)) + i.saturating_sub( + self.list.items_per_row(&self.layout.collections_pane), + ) }) .or(Some(0)), ); @@ -231,8 +238,11 @@ impl<'a> CollectionList<'a> { tokio::spawn(async move { match reqtui::fs::create_collection(name, description).await { - Ok(schema) => { - if sender_copy.send(Command::CreateCollection(schema)).is_err() { + Ok(collection) => { + if sender_copy + .send(Command::CreateCollection(collection)) + .is_err() + { tracing::error!("failed to send command through channel"); std::process::abort(); } @@ -277,21 +287,21 @@ impl<'a> CollectionList<'a> { .list_state .selected() .expect("deleting when nothing is selected should never happen"); - let schema = self - .schemas + let collection = self + .collections .get(selected) .expect("should never attempt to delete a non existing item"); - let path = schema.path.clone(); + let path = collection.path.clone(); tokio::spawn(async move { - tracing::debug!("attempting to delete schema: {:?}", path); + tracing::debug!("attempting to delete collection: {:?}", path); reqtui::fs::delete_collection(&path) .await - .expect("failed to delete schema from filesystem"); + .expect("failed to delete collection from filesystem"); }); - self.schemas.remove(selected); - self.list_state.set_items(self.schemas.clone()); + self.collections.remove(selected); + self.list_state.set_items(self.collections.clone()); self.list_state.select(None); self.pane_focus = PaneFocus::List; } @@ -392,10 +402,10 @@ impl<'a> CollectionList<'a> { frame.render_widget(filter, self.layout.hint_pane); } - fn draw_schemas_list(&mut self, frame: &mut Frame) { + fn draw_collection_list(&mut self, frame: &mut Frame) { frame.render_stateful_widget( self.list.clone(), - self.layout.schemas_pane, + self.layout.collections_pane, &mut self.list_state, ); } @@ -408,7 +418,7 @@ impl<'a> CollectionList<'a> { Constraint::Length(8), Constraint::Fill(1), ]) - .split(self.layout.schemas_pane)[1]; + .split(self.layout.collections_pane)[1]; let no_matches = BigText::builder() .pixel_size(PixelSize::Quadrant) @@ -431,12 +441,12 @@ impl<'a> CollectionList<'a> { Constraint::Length(8), Constraint::Fill(1), ]) - .split(self.layout.schemas_pane)[1]; + .split(self.layout.collections_pane)[1]; let empty_message = BigText::builder() .pixel_size(PixelSize::Quadrant) .style(Style::default().fg(self.colors.normal.magenta)) - .lines(vec!["No schemas".into()]) + .lines(vec!["No collections".into()]) .alignment(Alignment::Center) .build()?; @@ -473,7 +483,7 @@ impl<'a> CollectionList<'a> { .selected() .expect("attempted to open confirm popup without an item selected"); let selected_item_name = &self - .schemas + .collections .get(selected_index) .expect("should never be able to have an out of bounds selection") .info @@ -503,13 +513,16 @@ impl<'a> CollectionList<'a> { } } -impl Page for CollectionList<'_> { +impl Page for CollectionDashboard<'_> { fn draw(&mut self, frame: &mut Frame, size: Rect) -> anyhow::Result<()> { self.draw_background(size, frame); self.draw_title(frame)?; - match (self.schemas.is_empty(), self.list_state.items.is_empty()) { - (false, false) => self.draw_schemas_list(frame), + match ( + self.collections.is_empty(), + self.list_state.items.is_empty(), + ) { + (false, false) => self.draw_collection_list(frame), (false, true) => self.draw_no_matches_text(frame)?, (true, true) => self.draw_empty_message(frame)?, (true, false) => unreachable!(), @@ -537,7 +550,7 @@ impl Page for CollectionList<'_> { } } -impl Eventful for CollectionList<'_> { +impl Eventful for CollectionDashboard<'_> { fn handle_key_event(&mut self, key_event: KeyEvent) -> anyhow::Result> { if let (KeyCode::Char('c'), KeyModifiers::CONTROL) = (key_event.code, key_event.modifiers) { return Ok(Some(Command::Quit)); @@ -564,7 +577,7 @@ fn build_layout(size: Rect) -> DashboardLayout { .constraints([Constraint::Fill(1), Constraint::Length(1)]) .areas(size); - let [_, title_pane, schemas_pane] = Layout::default() + let [_, title_pane, collections_pane] = Layout::default() .direction(Direction::Vertical) .constraints([ Constraint::Length(1), @@ -599,7 +612,7 @@ fn build_layout(size: Rect) -> DashboardLayout { ); DashboardLayout { - schemas_pane, + collections_pane, hint_pane: help_pane, title_pane, help_popup, @@ -621,14 +634,14 @@ mod tests { use super::*; - fn setup_temp_schemas(amount: usize) -> (TempDir, String) { + fn setup_temp_collections(amount: usize) -> (TempDir, String) { let tmp_data_dir = tempdir().expect("Failed to create temp data dir"); - let tmp_dir = tmp_data_dir.path().join("schemas"); - create_dir(&tmp_dir).expect("Failed to create schemas directory"); + let tmp_dir = tmp_data_dir.path().join("collections"); + create_dir(&tmp_dir).expect("Failed to create collections directory"); for i in 0..amount { - let file_path = tmp_dir.join(format!("test_schema_{}.json", i)); + let file_path = tmp_dir.join(format!("test_collection_{}.json", i)); let mut tmp_file = File::create(&file_path).expect("Failed to create file"); write!( @@ -643,7 +656,7 @@ mod tests { (tmp_data_dir, tmp_dir.to_string_lossy().to_string()) } - fn feed_keys(dashboard: &mut CollectionList, events: &[KeyEvent]) { + fn feed_keys(dashboard: &mut CollectionDashboard, events: &[KeyEvent]) { for event in events { _ = dashboard.handle_key_event(*event); } @@ -653,7 +666,7 @@ mod tests { fn test_build_layout() { let size = Rect::new(0, 0, 80, 24); let expected = DashboardLayout { - schemas_pane: Rect::new(1, 6, 79, 17), + collections_pane: Rect::new(1, 6, 79, 17), hint_pane: Rect::new(1, 23, 79, 1), title_pane: Rect::new(1, 1, 79, 5), help_popup: Rect::new(14, 5, 50, 14), @@ -671,12 +684,12 @@ mod tests { fn test_open_close_help() { let size = Rect::new(0, 0, 80, 24); let colors = colors::Colors::default(); - let (_guard, path) = setup_temp_schemas(1); - let schemas = collection::collection::get_collections(path).unwrap(); + let (_guard, path) = setup_temp_collections(1); + let collection = collection::collection::get_collections(path).unwrap(); - let mut dashboard = CollectionList::new(size, &colors, schemas).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, collection).unwrap(); - assert_eq!(dashboard.schemas.len(), 1); + assert_eq!(dashboard.collections.len(), 1); assert_eq!(dashboard.list_state.selected(), Some(0)); assert_eq!(dashboard.pane_focus, PaneFocus::List); @@ -697,12 +710,12 @@ mod tests { } #[test] - fn test_actions_without_any_schemas() { + fn test_actions_without_any_collections() { let size = Rect::new(0, 0, 80, 24); let colors = colors::Colors::default(); - let mut dashboard = CollectionList::new(size, &colors, vec![]).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, vec![]).unwrap(); - assert!(dashboard.schemas.is_empty()); + assert!(dashboard.collections.is_empty()); assert_eq!(dashboard.list_state.selected(), None); feed_keys( @@ -716,7 +729,7 @@ mod tests { ], ); - assert!(dashboard.schemas.is_empty()); + assert!(dashboard.collections.is_empty()); assert_eq!(dashboard.list_state.selected(), None); } @@ -724,12 +737,12 @@ mod tests { fn test_filtering_list() { let size = Rect::new(0, 0, 80, 24); let colors = colors::Colors::default(); - let (_guard, path) = setup_temp_schemas(10); - let schemas = collection::collection::get_collections(path).unwrap(); + let (_guard, path) = setup_temp_collections(10); + let collections = collection::collection::get_collections(path).unwrap(); - let mut dashboard = CollectionList::new(size, &colors, schemas).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, collections).unwrap(); - assert_eq!(dashboard.schemas.len(), 10); + assert_eq!(dashboard.collections.len(), 10); assert_eq!(dashboard.list_state.selected(), Some(0)); feed_keys( @@ -792,10 +805,10 @@ mod tests { fn test_moving_out_of_bounds() { let size = Rect::new(0, 0, 80, 24); let colors = colors::Colors::default(); - let (_guard, path) = setup_temp_schemas(3); - let schemas = collection::collection::get_collections(path).unwrap(); + let (_guard, path) = setup_temp_collections(3); + let collections = collection::collection::get_collections(path).unwrap(); - let mut dashboard = CollectionList::new(size, &colors, schemas).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, collections).unwrap(); feed_keys( &mut dashboard, @@ -827,13 +840,13 @@ mod tests { } #[test] - fn test_creating_new_schema() { + fn test_creating_new_collections() { let size = Rect::new(0, 0, 80, 24); let colors = colors::Colors::default(); - let (_guard, path) = setup_temp_schemas(3); - let schemas = collection::collection::get_collections(path).unwrap(); + let (_guard, path) = setup_temp_collections(3); + let collections = collection::collection::get_collections(path).unwrap(); - let mut dashboard = CollectionList::new(size, &colors, schemas).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, collections).unwrap(); feed_keys( &mut dashboard, @@ -904,12 +917,12 @@ mod tests { } #[test] - fn test_prompt_delete_schema() { + fn test_prompt_delete_collections() { let size = Rect::new(0, 0, 80, 24); let colors = colors::Colors::default(); - let (_guard, path) = setup_temp_schemas(3); - let schemas = collection::collection::get_collections(path).unwrap(); - let mut dashboard = CollectionList::new(size, &colors, schemas).unwrap(); + let (_guard, path) = setup_temp_collections(3); + let collections = collection::collection::get_collections(path).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, collections).unwrap(); feed_keys( &mut dashboard, @@ -930,7 +943,7 @@ mod tests { fn test_display_error() { let size = Rect::new(0, 0, 80, 24); let colors = colors::Colors::default(); - let mut dashboard = CollectionList::new(size, &colors, vec![]).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, vec![]).unwrap(); dashboard.display_error("any error message".into()); @@ -942,7 +955,7 @@ mod tests { fn test_draw_background() { let colors = colors::Colors::default(); let size = Rect::new(0, 0, 80, 22); - let dashboard = CollectionList::new(size, &colors, vec![]).unwrap(); + let dashboard = CollectionDashboard::new(size, &colors, vec![]).unwrap(); let mut terminal = Terminal::new(TestBackend::new(80, 22)).unwrap(); let mut frame = terminal.get_frame(); @@ -962,9 +975,9 @@ mod tests { fn test_close_error_popup() { let colors = colors::Colors::default(); let size = Rect::new(0, 0, 80, 22); - let (_guard, path) = setup_temp_schemas(3); - let schemas = collection::collection::get_collections(path).unwrap(); - let mut dashboard = CollectionList::new(size, &colors, schemas).unwrap(); + let (_guard, path) = setup_temp_collections(3); + let collections = collection::collection::get_collections(path).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, collections).unwrap(); dashboard.display_error("any_error_message".into()); assert_eq!(dashboard.pane_focus, PaneFocus::Error); @@ -981,11 +994,11 @@ mod tests { let colors = colors::Colors::default(); let size = Rect::new(0, 0, 80, 22); let new_size = Rect::new(0, 0, 80, 24); - let (_guard, path) = setup_temp_schemas(3); - let schemas = collection::collection::get_collections(path).unwrap(); - let mut dashboard = CollectionList::new(size, &colors, schemas).unwrap(); + let (_guard, path) = setup_temp_collections(3); + let collections = collection::collection::get_collections(path).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, collections).unwrap(); let expected = DashboardLayout { - schemas_pane: Rect::new(1, 6, 79, 17), + collections_pane: Rect::new(1, 6, 79, 17), hint_pane: Rect::new(1, 23, 79, 1), title_pane: Rect::new(1, 1, 79, 5), help_popup: Rect::new(14, 5, 50, 14), diff --git a/tui/src/components/dashboard/mod.rs b/tui/src/components/dashboard/mod.rs index 9fcf926..7510643 100644 --- a/tui/src/components/dashboard/mod.rs +++ b/tui/src/components/dashboard/mod.rs @@ -1,6 +1,6 @@ +mod collection_list; #[allow(clippy::module_inception)] mod dashboard; mod new_collection_form; -mod schema_list; -pub use dashboard::CollectionList; +pub use dashboard::CollectionDashboard; diff --git a/tui/src/screen_manager.rs b/tui/src/screen_manager.rs index 94c2738..8272f86 100644 --- a/tui/src/screen_manager.rs +++ b/tui/src/screen_manager.rs @@ -1,6 +1,6 @@ use crate::{ components::{ - api_explorer::CollectionViewer, dashboard::CollectionList, + api_explorer::CollectionViewer, dashboard::CollectionDashboard, terminal_too_small::TerminalTooSmall, Eventful, Page, }, event_pool::Event, @@ -12,7 +12,7 @@ use tokio::sync::mpsc::UnboundedSender; #[derive(Debug, Clone, PartialEq)] pub enum Screens { - CollectionList, + CollectionDashboard, CollectionViewer, TerminalTooSmall, } @@ -21,8 +21,8 @@ pub enum Screens { /// be seeing at any point by the application, it is the entity behind navigation pub struct ScreenManager<'sm> { terminal_too_small: TerminalTooSmall<'sm>, - collection_list: CollectionList<'sm>, - /// CollectionViewer is a option as we need a selected schema in order to build + collection_list: CollectionDashboard<'sm>, + /// CollectionViewer is a option as we need a selected collection in order to build /// all the components inside collection_viewer: Option>, @@ -44,15 +44,15 @@ impl<'sm> ScreenManager<'sm> { pub fn new( size: Rect, colors: &'sm colors::Colors, - schemas: Vec, + collections: Vec, config: &'sm config::Config, ) -> anyhow::Result { Ok(Self { - curr_screen: Screens::CollectionList, - prev_screen: Screens::CollectionList, + curr_screen: Screens::CollectionDashboard, + prev_screen: Screens::CollectionDashboard, collection_viewer: None, terminal_too_small: TerminalTooSmall::new(colors), - collection_list: CollectionList::new(size, colors, schemas)?, + collection_list: CollectionDashboard::new(size, colors, collections)?, size, colors, config, @@ -77,11 +77,11 @@ impl<'sm> ScreenManager<'sm> { // in such command pub fn handle_command(&mut self, command: Command) { match command { - Command::SelectCollection(schema) | Command::CreateCollection(schema) => { - tracing::debug!("changing to api explorer: {}", schema.info.name); + Command::SelectCollection(collection) | Command::CreateCollection(collection) => { + tracing::debug!("changing to api explorer: {}", collection.info.name); self.switch_screen(Screens::CollectionViewer); let mut collection_viewer = - CollectionViewer::new(self.size, schema, self.colors, self.config); + CollectionViewer::new(self.size, collection, self.colors, self.config); collection_viewer .register_command_handler( self.sender @@ -115,9 +115,11 @@ impl Page for ScreenManager<'_> { Screens::CollectionViewer => self .collection_viewer .as_mut() - .expect("should never be able to switch to editor screen without having a schema") + .expect( + "should never be able to switch to editor screen without having a collection", + ) .draw(frame, frame.size())?, - Screens::CollectionList => self.collection_list.draw(frame, frame.size())?, + Screens::CollectionDashboard => self.collection_list.draw(frame, frame.size())?, Screens::TerminalTooSmall => self.terminal_too_small.draw(frame, frame.size())?, }; @@ -160,9 +162,11 @@ impl Eventful for ScreenManager<'_> { Screens::CollectionViewer => self .collection_viewer .as_mut() - .expect("should never be able to switch to editor screen without having a schema") + .expect( + "should never be able to switch to editor screen without having a collection", + ) .handle_event(event), - Screens::CollectionList => self.collection_list.handle_event(event), + Screens::CollectionDashboard => self.collection_list.handle_event(event), Screens::TerminalTooSmall => Ok(None), } } @@ -180,14 +184,14 @@ mod tests { }; use tempfile::{tempdir, TempDir}; - fn setup_temp_schemas(amount: usize) -> (TempDir, String) { + fn setup_temp_collections(amount: usize) -> (TempDir, String) { let tmp_data_dir = tempdir().expect("Failed to create temp data dir"); - let tmp_dir = tmp_data_dir.path().join("schemas"); - create_dir(&tmp_dir).expect("Failed to create schemas directory"); + let tmp_dir = tmp_data_dir.path().join("collections"); + create_dir(&tmp_dir).expect("Failed to create collections directory"); for i in 0..amount { - let file_path = tmp_dir.join(format!("test_schema_{}.json", i)); + let file_path = tmp_dir.join(format!("test_collection_{}.json", i)); let mut tmp_file = File::create(&file_path).expect("Failed to create file"); write!( @@ -207,10 +211,10 @@ mod tests { let small_in_width = Rect::new(0, 0, 79, 22); let small_in_height = Rect::new(0, 0, 100, 19); let colors = colors::Colors::default(); - let (_guard, path) = setup_temp_schemas(10); - let schemas = collection::collection::get_collections(path).unwrap(); + let (_guard, path) = setup_temp_collections(10); + let collections = collection::collection::get_collections(path).unwrap(); let config = config::load_config(); - let mut sm = ScreenManager::new(small_in_width, &colors, schemas, &config).unwrap(); + let mut sm = ScreenManager::new(small_in_width, &colors, collections, &config).unwrap(); let mut terminal = Terminal::new(TestBackend::new(80, 22)).unwrap(); sm.draw(&mut terminal.get_frame(), small_in_width).unwrap(); @@ -225,20 +229,20 @@ mod tests { let small = Rect::new(0, 0, 79, 22); let enough = Rect::new(0, 0, 80, 22); let colors = colors::Colors::default(); - let (_guard, path) = setup_temp_schemas(10); - let schemas = collection::collection::get_collections(path).unwrap(); + let (_guard, path) = setup_temp_collections(10); + let collections = collection::collection::get_collections(path).unwrap(); let config = config::load_config(); - let mut sm = ScreenManager::new(small, &colors, schemas, &config).unwrap(); + let mut sm = ScreenManager::new(small, &colors, collections, &config).unwrap(); let mut terminal = Terminal::new(TestBackend::new(80, 22)).unwrap(); terminal.resize(small).unwrap(); sm.draw(&mut terminal.get_frame(), small).unwrap(); assert_eq!(sm.curr_screen, Screens::TerminalTooSmall); - assert_eq!(sm.prev_screen, Screens::CollectionList); + assert_eq!(sm.prev_screen, Screens::CollectionDashboard); terminal.resize(enough).unwrap(); sm.draw(&mut terminal.get_frame(), enough).unwrap(); - assert_eq!(sm.curr_screen, Screens::CollectionList); + assert_eq!(sm.curr_screen, Screens::CollectionDashboard); assert_eq!(sm.prev_screen, Screens::TerminalTooSmall); } @@ -247,10 +251,10 @@ mod tests { let initial = Rect::new(0, 0, 80, 22); let expected = Rect::new(0, 0, 100, 22); let colors = colors::Colors::default(); - let (_guard, path) = setup_temp_schemas(10); - let schemas = collection::collection::get_collections(path).unwrap(); + let (_guard, path) = setup_temp_collections(10); + let collection = collection::collection::get_collections(path).unwrap(); let config = config::load_config(); - let mut sm = ScreenManager::new(initial, &colors, schemas, &config).unwrap(); + let mut sm = ScreenManager::new(initial, &colors, collection, &config).unwrap(); sm.resize(expected); @@ -261,7 +265,7 @@ mod tests { fn test_switch_to_explorer_on_select() { let initial = Rect::new(0, 0, 80, 22); let colors = colors::Colors::default(); - let schema = Collection { + let collection = Collection { info: Info { name: String::from("any_name"), description: None, @@ -269,14 +273,14 @@ mod tests { path: "any_path".into(), requests: None, }; - let command = Command::SelectCollection(schema.clone()); - let (_guard, path) = setup_temp_schemas(10); - let schemas = collection::collection::get_collections(path).unwrap(); + let command = Command::SelectCollection(collection.clone()); + let (_guard, path) = setup_temp_collections(10); + let collection = collection::collection::get_collections(path).unwrap(); let config = config::load_config(); let (tx, _) = tokio::sync::mpsc::unbounded_channel::(); - let mut sm = ScreenManager::new(initial, &colors, schemas, &config).unwrap(); + let mut sm = ScreenManager::new(initial, &colors, collection, &config).unwrap(); _ = sm.register_command_handler(tx.clone()); - assert_eq!(sm.curr_screen, Screens::CollectionList); + assert_eq!(sm.curr_screen, Screens::CollectionDashboard); sm.handle_command(command); assert_eq!(sm.curr_screen, Screens::CollectionViewer); @@ -286,10 +290,10 @@ mod tests { fn test_register_command_sender_for_dashboard() { let initial = Rect::new(0, 0, 80, 22); let colors = colors::Colors::default(); - let (_guard, path) = setup_temp_schemas(10); - let schemas = collection::collection::get_collections(path).unwrap(); + let (_guard, path) = setup_temp_collections(10); + let collections = collection::collection::get_collections(path).unwrap(); let config = config::load_config(); - let mut sm = ScreenManager::new(initial, &colors, schemas, &config).unwrap(); + let mut sm = ScreenManager::new(initial, &colors, collections, &config).unwrap(); let (tx, _) = tokio::sync::mpsc::unbounded_channel::(); @@ -302,10 +306,10 @@ mod tests { fn test_quit_event() { let initial = Rect::new(0, 0, 80, 22); let colors = colors::Colors::default(); - let (_guard, path) = setup_temp_schemas(10); - let schemas = collection::collection::get_collections(path).unwrap(); + let (_guard, path) = setup_temp_collections(10); + let collections = collection::collection::get_collections(path).unwrap(); let config = config::load_config(); - let mut sm = ScreenManager::new(initial, &colors, schemas, &config).unwrap(); + let mut sm = ScreenManager::new(initial, &colors, collections, &config).unwrap(); let event = Event::Key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)); diff --git a/tui/tests/dashboard.rs b/tui/tests/dashboard.rs index e7ce06a..6545dde 100644 --- a/tui/tests/dashboard.rs +++ b/tui/tests/dashboard.rs @@ -6,16 +6,16 @@ use std::{ io::Write, }; use tempfile::{tempdir, TempDir}; -use tui::components::{dashboard::CollectionList, Eventful, Page}; +use tui::components::{dashboard::CollectionDashboard, Eventful, Page}; -fn setup_temp_schemas(amount: usize) -> (TempDir, String) { +fn setup_temp_collections(amount: usize) -> (TempDir, String) { let tmp_data_dir = tempdir().expect("Failed to create temp data dir"); - let tmp_dir = tmp_data_dir.path().join("schemas"); - create_dir(&tmp_dir).expect("Failed to create schemas directory"); + let tmp_dir = tmp_data_dir.path().join("collections"); + create_dir(&tmp_dir).expect("Failed to create collections directory"); for i in 0..amount { - let file_path = tmp_dir.join(format!("test_schema_{}.json", i)); + let file_path = tmp_dir.join(format!("test_collection_{}.json", i)); let mut tmp_file = File::create(&file_path).expect("Failed to create file"); write!( @@ -30,7 +30,7 @@ fn setup_temp_schemas(amount: usize) -> (TempDir, String) { (tmp_data_dir, tmp_dir.to_string_lossy().to_string()) } -fn feed_keys(dashboard: &mut CollectionList, events: &[KeyEvent]) { +fn feed_keys(dashboard: &mut CollectionDashboard, events: &[KeyEvent]) { for event in events { _ = dashboard.handle_key_event(*event); } @@ -49,15 +49,15 @@ fn get_rendered_from_buffer(frame: &mut Frame, size: Rect) -> Vec { fn test_draw_empty_message() { let colors = colors::Colors::default(); let size = Rect::new(0, 0, 80, 22); - let mut dashboard = CollectionList::new(size, &colors, vec![]).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, vec![]).unwrap(); let mut terminal = Terminal::new(TestBackend::new(80, 22)).unwrap(); let mut frame = terminal.get_frame(); let expected = [ - " █▖▐▌ ▜▌ ", - " █▜▟▌▟▀▙ ▟▀▀ ▟▀▙ ▐▙▜▖▟▀▙ █▄█▖▝▀▙ ▟▀▀ ", - " █ ▜▌█ █ ▝▀▙ █ ▄ ▐▌▐▌█▀▀ █▜▜▌▟▀█ ▝▀▙ ", - " ▀ ▝▘▝▀▘ ▀▀▘ ▝▀▘ ▀▘▝▘▝▀▘ ▀ ▝▘▝▀▝▘▀▀▘ ", + " █▖▐▌ ▝█ ▝█ ▟ ▀ ", + " █▜▟▌▟▀▙ ▟▀▙ ▟▀▙ █ █ ▟▀▙ ▟▀▙ ▝█▀ ▝█ ▟▀▙ █▀▙ ▟▀▀ ", + " █ ▜▌█ █ █ ▄ █ █ █ █ █▀▀ █ ▄ █▗ █ █ █ █ █ ▝▀▙ ", + " ▀ ▝▘▝▀▘ ▝▀▘ ▝▀▘ ▝▀▘ ▝▀▘ ▝▀▘ ▝▀▘ ▝▘ ▝▀▘ ▝▀▘ ▀ ▀ ▀▀▘ ", ]; dashboard.draw(&mut frame, size).unwrap(); @@ -78,9 +78,9 @@ fn test_draw_empty_message() { fn test_draw_no_matches_message() { let colors = colors::Colors::default(); let size = Rect::new(0, 0, 80, 22); - let (_guard, path) = setup_temp_schemas(3); - let schemas = collection::collection::get_collections(path).unwrap(); - let mut dashboard = CollectionList::new(size, &colors, schemas).unwrap(); + let (_guard, path) = setup_temp_collections(3); + let collections = collection::collection::get_collections(path).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, collections).unwrap(); let mut terminal = Terminal::new(TestBackend::new(80, 22)).unwrap(); let mut frame = terminal.get_frame(); @@ -128,9 +128,9 @@ fn test_draw_no_matches_message() { fn draw_hint_text() { let colors = colors::Colors::default(); let size = Rect::new(0, 0, 80, 22); - let (_guard, path) = setup_temp_schemas(3); - let schemas = collection::collection::get_collections(path).unwrap(); - let mut dashboard = CollectionList::new(size, &colors, schemas).unwrap(); + let (_guard, path) = setup_temp_collections(3); + let collections = collection::collection::get_collections(path).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, collections).unwrap(); let mut terminal = Terminal::new(TestBackend::new(80, 22)).unwrap(); let mut frame = terminal.get_frame(); @@ -148,9 +148,9 @@ fn draw_hint_text() { fn draw_filter_prompt() { let colors = colors::Colors::default(); let size = Rect::new(0, 0, 80, 22); - let (_guard, path) = setup_temp_schemas(3); - let schemas = collection::collection::get_collections(path).unwrap(); - let mut dashboard = CollectionList::new(size, &colors, schemas).unwrap(); + let (_guard, path) = setup_temp_collections(3); + let collections = collection::collection::get_collections(path).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, collections).unwrap(); let mut terminal = Terminal::new(TestBackend::new(80, 22)).unwrap(); let mut frame = terminal.get_frame(); let expected = @@ -184,7 +184,7 @@ fn draw_filter_prompt() { fn test_draw_title() { let colors = colors::Colors::default(); let size = Rect::new(0, 0, 80, 22); - let mut dashboard = CollectionList::new(size, &colors, vec![]).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, vec![]).unwrap(); let mut terminal = Terminal::new(TestBackend::new(80, 22)).unwrap(); let mut frame = terminal.get_frame(); @@ -213,7 +213,7 @@ fn test_draw_title() { fn test_draw_error() { let colors = colors::Colors::default(); let size = Rect::new(0, 0, 80, 22); - let mut dashboard = CollectionList::new(size, &colors, vec![]).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, vec![]).unwrap(); let mut terminal = Terminal::new(TestBackend::new(80, 22)).unwrap(); let mut frame = terminal.get_frame(); @@ -228,10 +228,10 @@ fn test_draw_error() { " │ │ ", " │ │ ", " │ │ ", - " │ │ ", - " │ │▀ ", - " │ │▙ ", - " │ │▘ ", + " █▖▐▌ │ │ ", + " █▜▟▌▟▀▙│ │▙ █▀▙ ▟▀▀ ", + " █ ▜▌█ █│ │█ █ █ ▝▀▙ ", + " ▀ ▝▘▝▀▘│ │▘ ▀ ▀ ▀▀▘ ", " │ │ ", " │ │ ", " │ │ ", @@ -259,7 +259,7 @@ fn test_draw_error() { fn test_draw_help() { let colors = colors::Colors::default(); let size = Rect::new(0, 0, 80, 22); - let mut dashboard = CollectionList::new(size, &colors, vec![]).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, vec![]).unwrap(); let mut terminal = Terminal::new(TestBackend::new(80, 22)).unwrap(); let mut frame = terminal.get_frame(); @@ -308,7 +308,7 @@ fn test_draw_help() { fn test_draw_form_popup() { let colors = colors::Colors::default(); let size = Rect::new(0, 0, 80, 22); - let mut dashboard = CollectionList::new(size, &colors, vec![]).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, vec![]).unwrap(); let mut terminal = Terminal::new(TestBackend::new(80, 22)).unwrap(); let mut frame = terminal.get_frame(); @@ -359,9 +359,9 @@ fn test_draw_form_popup() { fn test_draw_delete_prompt() { let colors = colors::Colors::default(); let size = Rect::new(0, 0, 80, 22); - let (_guard, path) = setup_temp_schemas(3); - let schemas = collection::collection::get_collections(path).unwrap(); - let mut dashboard = CollectionList::new(size, &colors, schemas).unwrap(); + let (_guard, path) = setup_temp_collections(3); + let collections = collection::collection::get_collections(path).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, collections).unwrap(); let mut terminal = Terminal::new(TestBackend::new(80, 22)).unwrap(); let mut frame = terminal.get_frame(); @@ -409,12 +409,12 @@ fn test_draw_delete_prompt() { } #[test] -fn test_draw_schema_list() { +fn test_draw_collections_list() { let colors = colors::Colors::default(); let size = Rect::new(0, 0, 80, 22); - let (_guard, path) = setup_temp_schemas(3); - let schemas = collection::collection::get_collections(path).unwrap(); - let mut dashboard = CollectionList::new(size, &colors, schemas).unwrap(); + let (_guard, path) = setup_temp_collections(3); + let collections = collection::collection::get_collections(path).unwrap(); + let mut dashboard = CollectionDashboard::new(size, &colors, collections).unwrap(); let mut terminal = Terminal::new(TestBackend::new(80, 22)).unwrap(); let mut frame = terminal.get_frame();