contacts: refactor module structure
Tests / Test on ${{ matrix.build }} (linux-amd64, ubuntu-latest, stable, x86_64-unknown-linux-gnu) (pull_request) Successful in 8m14s Details
Tests / Test on ${{ matrix.build }} (linux-amd64, ubuntu-latest, stable, x86_64-unknown-linux-gnu) (push) Successful in 13m51s Details

To prepare for more functionality, refactor contacts module.

Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>
pull/294/head
Manos Pitsidianakis 2023-08-30 01:12:45 +03:00
parent 46636d8748
commit a337e2269e
Signed by: Manos Pitsidianakis
GPG Key ID: 7729C7707F7E09D0
5 changed files with 59 additions and 16 deletions

View File

@ -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<bool>,
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,

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
pub mod list;
pub mod editor;

View File

@ -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,
));

View File

@ -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 {