From ab14f81900a03a07ef00a6b3232cb29d78e8edf5 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Fri, 27 Oct 2023 11:37:19 +0300 Subject: [PATCH] terminal/cells: make write_string_to_grid a CellBuffer method For future reference, refactoring was done with comby: comby -review ":[w~\s]write_string_to_grid(:[s], &mut :[var],:[rest])" ":[var].write_string_to_grid(:[s],:[rest])" .rs comby -review ":[w~\s]write_string_to_grid(:[s],:[var],:[rest])" ":[var].write_string_to_grid(:[s],:[rest])" .rs Signed-off-by: Manos Pitsidianakis --- meli/src/contacts/editor.rs | 11 +- meli/src/contacts/list.rs | 33 +-- meli/src/jobs_view.rs | 24 +- meli/src/mail/compose.rs | 31 +-- meli/src/mail/compose/edit_attachments.rs | 12 +- meli/src/mail/compose/gpg.rs | 3 +- meli/src/mail/listing.rs | 18 +- meli/src/mail/listing/compact.rs | 45 ++-- meli/src/mail/listing/conversations.rs | 22 +- meli/src/mail/listing/offline.rs | 19 +- meli/src/mail/listing/plain.rs | 45 ++-- meli/src/mail/listing/thread.rs | 31 +-- meli/src/mail/status.rs | 97 +++----- meli/src/mail/view/envelope.rs | 30 +-- meli/src/mail/view/thread.rs | 12 +- meli/src/mailbox_management.rs | 15 +- meli/src/state.rs | 6 +- meli/src/terminal/cells.rs | 267 +++++++++++----------- meli/src/utilities.rs | 48 ++-- meli/src/utilities/dialogs.rs | 29 +-- meli/src/utilities/pager.rs | 6 +- meli/src/utilities/text.rs | 3 +- meli/src/utilities/widgets.rs | 21 +- tools/src/embed.rs | 3 +- 24 files changed, 330 insertions(+), 501 deletions(-) diff --git a/meli/src/contacts/editor.rs b/meli/src/contacts/editor.rs index d5354816..40a4f0b8 100644 --- a/meli/src/contacts/editor.rs +++ b/meli/src/contacts/editor.rs @@ -79,18 +79,16 @@ impl ContactManager { fn initialize(&mut self, context: &Context) { let (width, _) = self.content.size(); - let (x, _) = write_string_to_grid( + let (x, _) = self.content.write_string_to_grid( "Last edited: ", - &mut self.content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, ((0, 0), (width - 1, 0)), None, ); - let (x, y) = write_string_to_grid( + let (x, y) = self.content.write_string_to_grid( &self.card.last_edited(), - &mut self.content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -100,10 +98,9 @@ impl ContactManager { if self.card.external_resource() { self.mode = ViewMode::ReadOnly; - let _ = self.content.resize(self.content.size().0, 2, None); - write_string_to_grid( + _ = self.content.resize(self.content.size().0, 2, None); + self.content.write_string_to_grid( "This contact's origin is external and cannot be edited within meli.", - &mut self.content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, diff --git a/meli/src/contacts/list.rs b/meli/src/contacts/list.rs index 4ea8e93c..3038781a 100644 --- a/meli/src/contacts/list.rs +++ b/meli/src/contacts/list.rs @@ -161,9 +161,8 @@ impl ContactList { for (idx, c) in book_values.iter().enumerate() { self.id_positions.push(*c.id()); - write_string_to_grid( + self.data_columns.columns[0].write_string_to_grid( c.name(), - &mut self.data_columns.columns[0], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -171,9 +170,8 @@ impl ContactList { None, ); - write_string_to_grid( + self.data_columns.columns[1].write_string_to_grid( c.email(), - &mut self.data_columns.columns[1], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -181,9 +179,8 @@ impl ContactList { None, ); - write_string_to_grid( + self.data_columns.columns[2].write_string_to_grid( c.url(), - &mut self.data_columns.columns[2], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -191,13 +188,12 @@ impl ContactList { None, ); - write_string_to_grid( + self.data_columns.columns[3].write_string_to_grid( if c.external_resource() { "external" } else { "local" }, - &mut self.data_columns.columns[3], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -210,9 +206,8 @@ impl ContactList { let message = "Address book is empty.".to_string(); self.data_columns.columns[0] = CellBuffer::new_with_context(message.len(), self.length, None, context); - write_string_to_grid( + self.data_columns.columns[0].write_string_to_grid( &message, - &mut self.data_columns.columns[0], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -282,18 +277,16 @@ impl ContactList { if a.name.grapheme_len() + s.len() > width + 1 { /* Print account name */ - let (x, y) = write_string_to_grid( + let (x, y) = grid.write_string_to_grid( &a.name, - grid, account_attrs.fg, account_attrs.bg, account_attrs.attrs, area, None, ); - write_string_to_grid( + grid.write_string_to_grid( &s, - grid, account_attrs.fg, account_attrs.bg, account_attrs.attrs, @@ -306,9 +299,8 @@ impl ContactList { ), None, ); - write_string_to_grid( + grid.write_string_to_grid( "…", - grid, account_attrs.fg, account_attrs.bg, account_attrs.attrs, @@ -331,18 +323,16 @@ impl ContactList { } else { /* Print account name */ - let (x, y) = write_string_to_grid( + let (x, y) = grid.write_string_to_grid( &a.name, - grid, account_attrs.fg, account_attrs.bg, account_attrs.attrs, area, None, ); - write_string_to_grid( + grid.write_string_to_grid( &s, - grid, account_attrs.fg, account_attrs.bg, account_attrs.attrs, @@ -495,7 +485,7 @@ impl ContactList { continue; } let (column_width, column_height) = self.data_columns.columns[i].size(); - write_string_to_grid( + grid.write_string_to_grid( match i { 0 => "NAME", 1 => "E-MAIL", @@ -503,7 +493,6 @@ impl ContactList { 3 => "SOURCE", _ => "", }, - grid, header_attrs.fg, header_attrs.bg, header_attrs.attrs, diff --git a/meli/src/jobs_view.rs b/meli/src/jobs_view.rs index b2157124..1ef0a2ba 100644 --- a/meli/src/jobs_view.rs +++ b/meli/src/jobs_view.rs @@ -170,9 +170,8 @@ impl JobManager { CellBuffer::new_with_context(self.min_width[4], self.length, None, context); for (idx, e) in self.entries.values().enumerate() { - write_string_to_grid( + self.data_columns.columns[0].write_string_to_grid( &e.id.to_string(), - &mut self.data_columns.columns[0], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -180,9 +179,8 @@ impl JobManager { None, ); - write_string_to_grid( + self.data_columns.columns[1].write_string_to_grid( &e.desc, - &mut self.data_columns.columns[1], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -190,9 +188,8 @@ impl JobManager { None, ); - write_string_to_grid( + self.data_columns.columns[2].write_string_to_grid( &datetime::timestamp_to_string(e.started, Some(RFC3339_DATETIME_AND_SPACE), true), - &mut self.data_columns.columns[2], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -200,7 +197,7 @@ impl JobManager { None, ); - write_string_to_grid( + self.data_columns.columns[3].write_string_to_grid( &if let Some(t) = e.finished { Cow::Owned(datetime::timestamp_to_string( t, @@ -210,7 +207,6 @@ impl JobManager { } else { Cow::Borrowed("null") }, - &mut self.data_columns.columns[3], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -218,13 +214,12 @@ impl JobManager { None, ); - write_string_to_grid( + self.data_columns.columns[4].write_string_to_grid( &if e.finished.is_some() { Cow::Owned(format!("{:?}", e.succeeded)) } else { Cow::Borrowed("-") }, - &mut self.data_columns.columns[4], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -237,9 +232,8 @@ impl JobManager { let message = "No jobs.".to_string(); self.data_columns.columns[0] = CellBuffer::new_with_context(message.len(), self.length, None, context); - write_string_to_grid( + self.data_columns.columns[0].write_string_to_grid( &message, - &mut self.data_columns.columns[0], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -400,9 +394,8 @@ impl Component for JobManager { let mut x_offset = 0; let (upper_left, bottom_right) = area; for (i, (h, w)) in Self::HEADERS.iter().zip(self.min_width).enumerate() { - write_string_to_grid( + grid.write_string_to_grid( h, - grid, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs | Attr::BOLD, @@ -417,9 +410,8 @@ impl Component for JobManager { (false, Asc) => DataColumns::<5>::ARROW_UP, (false, Desc) => DataColumns::<5>::ARROW_DOWN, }; - write_string_to_grid( + grid.write_string_to_grid( arrow, - grid, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, diff --git a/meli/src/mail/compose.rs b/meli/src/mail/compose.rs index ae9e7c90..227b7952 100644 --- a/meli/src/mail/compose.rs +++ b/meli/src/mail/compose.rs @@ -690,7 +690,8 @@ To: {} .map(|k| k.fingerprint()) .collect::>() .join(", "); - write_string_to_grid( + + grid.write_string_to_grid( &format!( "☑ sign with {}", if self.gpg_state.sign_keys.is_empty() { @@ -699,7 +700,6 @@ To: {} key_list.as_str() } ), - grid, theme_default.fg, if self.cursor == Cursor::Sign { crate::conf::value(context, "highlight").bg @@ -711,9 +711,8 @@ To: {} None, ); } else { - write_string_to_grid( + grid.write_string_to_grid( "☐ don't sign", - grid, theme_default.fg, if self.cursor == Cursor::Sign { crate::conf::value(context, "highlight").bg @@ -735,7 +734,7 @@ To: {} .collect::>() .join(", "); - write_string_to_grid( + grid.write_string_to_grid( &format!( "{}{}", if self.gpg_state.encrypt_keys.is_empty() { @@ -749,7 +748,6 @@ To: {} key_list.as_str() } ), - grid, theme_default.fg, if self.cursor == Cursor::Encrypt { crate::conf::value(context, "highlight").bg @@ -761,9 +759,8 @@ To: {} None, ); } else { - write_string_to_grid( + grid.write_string_to_grid( "☐ don't encrypt", - grid, theme_default.fg, if self.cursor == Cursor::Encrypt { crate::conf::value(context, "highlight").bg @@ -776,9 +773,8 @@ To: {} ); } if attachments_no == 0 { - write_string_to_grid( + grid.write_string_to_grid( "no attachments", - grid, theme_default.fg, if self.cursor == Cursor::Attachments { crate::conf::value(context, "highlight").bg @@ -790,9 +786,8 @@ To: {} None, ); } else { - write_string_to_grid( + grid.write_string_to_grid( &format!("{} attachments ", attachments_no), - grid, theme_default.fg, if self.cursor == Cursor::Attachments { crate::conf::value(context, "highlight").bg @@ -805,7 +800,7 @@ To: {} ); for (i, a) in self.draft.attachments().iter().enumerate() { if let Some(name) = a.content_type().name() { - write_string_to_grid( + grid.write_string_to_grid( &format!( "[{}] \"{}\", {} {}", i, @@ -813,7 +808,6 @@ To: {} a.content_type(), melib::BytesDisplay(a.raw.len()) ), - grid, theme_default.fg, theme_default.bg, theme_default.attrs, @@ -821,14 +815,13 @@ To: {} None, ); } else { - write_string_to_grid( + grid.write_string_to_grid( &format!( "[{}] {} {}", i, a.content_type(), melib::BytesDisplay(a.raw.len()) ), - grid, theme_default.fg, theme_default.bg, theme_default.attrs, @@ -951,13 +944,12 @@ impl Component for Composer { ), ); - let (x, y) = write_string_to_grid( + let (x, y) = grid.write_string_to_grid( if self.reply_context.is_some() { "COMPOSING REPLY" } else { "COMPOSING MESSAGE" }, - grid, crate::conf::value(context, "highlight").fg, crate::conf::value(context, "highlight").bg, crate::conf::value(context, "highlight").attrs, @@ -1063,9 +1055,8 @@ impl Component for Composer { .iter() .enumerate() { - write_string_to_grid( + grid.write_string_to_grid( l, - grid, theme_default.fg, theme_default.bg, theme_default.attrs, diff --git a/meli/src/mail/compose/edit_attachments.rs b/meli/src/mail/compose/edit_attachments.rs index 845a8600..17488743 100644 --- a/meli/src/mail/compose/edit_attachments.rs +++ b/meli/src/mail/compose/edit_attachments.rs @@ -125,9 +125,8 @@ impl Component for EditAttachmentsRefMut<'_, '_> { let theme_default = crate::conf::value(context, "theme_default"); clear_area(grid, area, theme_default); if attachments_no == 0 { - write_string_to_grid( + grid.write_string_to_grid( "no attachments", - grid, theme_default.fg, theme_default.bg, theme_default.attrs, @@ -135,9 +134,8 @@ impl Component for EditAttachmentsRefMut<'_, '_> { None, ); } else { - write_string_to_grid( + grid.write_string_to_grid( &format!("{} attachments ", attachments_no), - grid, theme_default.fg, theme_default.bg, theme_default.attrs, @@ -155,7 +153,7 @@ impl Component for EditAttachmentsRefMut<'_, '_> { theme_default.bg }; if let Some(name) = a.content_type().name() { - write_string_to_grid( + grid.write_string_to_grid( &format!( "[{}] \"{}\", {} {}", i, @@ -163,7 +161,6 @@ impl Component for EditAttachmentsRefMut<'_, '_> { a.content_type(), melib::BytesDisplay(a.raw.len()) ), - grid, theme_default.fg, bg, theme_default.attrs, @@ -171,14 +168,13 @@ impl Component for EditAttachmentsRefMut<'_, '_> { None, ); } else { - write_string_to_grid( + grid.write_string_to_grid( &format!( "[{}] {} {}", i, a.content_type(), melib::BytesDisplay(a.raw.len()) ), - grid, theme_default.fg, bg, theme_default.attrs, diff --git a/meli/src/mail/compose/gpg.rs b/meli/src/mail/compose/gpg.rs index 01e40bf0..40d822d9 100644 --- a/meli/src/mail/compose/gpg.rs +++ b/meli/src/mail/compose/gpg.rs @@ -89,9 +89,8 @@ impl Component for KeySelection { } => progress_spinner.draw(grid, center_area(area, (2, 2)), context), KeySelection::Error { ref err, .. } => { let theme_default = crate::conf::value(context, "theme_default"); - write_string_to_grid( + grid.write_string_to_grid( &err.to_string(), - grid, theme_default.fg, theme_default.bg, theme_default.attrs, diff --git a/meli/src/mail/listing.rs b/meli/src/mail/listing.rs index e64a431f..0f385c32 100644 --- a/meli/src/mail/listing.rs +++ b/meli/src/mail/listing.rs @@ -2654,9 +2654,8 @@ impl Listing { }; /* Print account name first */ - write_string_to_grid( + self.menu_content.write_string_to_grid( &self.accounts[aidx].name, - &mut self.menu_content, account_attrs.fg, account_attrs.bg, account_attrs.attrs, @@ -2665,9 +2664,8 @@ impl Listing { ); if lines.is_empty() { - write_string_to_grid( + self.menu_content.write_string_to_grid( "offline", - &mut self.menu_content, crate::conf::value(context, "error_message").fg, account_attrs.bg, account_attrs.attrs, @@ -2807,7 +2805,7 @@ impl Listing { .map(|s| s.as_str()) .unwrap_or(" "); - let (x, _) = write_string_to_grid( + let (x, _) = self.menu_content.write_string_to_grid( &if *account_settings!( context[self.accounts[aidx].hash] .listing @@ -2822,7 +2820,6 @@ impl Listing { } else { format!("{:>width$}", l.inc, width = total_mailbox_no_digits) }, - &mut self.menu_content, index_att.fg, index_att.bg, index_att.attrs, @@ -2850,18 +2847,16 @@ impl Listing { } } } - let (x, _) = write_string_to_grid( + let (x, _) = self.menu_content.write_string_to_grid( &branches, - &mut self.menu_content, att.fg, att.bg, att.attrs, ((x, y), bottom_right), None, ); - let (x, _) = write_string_to_grid( + let (x, _) = self.menu_content.write_string_to_grid( context.accounts[self.accounts[aidx].index].mailbox_entries[&l.mailbox_idx].name(), - &mut self.menu_content, att.fg, att.bg, att.attrs, @@ -2881,9 +2876,8 @@ impl Listing { (None, Some(coll)) => format!(" ({}) v", coll), }; - let (x, _) = write_string_to_grid( + let (x, _) = self.menu_content.write_string_to_grid( &count_string, - &mut self.menu_content, unread_count_att.fg, unread_count_att.bg, unread_count_att.attrs diff --git a/meli/src/mail/listing/compact.rs b/meli/src/mail/listing/compact.rs index bf744723..aa4bf8e2 100644 --- a/meli/src/mail/listing/compact.rs +++ b/meli/src/mail/listing/compact.rs @@ -289,9 +289,8 @@ impl MailListingTrait for CompactListing { self.data_columns.columns[0] = CellBuffer::new_with_context(message.len(), 1, None, context); self.length = 0; - write_string_to_grid( + self.data_columns.columns[0].write_string_to_grid( message.as_str(), - &mut self.data_columns.columns[0], self.color_cache.theme_default.fg, self.color_cache.theme_default.bg, self.color_cache.theme_default.attrs, @@ -583,9 +582,8 @@ impl MailListingTrait for CompactListing { let message: String = account[&self.cursor_pos.1].status(); self.data_columns.columns[0] = CellBuffer::new_with_context(message.len(), self.length + 1, None, context); - write_string_to_grid( + self.data_columns.columns[0].write_string_to_grid( &message, - &mut self.data_columns.columns[0], self.color_cache.theme_default.fg, self.color_cache.theme_default.bg, self.color_cache.theme_default.attrs, @@ -1237,9 +1235,8 @@ impl CompactListing { columns[2].size().0, columns[3].size().0, ); - let (x, _) = write_string_to_grid( + let (x, _) = columns[0].write_string_to_grid( &idx.to_string(), - &mut columns[0], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -1249,9 +1246,8 @@ impl CompactListing { for c in columns[0].row_iter(x..min_width.0, idx) { columns[0][c].set_bg(row_attr.bg).set_ch(' '); } - let (x, _) = write_string_to_grid( + let (x, _) = columns[1].write_string_to_grid( &strings.date, - &mut columns[1], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -1261,9 +1257,8 @@ impl CompactListing { for c in columns[1].row_iter(x..min_width.1, idx) { columns[1][c].set_bg(row_attr.bg).set_ch(' '); } - let (x, _) = write_string_to_grid( + let (x, _) = columns[2].write_string_to_grid( &strings.from, - &mut columns[2], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -1273,18 +1268,16 @@ impl CompactListing { for c in columns[2].row_iter(x..min_width.2, idx) { columns[2][c].set_bg(row_attr.bg).set_ch(' '); } - let (x, _) = write_string_to_grid( + let (x, _) = columns[3].write_string_to_grid( &strings.flag, - &mut columns[3], row_attr.fg, row_attr.bg, row_attr.attrs, ((0, idx), (min_width.3, idx)), None, ); - let (x, _) = write_string_to_grid( + let (x, _) = columns[3].write_string_to_grid( &strings.subject, - &mut columns[3], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -1298,9 +1291,8 @@ impl CompactListing { let mut x = x + 1; for (t, &color) in strings.tags.split_whitespace().zip(strings.tags.1.iter()) { let color = color.unwrap_or(self.color_cache.tag_default.bg); - let (_x, _) = write_string_to_grid( + let (_x, _) = columns[3].write_string_to_grid( t, - &mut columns[3], self.color_cache.tag_default.fg, color, self.color_cache.tag_default.attrs, @@ -1372,9 +1364,8 @@ impl CompactListing { panic!(); } let row_attr = self.rows.row_attr_cache[&idx]; - let (x, _) = write_string_to_grid( + let (x, _) = self.data_columns.columns[0].write_string_to_grid( &idx.to_string(), - &mut self.data_columns.columns[0], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -1386,9 +1377,8 @@ impl CompactListing { .set_bg(row_attr.bg) .set_attrs(row_attr.attrs); } - let (x, _) = write_string_to_grid( + let (x, _) = self.data_columns.columns[1].write_string_to_grid( &strings.date, - &mut self.data_columns.columns[1], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -1400,9 +1390,8 @@ impl CompactListing { .set_bg(row_attr.bg) .set_attrs(row_attr.attrs); } - let (x, _) = write_string_to_grid( + let (x, _) = self.data_columns.columns[2].write_string_to_grid( &strings.from, - &mut self.data_columns.columns[2], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -1423,18 +1412,16 @@ impl CompactListing { .set_bg(row_attr.bg) .set_attrs(row_attr.attrs); } - let (x, _) = write_string_to_grid( + let (x, _) = self.data_columns.columns[3].write_string_to_grid( &strings.flag, - &mut self.data_columns.columns[3], row_attr.fg, row_attr.bg, row_attr.attrs, ((0, idx), (min_width.3, idx)), None, ); - let (x, _) = write_string_to_grid( + let (x, _) = self.data_columns.columns[3].write_string_to_grid( &strings.subject, - &mut self.data_columns.columns[3], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -1454,9 +1441,8 @@ impl CompactListing { let mut x = x + 1; for (t, &color) in strings.tags.split_whitespace().zip(strings.tags.1.iter()) { let color = color.unwrap_or(self.color_cache.tag_default.bg); - let (_x, _) = write_string_to_grid( + let (_x, _) = self.data_columns.columns[3].write_string_to_grid( t, - &mut self.data_columns.columns[3], self.color_cache.tag_default.fg, color, self.color_cache.tag_default.attrs, @@ -1550,13 +1536,12 @@ impl Component for CompactListing { let mut area = area; if !self.filter_term.is_empty() { let (upper_left, bottom_right) = area; - let (x, y) = write_string_to_grid( + let (x, y) = grid.write_string_to_grid( &format!( "{} results for `{}` (Press ESC to exit)", self.filtered_selection.len(), self.filter_term ), - grid, self.color_cache.theme_default.fg, self.color_cache.theme_default.bg, self.color_cache.theme_default.attrs, diff --git a/meli/src/mail/listing/conversations.rs b/meli/src/mail/listing/conversations.rs index 36eed3cb..c25c39ed 100644 --- a/meli/src/mail/listing/conversations.rs +++ b/meli/src/mail/listing/conversations.rs @@ -464,9 +464,9 @@ impl ListingTrait for ConversationsListing { let bottom_right = bottom_right!(area); if let Err(message) = self.error.as_ref() { clear_area(grid, area, self.color_cache.theme_default); - write_string_to_grid( + + grid.write_string_to_grid( message, - grid, self.color_cache.theme_default.fg, self.color_cache.theme_default.bg, self.color_cache.theme_default.attrs, @@ -904,9 +904,8 @@ impl ConversationsListing { self.rows.is_thread_selected(*thread_hash) ); /* draw flags */ - let (x, _) = write_string_to_grid( + let (x, _) = grid.write_string_to_grid( &strings.flag, - grid, row_attr.fg, row_attr.bg, row_attr.attrs, @@ -924,9 +923,8 @@ impl ConversationsListing { self.rows.is_thread_selected(*thread_hash) ); /* draw subject */ - let (mut x, subject_overflowed) = write_string_to_grid( + let (mut x, subject_overflowed) = grid.write_string_to_grid( &strings.subject, - grid, subject_attr.fg, subject_attr.bg, subject_attr.attrs, @@ -939,9 +937,8 @@ impl ConversationsListing { break; }; let color = color.unwrap_or(self.color_cache.tag_default.bg); - let (_x, _y) = write_string_to_grid( + let (_x, _y) = grid.write_string_to_grid( t, - grid, self.color_cache.tag_default.fg, color, self.color_cache.tag_default.attrs, @@ -984,9 +981,8 @@ impl ConversationsListing { return; } /* Next line, draw date */ - let (x, _) = write_string_to_grid( + let (x, _) = grid.write_string_to_grid( &strings.date, - grid, date_attr.fg, date_attr.bg, date_attr.attrs, @@ -1007,9 +1003,8 @@ impl ConversationsListing { self.rows.is_thread_selected(*thread_hash) ); /* draw from */ - let (x, _) = write_string_to_grid( + let (x, _) = grid.write_string_to_grid( &strings.from, - grid, from_attr.fg, from_attr.bg, from_attr.attrs, @@ -1047,13 +1042,12 @@ impl Component for ConversationsListing { let mut area = area; if !self.filter_term.is_empty() { - let (x, y) = write_string_to_grid( + let (x, y) = grid.write_string_to_grid( &format!( "{} results for `{}` (Press ESC to exit)", self.filtered_selection.len(), self.filter_term ), - grid, self.color_cache.theme_default.fg, self.color_cache.theme_default.bg, self.color_cache.theme_default.attrs, diff --git a/meli/src/mail/listing/offline.rs b/meli/src/mail/listing/offline.rs index 25d1509a..214e59f9 100644 --- a/meli/src/mail/listing/offline.rs +++ b/meli/src/mail/listing/offline.rs @@ -141,18 +141,17 @@ impl Component for OfflineListing { let error_message = conf::value(context, "error_message"); clear_area(grid, area, theme_default); if let Err(err) = context.is_online(self.cursor_pos.0) { - let (x, _) = write_string_to_grid( + let (x, _) = grid.write_string_to_grid( "offline: ", - grid, error_message.fg, error_message.bg, error_message.attrs, area, None, ); - write_string_to_grid( + + grid.write_string_to_grid( &err.to_string(), - grid, error_message.fg, error_message.bg, error_message.attrs, @@ -160,9 +159,8 @@ impl Component for OfflineListing { Some(get_x(upper_left!(area))), ); if let Some(msg) = self.messages.last() { - write_string_to_grid( + grid.write_string_to_grid( msg, - grid, text_unfocused.fg, text_unfocused.bg, Attr::BOLD, @@ -171,9 +169,8 @@ impl Component for OfflineListing { ); } for (i, msg) in self.messages.iter().rev().skip(1).enumerate() { - write_string_to_grid( + grid.write_string_to_grid( msg, - grid, text_unfocused.fg, text_unfocused.bg, text_unfocused.attrs, @@ -182,9 +179,8 @@ impl Component for OfflineListing { ); } } else { - let (_, mut y) = write_string_to_grid( + let (_, mut y) = grid.write_string_to_grid( "loading...", - grid, conf::value(context, "highlight").fg, conf::value(context, "highlight").bg, conf::value(context, "highlight").attrs, @@ -197,9 +193,8 @@ impl Component for OfflineListing { .collect(); jobs.sort_by_key(|(j, _)| *j); for (job_id, j) in jobs { - write_string_to_grid( + grid.write_string_to_grid( &format!("{}: {:?}", job_id, j), - grid, text_unfocused.fg, text_unfocused.bg, text_unfocused.attrs, diff --git a/meli/src/mail/listing/plain.rs b/meli/src/mail/listing/plain.rs index 4dcad30e..7f99f727 100644 --- a/meli/src/mail/listing/plain.rs +++ b/meli/src/mail/listing/plain.rs @@ -232,9 +232,8 @@ impl MailListingTrait for PlainListing { self.data_columns.columns[0] = CellBuffer::new_with_context(message.len(), 1, None, context); self.length = 0; - write_string_to_grid( + self.data_columns.columns[0].write_string_to_grid( message.as_str(), - &mut self.data_columns.columns[0], self.color_cache.theme_default.fg, self.color_cache.theme_default.bg, self.color_cache.theme_default.attrs, @@ -919,9 +918,8 @@ impl PlainListing { let row_attr = self.rows.row_attr_cache[&idx]; - let (x, _) = write_string_to_grid( + let (x, _) = columns[0].write_string_to_grid( &idx.to_string(), - &mut columns[0], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -931,9 +929,8 @@ impl PlainListing { for c in columns[0].row_iter(x..min_width.0, idx) { columns[0][c].set_bg(row_attr.bg).set_attrs(row_attr.attrs); } - let (x, _) = write_string_to_grid( + let (x, _) = columns[1].write_string_to_grid( &strings.date, - &mut columns[1], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -943,9 +940,8 @@ impl PlainListing { for c in columns[1].row_iter(x..min_width.1, idx) { columns[1][c].set_bg(row_attr.bg).set_attrs(row_attr.attrs); } - let (x, _) = write_string_to_grid( + let (x, _) = columns[2].write_string_to_grid( &strings.from, - &mut columns[2], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -955,18 +951,16 @@ impl PlainListing { for c in columns[2].row_iter(x..min_width.2, idx) { columns[2][c].set_bg(row_attr.bg).set_attrs(row_attr.attrs); } - let (x, _) = write_string_to_grid( + let (x, _) = columns[3].write_string_to_grid( &strings.flag, - &mut columns[3], row_attr.fg, row_attr.bg, row_attr.attrs, ((0, idx), (min_width.3, idx)), None, ); - let (x, _) = write_string_to_grid( + let (x, _) = columns[3].write_string_to_grid( &strings.subject, - &mut columns[3], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -977,9 +971,8 @@ impl PlainListing { let mut x = x + 1; for (t, &color) in strings.tags.split_whitespace().zip(strings.tags.1.iter()) { let color = color.unwrap_or(self.color_cache.tag_default.bg); - let (_x, _) = write_string_to_grid( + let (_x, _) = columns[3].write_string_to_grid( t, - &mut columns[3], self.color_cache.tag_default.fg, color, self.color_cache.tag_default.attrs, @@ -1010,9 +1003,8 @@ impl PlainListing { let message: String = account[&self.cursor_pos.1].status(); self.data_columns.columns[0] = CellBuffer::new_with_context(message.len(), self.length + 1, None, context); - write_string_to_grid( + self.data_columns.columns[0].write_string_to_grid( &message, - &mut self.data_columns.columns[0], self.color_cache.theme_default.fg, self.color_cache.theme_default.bg, self.color_cache.theme_default.attrs, @@ -1065,9 +1057,8 @@ impl PlainListing { clear_area(&mut columns[2], ((0, idx), (min_width.2, idx)), row_attr); clear_area(&mut columns[3], ((0, idx), (min_width.3, idx)), row_attr); - let (x, _) = write_string_to_grid( + let (x, _) = columns[0].write_string_to_grid( &idx.to_string(), - &mut columns[0], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -1077,9 +1068,8 @@ impl PlainListing { for c in columns[0].row_iter(x..min_width.0, idx) { columns[0][c].set_bg(row_attr.bg).set_attrs(row_attr.attrs); } - let (x, _) = write_string_to_grid( + let (x, _) = columns[1].write_string_to_grid( &strings.date, - &mut columns[1], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -1089,9 +1079,8 @@ impl PlainListing { for c in columns[1].row_iter(x..min_width.1, idx) { columns[1][c].set_bg(row_attr.bg).set_attrs(row_attr.attrs); } - let (x, _) = write_string_to_grid( + let (x, _) = columns[2].write_string_to_grid( &strings.from, - &mut columns[2], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -1101,18 +1090,16 @@ impl PlainListing { for c in columns[2].row_iter(x..min_width.2, idx) { columns[2][c].set_bg(row_attr.bg).set_attrs(row_attr.attrs); } - let (x, _) = write_string_to_grid( + let (x, _) = columns[3].write_string_to_grid( &strings.flag, - &mut columns[3], row_attr.fg, row_attr.bg, row_attr.attrs, ((0, idx), (min_width.3, idx)), None, ); - let (x, _) = write_string_to_grid( + let (x, _) = columns[3].write_string_to_grid( &strings.subject, - &mut columns[3], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -1123,9 +1110,8 @@ impl PlainListing { let mut x = x + 1; for (t, &color) in strings.tags.split_whitespace().zip(strings.tags.1.iter()) { let color = color.unwrap_or(self.color_cache.tag_default.bg); - let (_x, _) = write_string_to_grid( + let (_x, _) = columns[3].write_string_to_grid( t, - &mut columns[3], self.color_cache.tag_default.fg, color, self.color_cache.tag_default.attrs, @@ -1170,13 +1156,12 @@ impl Component for PlainListing { let mut area = area; if !self.filter_term.is_empty() { let (upper_left, bottom_right) = area; - let (x, y) = write_string_to_grid( + let (x, y) = grid.write_string_to_grid( &format!( "{} results for `{}` (Press ESC to exit)", self.filtered_selection.len(), self.filter_term ), - grid, self.color_cache.theme_default.fg, self.color_cache.theme_default.bg, self.color_cache.theme_default.attrs, diff --git a/meli/src/mail/listing/thread.rs b/meli/src/mail/listing/thread.rs index e0f01475..8e70cf51 100644 --- a/meli/src/mail/listing/thread.rs +++ b/meli/src/mail/listing/thread.rs @@ -218,9 +218,8 @@ impl MailListingTrait for ThreadListing { self.data_columns.columns[0] = CellBuffer::new_with_context(message.len(), 1, None, context); self.length = 0; - write_string_to_grid( + self.data_columns.columns[0].write_string_to_grid( message.as_str(), - &mut self.data_columns.columns[0], self.color_cache.theme_default.fg, self.color_cache.theme_default.bg, self.color_cache.theme_default.attrs, @@ -259,9 +258,8 @@ impl MailListingTrait for ThreadListing { let message: String = account[&self.cursor_pos.1].status(); self.data_columns.columns[0] = CellBuffer::new_with_context(message.len(), 1, None, context); - write_string_to_grid( + self.data_columns.columns[0].write_string_to_grid( message.as_str(), - &mut self.data_columns.columns[0], self.color_cache.theme_default.fg, self.color_cache.theme_default.bg, self.color_cache.theme_default.attrs, @@ -950,9 +948,8 @@ impl ThreadListing { self.rows.selection[env_hash] ); if !*account_settings!(context[self.cursor_pos.0].listing.relative_list_indices) { - let (x, _) = write_string_to_grid( + let (x, _) = self.data_columns.columns[0].write_string_to_grid( &idx.to_string(), - &mut self.data_columns.columns[0], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -965,9 +962,8 @@ impl ThreadListing { .set_attrs(row_attr.attrs); } } - let (x, _) = write_string_to_grid( + let (x, _) = self.data_columns.columns[1].write_string_to_grid( &strings.date, - &mut self.data_columns.columns[1], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -979,9 +975,8 @@ impl ThreadListing { .set_bg(row_attr.bg) .set_attrs(row_attr.attrs); } - let (x, _) = write_string_to_grid( + let (x, _) = self.data_columns.columns[2].write_string_to_grid( &strings.from, - &mut self.data_columns.columns[2], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -1002,9 +997,8 @@ impl ThreadListing { .set_bg(row_attr.bg) .set_attrs(row_attr.attrs); } - let (x, _) = write_string_to_grid( + let (x, _) = self.data_columns.columns[3].write_string_to_grid( &strings.flag, - &mut self.data_columns.columns[3], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -1016,9 +1010,8 @@ impl ThreadListing { .set_bg(row_attr.bg) .set_attrs(row_attr.attrs); } - let (x, _) = write_string_to_grid( + let (x, _) = self.data_columns.columns[4].write_string_to_grid( &strings.subject, - &mut self.data_columns.columns[4], row_attr.fg, row_attr.bg, row_attr.attrs, @@ -1038,9 +1031,8 @@ impl ThreadListing { let mut x = x + 1; for (t, &color) in strings.tags.split_whitespace().zip(strings.tags.1.iter()) { let color = color.unwrap_or(self.color_cache.tag_default.bg); - let (_x, _) = write_string_to_grid( + let (_x, _) = self.data_columns.columns[4].write_string_to_grid( t, - &mut self.data_columns.columns[4], self.color_cache.tag_default.fg, color, self.color_cache.tag_default.attrs, @@ -1146,7 +1138,7 @@ impl ThreadListing { ), row_attr, ); - write_string_to_grid( + self.data_columns.columns[0].write_string_to_grid( &if self.new_cursor_pos.2.saturating_sub(top_idx) == i { self.new_cursor_pos.2.to_string() } else { @@ -1154,14 +1146,14 @@ impl ThreadListing { .abs() .to_string() }, - &mut self.data_columns.columns[0], row_attr.fg, row_attr.bg, row_attr.attrs, ((0, i), (width, i + 1)), None, ); - write_string_to_grid( + + grid.write_string_to_grid( &if self.new_cursor_pos.2.saturating_sub(top_idx) == i { self.new_cursor_pos.2.to_string() } else { @@ -1169,7 +1161,6 @@ impl ThreadListing { .abs() .to_string() }, - grid, row_attr.fg, row_attr.bg, row_attr.attrs, diff --git a/meli/src/mail/status.rs b/meli/src/mail/status.rs index c9461fac..42bf4a53 100644 --- a/meli/src/mail/status.rs +++ b/meli/src/mail/status.rs @@ -70,18 +70,16 @@ impl Component for AccountStatus { self.dirty = false; let (mut width, _) = self.content.size(); let a = &context.accounts[self.account_pos]; - let (_x, _y) = write_string_to_grid( + let (_x, _y) = self.content.write_string_to_grid( "Account ", - &mut self.content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs | Attr::UNDERLINE, ((1, 0), (width - 1, 0)), None, ); - let (_x, _y) = write_string_to_grid( + let (_x, _y) = self.content.write_string_to_grid( a.name(), - &mut self.content, self.theme_default.fg, self.theme_default.bg, Attr::BOLD | Attr::UNDERLINE, @@ -91,9 +89,8 @@ impl Component for AccountStatus { width = self.content.size().0; let mut line = 2; - write_string_to_grid( + self.content.write_string_to_grid( "In-progress jobs:", - &mut self.content, self.theme_default.fg, self.theme_default.bg, Attr::BOLD, @@ -105,9 +102,8 @@ impl Component for AccountStatus { for (job_id, req) in a.active_jobs.iter() { width = self.content.size().0; use crate::accounts::JobRequest; - let (x, y) = write_string_to_grid( + let (x, y) = self.content.write_string_to_grid( &format!("{} {}", req, job_id), - &mut self.content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -120,9 +116,8 @@ impl Component for AccountStatus { | JobRequest::Refresh { mailbox_hash, .. } | JobRequest::Fetch { mailbox_hash, .. } = req { - write_string_to_grid( + self.content.write_string_to_grid( a.mailbox_entries[mailbox_hash].name(), - &mut self.content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -137,9 +132,8 @@ impl Component for AccountStatus { line += 2; width = self.content.size().0; - let (_x, _y) = write_string_to_grid( + let (_x, _y) = self.content.write_string_to_grid( "Tag support: ", - &mut self.content, self.theme_default.fg, self.theme_default.bg, Attr::BOLD, @@ -147,13 +141,12 @@ impl Component for AccountStatus { None, ); width = self.content.size().0; - write_string_to_grid( + self.content.write_string_to_grid( if a.backend_capabilities.supports_tags { "yes" } else { "no" }, - &mut self.content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -162,9 +155,8 @@ impl Component for AccountStatus { ); width = self.content.size().0; line += 1; - let (_x, _y) = write_string_to_grid( + let (_x, _y) = self.content.write_string_to_grid( "Search backend: ", - &mut self.content, self.theme_default.fg, self.theme_default.bg, Attr::BOLD, @@ -172,7 +164,7 @@ impl Component for AccountStatus { None, ); width = self.content.size().0; - write_string_to_grid( + self.content.write_string_to_grid( &match ( a.settings.conf.search_backend(), a.backend_capabilities.supports_search, @@ -192,7 +184,6 @@ impl Component for AccountStatus { } } }, - &mut self.content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -202,9 +193,8 @@ impl Component for AccountStatus { width = self.content.size().0; line += 1; - write_string_to_grid( + self.content.write_string_to_grid( "Special Mailboxes:", - &mut self.content, self.theme_default.fg, self.theme_default.bg, Attr::BOLD, @@ -219,9 +209,8 @@ impl Component for AccountStatus { { width = self.content.size().0; line += 1; - write_string_to_grid( + self.content.write_string_to_grid( &format!("{}: {}", f.path(), f.special_usage()), - &mut self.content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -231,9 +220,8 @@ impl Component for AccountStatus { } line += 2; width = self.content.size().0; - write_string_to_grid( + self.content.write_string_to_grid( "Subscribed mailboxes:", - &mut self.content, self.theme_default.fg, self.theme_default.bg, Attr::BOLD, @@ -245,9 +233,8 @@ impl Component for AccountStatus { width = self.content.size().0; let f: &Mailbox = &a[&mailbox_node.hash].ref_mailbox; if f.is_subscribed() { - write_string_to_grid( + self.content.write_string_to_grid( f.path(), - &mut self.content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -261,9 +248,8 @@ impl Component for AccountStatus { line += 1; width = self.content.size().0; if let Some(ref extensions) = a.backend_capabilities.extensions { - write_string_to_grid( + self.content.write_string_to_grid( "Server Extensions:", - &mut self.content, self.theme_default.fg, self.theme_default.bg, Attr::BOLD, @@ -279,9 +265,8 @@ impl Component for AccountStatus { .unwrap_or(0), ); width = self.content.size().0; - write_string_to_grid( + self.content.write_string_to_grid( "meli support:", - &mut self.content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -291,9 +276,8 @@ impl Component for AccountStatus { line += 1; for (name, status) in extensions.iter() { width = self.content.size().0; - write_string_to_grid( + self.content.write_string_to_grid( name.trim_at_boundary(30), - &mut self.content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -302,61 +286,50 @@ impl Component for AccountStatus { ); width = self.content.size().0; - let (x, y) = match status { - MailBackendExtensionStatus::Unsupported { comment: _ } => write_string_to_grid( - "not supported", - &mut self.content, - Color::Red, - self.theme_default.bg, - self.theme_default.attrs, - ((max_name_width + 6, line), (width - 1, line)), - None, - ), - MailBackendExtensionStatus::Supported { comment: _ } => write_string_to_grid( - "supported", - &mut self.content, - Color::Green, - self.theme_default.bg, - self.theme_default.attrs, - ((max_name_width + 6, line), (width - 1, line)), - None, - ), - MailBackendExtensionStatus::Enabled { comment: _ } => write_string_to_grid( - "enabled", - &mut self.content, - Color::Green, + let (x, y) = { + let (status, color) = match status { + MailBackendExtensionStatus::Unsupported { comment: _ } => { + ("not supported", Color::Red) + } + MailBackendExtensionStatus::Supported { comment: _ } => { + ("supported", Color::Green) + } + MailBackendExtensionStatus::Enabled { comment: _ } => { + ("enabled", Color::Green) + } + }; + self.content.write_string_to_grid( + status, + color, self.theme_default.bg, self.theme_default.attrs, ((max_name_width + 6, line), (width - 1, line)), None, - ), + ) }; match status { MailBackendExtensionStatus::Unsupported { comment } | MailBackendExtensionStatus::Supported { comment } | MailBackendExtensionStatus::Enabled { comment } => { if let Some(s) = comment { - let (x, y) = write_string_to_grid( + let (x, y) = self.content.write_string_to_grid( " (", - &mut self.content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, ((x, y), (width - 1, y)), None, ); - let (x, y) = write_string_to_grid( + let (x, y) = self.content.write_string_to_grid( s, - &mut self.content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, ((x, y), (width - 1, y)), None, ); - write_string_to_grid( + self.content.write_string_to_grid( ")", - &mut self.content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, diff --git a/meli/src/mail/view/envelope.rs b/meli/src/mail/view/envelope.rs index 7adad066..52ce8380 100644 --- a/meli/src/mail/view/envelope.rs +++ b/meli/src/mail/view/envelope.rs @@ -742,9 +742,9 @@ impl Component for EnvelopeView { $({ if sticky || skip_header_ctr == 0 { if y <= get_y(bottom_right) { - let (_x, _y) = write_string_to_grid( + let (_x, _y) = + grid.write_string_to_grid( &format!("{}:", $header), - grid, headers_names.fg, headers_names.bg, headers_names.attrs, @@ -758,9 +758,9 @@ impl Component for EnvelopeView { .set_attrs(headers_area.attrs); } - let (_x, _y) = write_string_to_grid( + let (_x, _y) = + grid.write_string_to_grid( &$string, - grid, headers.fg, headers.bg, headers.attrs, @@ -879,18 +879,16 @@ impl Component for EnvelopeView { (set_y(upper_left, y), set_y(bottom_right, y)), headers_area, ); - let (_x, _) = write_string_to_grid( + let (_x, _) = grid.write_string_to_grid( "List-ID: ", - grid, headers_names.fg, headers_names.bg, headers_names.attrs, (set_y(upper_left, y), bottom_right), None, ); - let (_x, _y) = write_string_to_grid( + let (_x, _y) = grid.write_string_to_grid( id, - grid, headers.fg, headers.bg, headers.attrs, @@ -907,9 +905,8 @@ impl Component for EnvelopeView { } if sticky || skip_header_ctr == 0 { if archive.is_some() || post.is_some() || unsubscribe.is_some() { - let (_x, _y) = write_string_to_grid( + let (_x, _y) = grid.write_string_to_grid( " Available actions: [ ", - grid, headers_names.fg, headers_names.bg, headers_names.attrs, @@ -920,9 +917,8 @@ impl Component for EnvelopeView { y = _y; } if archive.is_some() { - let (_x, _y) = write_string_to_grid( + let (_x, _y) = grid.write_string_to_grid( "list-archive, ", - grid, headers.fg, headers.bg, headers.attrs, @@ -933,9 +929,8 @@ impl Component for EnvelopeView { y = _y; } if post.is_some() { - let (_x, _y) = write_string_to_grid( + let (_x, _y) = grid.write_string_to_grid( "list-post, ", - grid, headers.fg, headers.bg, headers.attrs, @@ -946,9 +941,8 @@ impl Component for EnvelopeView { y = _y; } if unsubscribe.is_some() { - let (_x, _y) = write_string_to_grid( + let (_x, _y) = grid.write_string_to_grid( "list-unsubscribe, ", - grid, headers.fg, headers.bg, headers.attrs, @@ -1247,9 +1241,9 @@ impl Component for EnvelopeView { ); } else { let s = self.cmd_buf.to_string(); - write_string_to_grid( + + grid.write_string_to_grid( &s, - grid, self.view_settings.theme_default.fg, self.view_settings.theme_default.bg, self.view_settings.theme_default.attrs, diff --git a/meli/src/mail/view/thread.rs b/meli/src/mail/view/thread.rs index 1ec61424..fab21451 100644 --- a/meli/src/mail/view/thread.rs +++ b/meli/src/mail/view/thread.rs @@ -377,9 +377,8 @@ impl ThreadView { set_and_join_box(&mut content, index, BoxBoundary::Horizontal); } } - write_string_to_grid( + content.write_string_to_grid( &e.heading, - &mut content, if e.seen { theme_default.fg } else { @@ -444,9 +443,8 @@ impl ThreadView { set_and_join_box(&mut content, index, BoxBoundary::Horizontal); } } - write_string_to_grid( + content.write_string_to_grid( &e.heading, - &mut content, if e.seen { theme_default.fg } else { @@ -754,9 +752,8 @@ impl ThreadView { }); let envelope: EnvelopeRef = account.collection.get_env(i); - let (x, y) = write_string_to_grid( + let (x, y) = grid.write_string_to_grid( &envelope.subject(), - grid, crate::conf::value(context, "highlight").fg, theme_default.bg, theme_default.attrs, @@ -871,9 +868,8 @@ impl ThreadView { }); let envelope: EnvelopeRef = account.collection.get_env(i); - let (x, y) = write_string_to_grid( + let (x, y) = grid.write_string_to_grid( &envelope.subject(), - grid, theme_default.fg, theme_default.bg, theme_default.attrs, diff --git a/meli/src/mailbox_management.rs b/meli/src/mailbox_management.rs index d6038313..05f4d825 100644 --- a/meli/src/mailbox_management.rs +++ b/meli/src/mailbox_management.rs @@ -130,9 +130,8 @@ impl MailboxManager { CellBuffer::new_with_context(min_width.3, self.length, None, context); for (idx, e) in self.entries.values().enumerate() { - write_string_to_grid( + self.data_columns.columns[0].write_string_to_grid( e.name(), - &mut self.data_columns.columns[0], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -140,9 +139,8 @@ impl MailboxManager { None, ); - write_string_to_grid( + self.data_columns.columns[1].write_string_to_grid( e.ref_mailbox.path(), - &mut self.data_columns.columns[1], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -151,9 +149,8 @@ impl MailboxManager { ); let (_unseen, total) = e.ref_mailbox.count().ok().unwrap_or((0, 0)); - write_string_to_grid( + self.data_columns.columns[2].write_string_to_grid( &total.to_string(), - &mut self.data_columns.columns[2], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -161,13 +158,12 @@ impl MailboxManager { None, ); - write_string_to_grid( + self.data_columns.columns[3].write_string_to_grid( if e.ref_mailbox.is_subscribed() { "yes" } else { "no" }, - &mut self.data_columns.columns[3], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -180,9 +176,8 @@ impl MailboxManager { let message = "No mailboxes.".to_string(); self.data_columns.columns[0] = CellBuffer::new_with_context(message.len(), self.length, None, context); - write_string_to_grid( + self.data_columns.columns[0].write_string_to_grid( &message, - &mut self.data_columns.columns[0], self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, diff --git a/meli/src/state.rs b/meli/src/state.rs index 2c081def..cfdb5ad4 100644 --- a/meli/src/state.rs +++ b/meli/src/state.rs @@ -709,9 +709,8 @@ impl State { .chain(Some(String::new())) .chain(Some(datetime::timestamp_to_string(*timestamp, None, false))) { - write_string_to_grid( + self.screen.overlay_grid.write_string_to_grid( &line, - &mut self.screen.overlay_grid, noto_colors.fg, noto_colors.bg, noto_colors.attrs, @@ -722,7 +721,7 @@ impl State { } if self.display_messages.len() > 1 { - write_string_to_grid( + self.screen.overlay_grid.write_string_to_grid( &if self.display_messages_pos == 0 { format!( "Next: {}", @@ -748,7 +747,6 @@ impl State { self.context.settings.shortcuts.general.info_message_next ) }, - &mut self.screen.overlay_grid, noto_colors.fg, noto_colors.bg, noto_colors.attrs, diff --git a/meli/src/terminal/cells.rs b/meli/src/terminal/cells.rs index a4c0d3a5..248ec2a8 100644 --- a/meli/src/terminal/cells.rs +++ b/meli/src/terminal/cells.rs @@ -434,6 +434,138 @@ impl CellBuffer { self.tag_associations.push((tag, (start, end))); } } + + /// Write an `&str` to a `CellBuffer` in a specified `Area` with the passed + /// colors. + pub fn write_string_to_grid( + &mut self, + s: &str, + fg_color: Color, + bg_color: Color, + attrs: Attr, + area: Area, + // The left-most x coordinate. + line_break: Option, + ) -> Pos { + macro_rules! inspect_bounds { + ($grid:ident, $area:ident, $x: ident, $y: ident, $line_break:ident) => { + let bounds = $grid.size(); + let (upper_left, bottom_right) = $area; + if $x > (get_x(bottom_right)) || $x >= get_x(bounds) { + if $grid.growable { + if !$grid.resize(std::cmp::max($x + 1, $grid.cols), $grid.rows, None) { + break; + }; + } else { + $x = get_x(upper_left); + $y += 1; + if let Some(_x) = $line_break { + $x = _x; + } else { + break; + } + } + } + if $y > (get_y(bottom_right)) || $y >= get_y(bounds) { + if $grid.growable { + if !$grid.resize($grid.cols, std::cmp::max($y + 1, $grid.rows), None) { + break; + }; + } else { + return ($x, $y - 1); + } + } + }; + } + + let mut bounds = self.size(); + let upper_left = upper_left!(area); + let bottom_right = bottom_right!(area); + let (mut x, mut y) = upper_left; + if y == get_y(bounds) || x == get_x(bounds) { + if self.growable { + if !self.resize( + std::cmp::max(self.cols, x + 2), + std::cmp::max(self.rows, y + 2), + None, + ) { + return (x, y); + } + bounds = self.size(); + } else { + return (x, y); + } + } + + if y > (get_y(bottom_right)) + || x > get_x(bottom_right) + || y > get_y(bounds) + || x > get_x(bounds) + { + if self.growable { + if !self.resize( + std::cmp::max(self.cols, x + 2), + std::cmp::max(self.rows, y + 2), + None, + ) { + return (x, y); + } + } else { + log::debug!(" Invalid area with string {} and area {:?}", s, area); + return (x, y); + } + } + for c in s.chars() { + inspect_bounds!(self, area, x, y, line_break); + if c == '\r' { + continue; + } + if c == '\n' { + y += 1; + if let Some(_x) = line_break { + x = _x; + inspect_bounds!(self, area, x, y, line_break); + continue; + } else { + break; + } + } + if c == '\t' { + self[(x, y)].set_ch(' '); + x += 1; + inspect_bounds!(self, area, x, y, line_break); + self[(x, y)].set_ch(' '); + } else { + self[(x, y)].set_ch(c); + } + self[(x, y)] + .set_fg(fg_color) + .set_bg(bg_color) + .set_attrs(attrs); + + match wcwidth(u32::from(c)) { + Some(0) | None => { + /* Skip drawing zero width characters */ + self[(x, y)].empty = true; + } + Some(2) => { + /* Grapheme takes more than one column, so the next cell will be + * drawn over. Set it as empty to skip drawing it. */ + x += 1; + inspect_bounds!(self, area, x, y, line_break); + self[(x, y)] = Cell::default(); + self[(x, y)] + .set_fg(fg_color) + .set_bg(bg_color) + .set_attrs(attrs) + .set_empty(true); + } + _ => {} + } + x += 1; + } + (x, y) + } } impl Deref for CellBuffer { @@ -1134,138 +1266,6 @@ pub fn change_theme(grid: &mut CellBuffer, area: Area, theme: ThemeAttribute) { } } -macro_rules! inspect_bounds { - ($grid:ident, $area:ident, $x: ident, $y: ident, $line_break:ident) => { - let bounds = $grid.size(); - let (upper_left, bottom_right) = $area; - if $x > (get_x(bottom_right)) || $x >= get_x(bounds) { - if $grid.growable { - if !$grid.resize(std::cmp::max($x + 1, $grid.cols), $grid.rows, None) { - break; - }; - } else { - $x = get_x(upper_left); - $y += 1; - if let Some(_x) = $line_break { - $x = _x; - } else { - break; - } - } - } - if $y > (get_y(bottom_right)) || $y >= get_y(bounds) { - if $grid.growable { - if !$grid.resize($grid.cols, std::cmp::max($y + 1, $grid.rows), None) { - break; - }; - } else { - return ($x, $y - 1); - } - } - }; -} - -/// Write an `&str` to a `CellBuffer` in a specified `Area` with the passed -/// colors. -pub fn write_string_to_grid( - s: &str, - grid: &mut CellBuffer, - fg_color: Color, - bg_color: Color, - attrs: Attr, - area: Area, - // The left-most x coordinate. - line_break: Option, -) -> Pos { - let mut bounds = grid.size(); - let upper_left = upper_left!(area); - let bottom_right = bottom_right!(area); - let (mut x, mut y) = upper_left; - if y == get_y(bounds) || x == get_x(bounds) { - if grid.growable { - if !grid.resize( - std::cmp::max(grid.cols, x + 2), - std::cmp::max(grid.rows, y + 2), - None, - ) { - return (x, y); - } - bounds = grid.size(); - } else { - return (x, y); - } - } - - if y > (get_y(bottom_right)) - || x > get_x(bottom_right) - || y > get_y(bounds) - || x > get_x(bounds) - { - if grid.growable { - if !grid.resize( - std::cmp::max(grid.cols, x + 2), - std::cmp::max(grid.rows, y + 2), - None, - ) { - return (x, y); - } - } else { - log::debug!(" Invalid area with string {} and area {:?}", s, area); - return (x, y); - } - } - for c in s.chars() { - inspect_bounds!(grid, area, x, y, line_break); - if c == '\r' { - continue; - } - if c == '\n' { - y += 1; - if let Some(_x) = line_break { - x = _x; - inspect_bounds!(grid, area, x, y, line_break); - continue; - } else { - break; - } - } - if c == '\t' { - grid[(x, y)].set_ch(' '); - x += 1; - inspect_bounds!(grid, area, x, y, line_break); - grid[(x, y)].set_ch(' '); - } else { - grid[(x, y)].set_ch(c); - } - grid[(x, y)] - .set_fg(fg_color) - .set_bg(bg_color) - .set_attrs(attrs); - - match wcwidth(u32::from(c)) { - Some(0) | None => { - /* Skip drawing zero width characters */ - grid[(x, y)].empty = true; - } - Some(2) => { - /* Grapheme takes more than one column, so the next cell will be - * drawn over. Set it as empty to skip drawing it. */ - x += 1; - inspect_bounds!(grid, area, x, y, line_break); - grid[(x, y)] = Cell::default(); - grid[(x, y)] - .set_fg(fg_color) - .set_bg(bg_color) - .set_attrs(attrs) - .set_empty(true); - } - _ => {} - } - x += 1; - } - (x, y) -} - /// Completely clear an `Area` with an empty char and the terminal's default /// colors. pub fn clear_area(grid: &mut CellBuffer, area: Area, attributes: ThemeAttribute) { @@ -1851,9 +1851,8 @@ mod tests { ); let width = buf.size().0; for (i, l) in lines.iter().enumerate() { - write_string_to_grid( + buf.write_string_to_grid( l, - &mut buf, Color::Default, Color::Default, Attr::DEFAULT, diff --git a/meli/src/utilities.rs b/meli/src/utilities.rs index b881c30f..cb3f246c 100644 --- a/meli/src/utilities.rs +++ b/meli/src/utilities.rs @@ -131,9 +131,8 @@ impl StatusBar { if !context.settings.terminal.use_color() { attribute.attrs |= Attr::REVERSE; } - let (x, y) = write_string_to_grid( + let (x, y) = grid.write_string_to_grid( &self.status, - grid, attribute.fg, attribute.bg, attribute.attrs, @@ -175,9 +174,8 @@ impl StatusBar { total_lines = *total_lines, has_more_lines = if *has_more_lines { "(+)" } else { "" } ); - write_string_to_grid( + grid.write_string_to_grid( &s, - grid, attribute.fg, attribute.bg, attribute.attrs, @@ -238,9 +236,8 @@ impl StatusBar { fn draw_command_bar(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) { clear_area(grid, area, crate::conf::value(context, "theme_default")); let command_bar = crate::conf::value(context, "status.command_bar"); - let (_, y) = write_string_to_grid( + let (_, y) = grid.write_string_to_grid( self.ex_buffer.as_str(), - grid, command_bar.fg, command_bar.bg, command_bar.attrs, @@ -438,9 +435,8 @@ impl Component for StatusBar { .take(hist_height) .enumerate() { - let (x, y) = write_string_to_grid( + let (x, y) = grid.write_string_to_grid( s.as_str(), - grid, history_hints.fg, history_hints.bg, history_hints.attrs, @@ -453,9 +449,8 @@ impl Component for StatusBar { ), Some(get_x(upper_left!(hist_area))), ); - write_string_to_grid( + grid.write_string_to_grid( &s.description, - grid, history_hints.fg, history_hints.bg, history_hints.attrs, @@ -477,9 +472,8 @@ impl Component for StatusBar { ), history_hints, ); - write_string_to_grid( + grid.write_string_to_grid( &s.as_str()[self.ex_buffer.as_str().len()..], - grid, history_hints.fg, history_hints.bg, history_hints.attrs, @@ -878,9 +872,8 @@ impl Tabbed { } else { tab_unfocused_attribute }; - let (x_, _y_) = write_string_to_grid( + let (x_, _y_) = grid.write_string_to_grid( &format!(" {} ", c), - grid, fg, bg, attrs, @@ -1022,9 +1015,8 @@ impl Component for Tabbed { context.dirty_areas.push_back(dialog_area); clear_area(grid, dialog_area, self.theme_default); let inner_area = create_box(grid, dialog_area); - let (x, y) = write_string_to_grid( + let (x, y) = grid.write_string_to_grid( "shortcuts", - grid, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs | Attr::BOLD, @@ -1034,12 +1026,11 @@ impl Component for Tabbed { ), None, ); - write_string_to_grid( + grid.write_string_to_grid( &format!( "Press {} to close", children_maps[Shortcuts::GENERAL]["toggle_help"] ), - grid, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs | Attr::ITALICS, @@ -1143,9 +1134,8 @@ impl Component for Tabbed { self.help_content = CellBuffer::new_with_context(max_width, max_length + 2, None, context); self.help_content.set_growable(true); - write_string_to_grid( + self.help_content.write_string_to_grid( "use COMMAND \"search\" to find shortcuts", - &mut self.help_content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -1154,9 +1144,8 @@ impl Component for Tabbed { ); let mut idx = 2; for (desc, shortcuts) in children_maps.iter() { - write_string_to_grid( + self.help_content.write_string_to_grid( desc, - &mut self.help_content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -1165,22 +1154,20 @@ impl Component for Tabbed { ); idx += 2; for (k, v) in shortcuts { - let (x, y) = write_string_to_grid( + let (x, y) = self.help_content.write_string_to_grid( &format!( "{: >width$}", format!("{}", v), width = max_first_column_width ), - &mut self.help_content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs | Attr::BOLD, ((2, 2 + idx), (max_width.saturating_sub(2), max_length - 1)), None, ); - write_string_to_grid( + self.help_content.write_string_to_grid( k, - &mut self.help_content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs, @@ -1204,9 +1191,8 @@ impl Component for Tabbed { context.dirty_areas.push_back(dialog_area); clear_area(grid, dialog_area, self.theme_default); let inner_area = create_box(grid, dialog_area); - let (x, y) = write_string_to_grid( + let (x, y) = grid.write_string_to_grid( "shortcuts", - grid, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs | Attr::BOLD, @@ -1216,12 +1202,11 @@ impl Component for Tabbed { ), None, ); - write_string_to_grid( + grid.write_string_to_grid( &format!( "Press {} to close", self.help_curr_views[Shortcuts::GENERAL]["toggle_help"] ), - grid, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs | Attr::ITALICS, @@ -1302,9 +1287,8 @@ impl Component for Tabbed { /* In this case we will be scrolling, so show the user how to do it */ if height.wrapping_div(rows + 1) > 0 || width.wrapping_div(cols + 1) > 0 { - write_string_to_grid( + self.help_content.write_string_to_grid( "Use Up, Down, Left, Right to scroll.", - &mut self.help_content, self.theme_default.fg, self.theme_default.bg, self.theme_default.attrs | Attr::ITALICS, diff --git a/meli/src/utilities/dialogs.rs b/meli/src/utilities/dialogs.rs index 0958d6cb..1409782f 100644 --- a/meli/src/utilities/dialogs.rs +++ b/meli/src/utilities/dialogs.rs @@ -135,9 +135,8 @@ impl Component f * cursor */ self.entries[c].1 = !self.entries[c].1; if self.entries[c].1 { - write_string_to_grid( + self.content.write_string_to_grid( "x", - &mut self.content, highlighted_attrs.fg, highlighted_attrs.bg, highlighted_attrs.attrs, @@ -145,9 +144,8 @@ impl Component f None, ); } else { - write_string_to_grid( + self.content.write_string_to_grid( " ", - &mut self.content, highlighted_attrs.fg, highlighted_attrs.bg, highlighted_attrs.attrs, @@ -462,9 +460,8 @@ impl Component for UIConfirmationDialog { * cursor */ self.entries[c].1 = !self.entries[c].1; if self.entries[c].1 { - write_string_to_grid( + self.content.write_string_to_grid( "x", - &mut self.content, highlighted_attrs.fg, highlighted_attrs.bg, highlighted_attrs.attrs, @@ -472,9 +469,8 @@ impl Component for UIConfirmationDialog { None, ); } else { - write_string_to_grid( + self.content.write_string_to_grid( " ", - &mut self.content, highlighted_attrs.fg, highlighted_attrs.bg, highlighted_attrs.attrs, @@ -819,9 +815,8 @@ impl { - write_string_to_grid( + grid.write_string_to_grid( str, - grid, theme_attr.fg, theme_attr.bg, theme_attr.attrs, @@ -366,9 +365,8 @@ impl Component for for (i, k) in self.layout.iter().enumerate().rev() { let v = self.fields.get_mut(k).unwrap(); /* Write field label */ - write_string_to_grid( + grid.write_string_to_grid( k.as_ref(), - grid, label_attrs.fg, label_attrs.bg, label_attrs.attrs, @@ -654,9 +652,8 @@ where let mut len = 0; for (i, k) in self.layout.iter().enumerate() { let cur_len = k.len(); - write_string_to_grid( + grid.write_string_to_grid( k.as_ref(), - grid, theme_default.fg, if i == self.cursor && self.focus { crate::conf::value(context, "highlight").bg @@ -879,27 +876,24 @@ impl AutoComplete { ); let width = content.cols(); for (i, e) in entries.iter().enumerate() { - let (x, _) = write_string_to_grid( + let (x, _) = content.write_string_to_grid( &e.entry, - &mut content, Color::Byte(23), Color::Byte(7), Attr::DEFAULT, ((0, i), (width - 1, i)), None, ); - write_string_to_grid( + content.write_string_to_grid( &e.description, - &mut content, Color::Byte(23), Color::Byte(7), Attr::ITALICS, ((x + 2, i), (width - 1, i)), None, ); - write_string_to_grid( + content.write_string_to_grid( "▒", - &mut content, Color::Byte(23), Color::Byte(7), Attr::DEFAULT, @@ -1246,12 +1240,11 @@ impl Component for ProgressSpinner { if self.dirty { clear_area(grid, area, self.theme_attr); if self.active { - write_string_to_grid( + grid.write_string_to_grid( match self.kind.as_ref() { Ok(kind) => (Self::KINDS[*kind].1)[self.stage], Err(custom) => custom[self.stage].as_ref(), }, - grid, self.theme_attr.fg, self.theme_attr.bg, self.theme_attr.attrs, diff --git a/tools/src/embed.rs b/tools/src/embed.rs index e158e872..75b82daa 100644 --- a/tools/src/embed.rs +++ b/tools/src/embed.rs @@ -179,9 +179,8 @@ impl Component for EmbedContainer { .iter() .enumerate() { - write_string_to_grid( + grid.write_string_to_grid( l, - grid, theme_default.fg, theme_default.bg, theme_default.attrs,