From a337e2269e584769314cdf325cdeb6e57cb0c622 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Wed, 30 Aug 2023 01:12:45 +0300 Subject: [PATCH] contacts: refactor module structure To prepare for more functionality, refactor contacts module. Signed-off-by: Manos Pitsidianakis --- meli/src/{contacts.rs => contacts/editor.rs} | 19 +++++++-------- .../src/contacts/{contact_list.rs => list.rs} | 18 ++++++++++---- meli/src/contacts/mod.rs | 24 +++++++++++++++++++ meli/src/main.rs | 2 +- melib/src/addressbook.rs | 12 ++++++++++ 5 files changed, 59 insertions(+), 16 deletions(-) rename meli/src/{contacts.rs => contacts/editor.rs} (95%) rename meli/src/contacts/{contact_list.rs => list.rs} (98%) create mode 100644 meli/src/contacts/mod.rs diff --git a/meli/src/contacts.rs b/meli/src/contacts/editor.rs similarity index 95% rename from meli/src/contacts.rs rename to meli/src/contacts/editor.rs index 8ae4cb4f..d5354816 100644 --- a/meli/src/contacts.rs +++ b/meli/src/contacts/editor.rs @@ -23,11 +23,10 @@ use std::collections::HashMap; use melib::Card; -use super::*; - -mod contact_list; - -pub use self::contact_list::*; +use crate::{ + terminal::*, Action::*, CellBuffer, Component, ComponentId, Context, Field, FormWidget, Key, + StatusEvent, TabAction, ThemeAttribute, UIDialog, UIEvent, +}; #[derive(Debug)] enum ViewMode { @@ -44,7 +43,7 @@ pub struct ContactManager { pub card: Card, mode: ViewMode, form: FormWidget, - account_pos: usize, + pub account_pos: usize, content: CellBuffer, theme_default: ThemeAttribute, dirty: bool, @@ -55,12 +54,12 @@ pub struct ContactManager { impl std::fmt::Display for ContactManager { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "contacts") + write!(f, "{}", self.card) } } impl ContactManager { - fn new(context: &Context) -> Self { + pub fn new(context: &Context) -> Self { let theme_default: ThemeAttribute = crate::conf::value(context, "theme_default"); ContactManager { id: ComponentId::default(), @@ -197,7 +196,7 @@ impl Component for ContactManager { if self.can_quit_cleanly(context) { context .replies - .push_back(UIEvent::Action(Tab(Kill(parent_id)))); + .push_back(UIEvent::Action(Tab(TabAction::Kill(parent_id)))); } return true; } @@ -289,7 +288,7 @@ impl Component for ContactManager { ], true, Some(Box::new(move |_, results: &[char]| match results[0] { - 'x' => Some(UIEvent::Action(Tab(Kill(parent_id)))), + 'x' => Some(UIEvent::Action(Tab(TabAction::Kill(parent_id)))), 'n' => None, 'y' => None, _ => None, diff --git a/meli/src/contacts/contact_list.rs b/meli/src/contacts/list.rs similarity index 98% rename from meli/src/contacts/contact_list.rs rename to meli/src/contacts/list.rs index 0ec2675b..4ea8e93c 100644 --- a/meli/src/contacts/contact_list.rs +++ b/meli/src/contacts/list.rs @@ -21,9 +21,13 @@ use std::cmp; -use melib::{backends::AccountHash, text_processing::TextProcessing, CardId, Draft}; +use melib::{backends::AccountHash, log, text_processing::TextProcessing, Card, CardId, Draft}; -use super::*; +use crate::{ + conf, contacts::editor::ContactManager, shortcut, terminal::*, Action::Tab, Component, + ComponentId, Composer, Context, DataColumns, PageMovement, ScrollContext, ScrollUpdate, + ShortcutMaps, Shortcuts, StatusEvent, TabAction, ThemeAttribute, UIEvent, UIMode, +}; #[derive(Debug, PartialEq, Eq)] enum ViewMode { @@ -69,7 +73,7 @@ pub struct ContactList { impl std::fmt::Display for ContactList { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "contact list") + write!(f, "contacts") } } @@ -715,7 +719,9 @@ impl Component for ContactList { composer.set_draft(draft, context); context .replies - .push_back(UIEvent::Action(Tab(New(Some(Box::new(composer)))))); + .push_back(UIEvent::Action(Tab(TabAction::New(Some(Box::new( + composer, + )))))); return true; } @@ -957,7 +963,9 @@ impl Component for ContactList { fn kill(&mut self, uuid: ComponentId, context: &mut Context) { debug_assert!(uuid == self.id); - context.replies.push_back(UIEvent::Action(Tab(Kill(uuid)))); + context + .replies + .push_back(UIEvent::Action(Tab(TabAction::Kill(uuid)))); } fn shortcuts(&self, context: &Context) -> ShortcutMaps { let mut map = self diff --git a/meli/src/contacts/mod.rs b/meli/src/contacts/mod.rs new file mode 100644 index 00000000..c063b1c5 --- /dev/null +++ b/meli/src/contacts/mod.rs @@ -0,0 +1,24 @@ +/* + * meli + * + * Copyright 2023 - Manos Pitsidianakis + * + * This file is part of meli. + * + * meli is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * meli is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with meli. If not, see . + */ + +pub mod list; + +pub mod editor; diff --git a/meli/src/main.rs b/meli/src/main.rs index 0094c40a..70ad1abf 100644 --- a/meli/src/main.rs +++ b/meli/src/main.rs @@ -146,7 +146,7 @@ fn run_app(opt: Opt) -> Result<()> { let window = Box::new(Tabbed::new( vec![ Box::new(listing::Listing::new(&mut state.context)), - Box::new(ContactList::new(&state.context)), + Box::new(contacts::list::ContactList::new(&state.context)), ], &state.context, )); diff --git a/melib/src/addressbook.rs b/melib/src/addressbook.rs index 981761e6..01c8cecb 100644 --- a/melib/src/addressbook.rs +++ b/melib/src/addressbook.rs @@ -103,6 +103,18 @@ pub struct Card { external_resource: bool, } +impl std::fmt::Display for Card { + fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { + if !self.name.is_empty() { + self.name.fmt(fmt) + } else if !self.email.is_empty() { + self.email.fmt(fmt) + } else { + "empty contact".fmt(fmt) + } + } +} + impl AddressBook { pub fn new(display_name: String) -> Self { Self {