mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-17 21:37:55 +08:00
use comfy-table instead of builtin impl for table
This is to out-source the table making in terminal to the external library. I removed the in-house table implementation since it is not used any more, and had been replaced by comfy-table, we use this instead. I also have reimplemented table_max_width since new implementation removed max width , with the new implemetation it will work again. Signed-off-by: Perma Alesheikh <me@prma.dev>
This commit is contained in:
committed by
Clément DOUIN
parent
1e448e56eb
commit
098ae380c3
+12
-16
@@ -6,12 +6,8 @@ use tracing::info;
|
||||
#[cfg(feature = "account-sync")]
|
||||
use crate::cache::arg::disable::CacheDisableFlag;
|
||||
use crate::{
|
||||
account::arg::name::AccountNameFlag,
|
||||
backend::Backend,
|
||||
config::TomlConfig,
|
||||
folder::Folders,
|
||||
printer::{PrintTableOpts, Printer},
|
||||
ui::arg::max_width::TableMaxWidthFlag,
|
||||
account::arg::name::AccountNameFlag, backend::Backend, config::TomlConfig, folder::Folders,
|
||||
printer::Printer,
|
||||
};
|
||||
|
||||
/// List all folders.
|
||||
@@ -19,15 +15,20 @@ use crate::{
|
||||
/// This command allows you to list all exsting folders.
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct FolderListCommand {
|
||||
#[command(flatten)]
|
||||
pub table: TableMaxWidthFlag,
|
||||
|
||||
#[cfg(feature = "account-sync")]
|
||||
#[command(flatten)]
|
||||
pub cache: CacheDisableFlag,
|
||||
|
||||
#[command(flatten)]
|
||||
pub account: AccountNameFlag,
|
||||
|
||||
/// The maximum width the table should not exceed.
|
||||
///
|
||||
/// This argument will force the table not to exceed the given
|
||||
/// width in pixels. Columns may shrink with ellipsis in order to
|
||||
/// fit the width.
|
||||
#[arg(long, short = 'w', name = "table_max_width", value_name = "PIXELS")]
|
||||
pub table_max_width: Option<u16>,
|
||||
}
|
||||
|
||||
impl FolderListCommand {
|
||||
@@ -52,12 +53,7 @@ impl FolderListCommand {
|
||||
|
||||
let folders: Folders = backend.list_folders().await?.into();
|
||||
|
||||
printer.print_table(
|
||||
Box::new(folders),
|
||||
PrintTableOpts {
|
||||
format: &account_config.get_message_read_format(),
|
||||
max_width: self.table.max_width,
|
||||
},
|
||||
)
|
||||
printer.print_table(folders, self.table_max_width)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
+51
-15
@@ -3,13 +3,11 @@ pub mod command;
|
||||
pub mod config;
|
||||
|
||||
use color_eyre::Result;
|
||||
use comfy_table::{presets, Attribute, Cell, ContentArrangement, Row, Table};
|
||||
use serde::Serialize;
|
||||
use std::ops;
|
||||
|
||||
use crate::{
|
||||
printer::{PrintTable, PrintTableOpts, WriteColor},
|
||||
ui::{Cell, Row, Table},
|
||||
};
|
||||
use crate::printer::{PrintTable, WriteColor};
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize)]
|
||||
pub struct Folder {
|
||||
@@ -25,24 +23,58 @@ impl From<&email::folder::Folder> for Folder {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<&Folder> for Row {
|
||||
fn from(folder: &Folder) -> Self {
|
||||
let mut row = Row::new();
|
||||
row.add_cell(Cell::new(&folder.name).fg(comfy_table::Color::Blue));
|
||||
row.add_cell(Cell::new(&folder.desc).fg(comfy_table::Color::Green));
|
||||
|
||||
impl Table for Folder {
|
||||
fn head() -> Row {
|
||||
Row::new()
|
||||
.cell(Cell::new("NAME").bold().underline().white())
|
||||
.cell(Cell::new("DESC").bold().underline().white())
|
||||
row
|
||||
}
|
||||
}
|
||||
|
||||
fn row(&self) -> Row {
|
||||
Row::new()
|
||||
.cell(Cell::new(&self.name).blue())
|
||||
.cell(Cell::new(&self.desc).green())
|
||||
impl From<Folder> for Row {
|
||||
fn from(folder: Folder) -> Self {
|
||||
let mut row = Row::new();
|
||||
row.add_cell(Cell::new(folder.name).fg(comfy_table::Color::Blue));
|
||||
row.add_cell(Cell::new(folder.desc).fg(comfy_table::Color::Green));
|
||||
|
||||
row
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize)]
|
||||
pub struct Folders(Vec<Folder>);
|
||||
|
||||
impl From<Folders> for Table {
|
||||
fn from(folders: Folders) -> Self {
|
||||
let mut table = Table::new();
|
||||
table
|
||||
.load_preset(presets::NOTHING)
|
||||
.set_header(Row::from([
|
||||
Cell::new("NAME").add_attribute(Attribute::Reverse),
|
||||
Cell::new("DESC").add_attribute(Attribute::Reverse),
|
||||
]))
|
||||
.add_rows(folders.0.into_iter().map(Row::from));
|
||||
table
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Folders> for Table {
|
||||
fn from(folders: &Folders) -> Self {
|
||||
let mut table = Table::new();
|
||||
table
|
||||
.load_preset(presets::NOTHING)
|
||||
.set_content_arrangement(ContentArrangement::Dynamic)
|
||||
.set_header(Row::from([
|
||||
Cell::new("NAME").add_attribute(Attribute::Reverse),
|
||||
Cell::new("DESC").add_attribute(Attribute::Reverse),
|
||||
]))
|
||||
.add_rows(folders.0.iter().map(Row::from));
|
||||
table
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::Deref for Folders {
|
||||
type Target = Vec<Folder>;
|
||||
|
||||
@@ -58,9 +90,13 @@ impl From<email::folder::Folders> for Folders {
|
||||
}
|
||||
|
||||
impl PrintTable for Folders {
|
||||
fn print_table(&self, writer: &mut dyn WriteColor, opts: PrintTableOpts) -> Result<()> {
|
||||
fn print_table(&self, writer: &mut dyn WriteColor, table_max_width: Option<u16>) -> Result<()> {
|
||||
let mut table = Table::from(self);
|
||||
if let Some(width) = table_max_width {
|
||||
table.set_width(width);
|
||||
}
|
||||
writeln!(writer)?;
|
||||
Table::print(writer, self, opts)?;
|
||||
write!(writer, "{}", table)?;
|
||||
writeln!(writer)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user