From b3a29fbaf8cc1bfbb04e54f17dec132fa50d2d45 Mon Sep 17 00:00:00 2001 From: Waleed Khan Date: Sun, 9 Oct 2022 22:05:43 -0700 Subject: [PATCH] refactor(formatting): `printable_styled_string` -> `Glyphs::render` --- git-branchless-lib/src/core/check_out.rs | 18 +++--- git-branchless-lib/src/core/formatting.rs | 46 +++++++-------- .../src/core/rewrite/execute.rs | 57 ++++++++----------- git-branchless-lib/src/core/rewrite/plan.rs | 14 ++--- .../src/core/rewrite/rewrite_hooks.rs | 16 +++--- git-branchless/src/commands/bug_report.rs | 4 +- git-branchless/src/commands/hide.rs | 6 +- git-branchless/src/commands/hooks.rs | 4 +- git-branchless/src/commands/mod.rs | 12 ++-- git-branchless/src/commands/navigation.rs | 21 +++---- git-branchless/src/commands/query.rs | 8 +-- git-branchless/src/commands/reword.rs | 7 +-- git-branchless/src/commands/smartlog.rs | 4 +- git-branchless/src/commands/snapshot.rs | 12 ++-- git-branchless/src/commands/sync.rs | 21 +++---- git-branchless/src/commands/undo.rs | 4 +- git-branchless/src/tui/prompt.rs | 9 +-- 17 files changed, 112 insertions(+), 151 deletions(-) diff --git a/git-branchless-lib/src/core/check_out.rs b/git-branchless-lib/src/core/check_out.rs index 99c204af9..e69e576c0 100644 --- a/git-branchless-lib/src/core/check_out.rs +++ b/git-branchless-lib/src/core/check_out.rs @@ -20,7 +20,6 @@ use crate::util::ExitCode; use super::config::get_undo_create_snapshots; use super::effects::Effects; use super::eventlog::{Event, EventLogDb, EventTransactionId}; -use super::formatting::printable_styled_string; use super::repo_ext::{RepoExt, RepoReferencesSnapshot}; /// An entity to check out. @@ -141,16 +140,13 @@ pub fn check_out_commit( writeln!( effects.get_output_stream(), "{}", - printable_styled_string( - effects.get_glyphs(), - StyledString::styled( - match target { - Some(target) => format!("Failed to check out commit: {target}"), - None => "Failed to check out commit".to_string(), - }, - BaseColor::Red.light() - ) - )? + effects.get_glyphs().render(StyledString::styled( + match target { + Some(target) => format!("Failed to check out commit: {target}"), + None => "Failed to check out commit".to_string(), + }, + BaseColor::Red.light() + ))? )?; return Ok(exit_code); } diff --git a/git-branchless-lib/src/core/formatting.rs b/git-branchless-lib/src/core/formatting.rs index 4c8bdd065..b50bf0462 100644 --- a/git-branchless-lib/src/core/formatting.rs +++ b/git-branchless-lib/src/core/formatting.rs @@ -200,6 +200,29 @@ impl Glyphs { cycle_lower_left_corner: "└", } } + + /// Write the provided string to `out`, using ANSI escape codes as necessary to + /// style it. + /// + /// TODO: return something that implements `Display` instead of a `String`. + pub fn render(&self, string: StyledString) -> eyre::Result { + let result = string + .spans() + .map(|span| { + let Span { + content, + attr, + width: _, + } = span; + if self.should_write_ansi_escape_codes { + Ok(render_style_as_ansi(content, *attr)?) + } else { + Ok(content.to_string()) + } + }) + .collect::>()?; + Ok(result) + } } impl std::fmt::Debug for Glyphs { @@ -379,26 +402,3 @@ fn render_style_as_ansi(content: &str, style: Style) -> eyre::Result { Ok(output.to_string()) } - -/// Write the provided string to `out`, using ANSI escape codes as necessary to -/// style it. -/// -/// TODO: return something that implements `Display` instead of a `String`. -pub fn printable_styled_string(glyphs: &Glyphs, string: StyledString) -> eyre::Result { - let result = string - .spans() - .map(|span| { - let Span { - content, - attr, - width: _, - } = span; - if glyphs.should_write_ansi_escape_codes { - Ok(render_style_as_ansi(content, *attr)?) - } else { - Ok(content.to_string()) - } - }) - .collect::>()?; - Ok(result) -} diff --git a/git-branchless-lib/src/core/rewrite/execute.rs b/git-branchless-lib/src/core/rewrite/execute.rs index 3f4f793b9..99e03491a 100644 --- a/git-branchless-lib/src/core/rewrite/execute.rs +++ b/git-branchless-lib/src/core/rewrite/execute.rs @@ -11,7 +11,7 @@ use tracing::warn; use crate::core::check_out::{check_out_commit, CheckOutCommitOptions, CheckoutTarget}; use crate::core::effects::Effects; use crate::core::eventlog::{EventLogDb, EventTransactionId}; -use crate::core::formatting::{printable_styled_string, Pluralize}; +use crate::core::formatting::Pluralize; use crate::core::repo_ext::RepoExt; use crate::git::{ GitRunInfo, MaybeZeroOid, NonZeroOid, ReferenceName, Repo, ResolvedReferenceInfo, @@ -347,8 +347,7 @@ impl MergeConflictInfo { amount: self.conflicting_paths.len(), unit: ("conflicting file", "conflicting files"), }, - printable_styled_string( - effects.get_glyphs(), + effects.get_glyphs().render( repo.friendly_describe_commit_from_oid(effects.get_glyphs(), self.commit_oid)? )? )?; @@ -382,7 +381,6 @@ mod in_memory { use crate::core::effects::{Effects, OperationType}; use crate::core::eventlog::EventLogDb; - use crate::core::formatting::printable_styled_string; use crate::core::gc::mark_commit_reachable; use crate::core::rewrite::execute::check_out_updated_head; use crate::core::rewrite::move_branches; @@ -526,10 +524,9 @@ mod in_memory { .wrap_err("Finding commit to apply")?; i += 1; - let commit_description = printable_styled_string( - effects.get_glyphs(), - commit_to_apply.friendly_describe(effects.get_glyphs())?, - )?; + let commit_description = effects + .get_glyphs() + .render(commit_to_apply.friendly_describe(effects.get_glyphs())?)?; let commit_num = format!("[{}/{}]", i, num_picks); progress.notify_progress(i, num_picks); @@ -593,13 +590,13 @@ mod in_memory { let rebased_commit = repo .find_commit_or_fail(rebased_commit_oid) .wrap_err("Looking up just-rebased commit")?; - let commit_description = printable_styled_string( - effects.get_glyphs(), - repo.friendly_describe_commit_from_oid( - effects.get_glyphs(), - rebased_commit_oid, - )?, - )?; + let commit_description = + effects + .get_glyphs() + .render(repo.friendly_describe_commit_from_oid( + effects.get_glyphs(), + rebased_commit_oid, + )?)?; if rebased_commit.is_empty() { rewritten_oids.push((*original_commit_oid, MaybeZeroOid::Zero)); maybe_set_skipped_head_new_oid(*original_commit_oid, current_oid); @@ -654,10 +651,9 @@ mod in_memory { .wrap_err("Finding commit to apply")?; i += 1; - let commit_description = printable_styled_string( - effects.get_glyphs(), - commit_to_apply.friendly_describe(effects.get_glyphs())?, - )?; + let commit_description = effects + .get_glyphs() + .render(commit_to_apply.friendly_describe(effects.get_glyphs())?)?; let commit_num = format!("[{}/{}]", i, num_picks); progress.notify_progress(i, num_picks); @@ -709,13 +705,13 @@ mod in_memory { ) .wrap_err("Applying rebased commit")?; - let commit_description = printable_styled_string( - effects.get_glyphs(), - repo.friendly_describe_commit_from_oid( - effects.get_glyphs(), - rebased_commit_oid, - )?, - )?; + let commit_description = + effects + .get_glyphs() + .render(repo.friendly_describe_commit_from_oid( + effects.get_glyphs(), + rebased_commit_oid, + )?)?; rewritten_oids.push((*commit_oid, MaybeZeroOid::NonZero(rebased_commit_oid))); current_oid = rebased_commit_oid; @@ -736,8 +732,7 @@ mod in_memory { maybe_set_skipped_head_new_oid(*commit_oid, current_oid); let commit_description = commit.friendly_describe(effects.get_glyphs())?; - let commit_description = - printable_styled_string(effects.get_glyphs(), commit_description)?; + let commit_description = effects.get_glyphs().render(commit_description)?; writeln!( effects.get_output_stream(), "{} Skipped commit (was already applied upstream): {}", @@ -1218,8 +1213,7 @@ pub fn execute_rebase_plan( writeln!( effects.get_output_stream(), "The merge commit was: {}", - printable_styled_string( - effects.get_glyphs(), + effects.get_glyphs().render( repo.friendly_describe_commit_from_oid(effects.get_glyphs(), commit_oid)? )?, )?; @@ -1246,8 +1240,7 @@ pub fn execute_rebase_plan( writeln!( effects.get_output_stream(), "The conflicting commit was: {}", - printable_styled_string( - effects.get_glyphs(), + effects.get_glyphs().render( repo.friendly_describe_commit_from_oid(effects.get_glyphs(), commit_oid)? )?, )?; diff --git a/git-branchless-lib/src/core/rewrite/plan.rs b/git-branchless-lib/src/core/rewrite/plan.rs index 55e6f3567..cc0088719 100644 --- a/git-branchless-lib/src/core/rewrite/plan.rs +++ b/git-branchless-lib/src/core/rewrite/plan.rs @@ -13,7 +13,7 @@ use tracing::{instrument, warn}; use crate::core::dag::{commit_set_to_vec, union_all, CommitSet, Dag}; use crate::core::effects::{Effects, OperationType}; -use crate::core::formatting::{printable_styled_string, Pluralize}; +use crate::core::formatting::Pluralize; use crate::core::rewrite::{RepoPool, RepoResource}; use crate::core::task::ResourcePool; use crate::git::{Commit, NonZeroOid, PatchId, Repo}; @@ -600,10 +600,7 @@ impl BuildRebasePlanError { char1, char2, char3, - printable_styled_string( - glyphs, - repo.friendly_describe_commit_from_oid(glyphs, *oid)?, - )?, + glyphs.render(repo.friendly_describe_commit_from_oid(glyphs, *oid)?,)?, )?; } } @@ -628,10 +625,9 @@ Retry with -f/--force-rewrite to proceed anyways.", amount: public_commits_to_move.count()?, unit: ("public commit", "public commits") }, - printable_styled_string( - effects.get_glyphs(), - example_bad_commit.friendly_describe(effects.get_glyphs())? - )?, + effects + .get_glyphs() + .render(example_bad_commit.friendly_describe(effects.get_glyphs())?)?, )?; } diff --git a/git-branchless-lib/src/core/rewrite/rewrite_hooks.rs b/git-branchless-lib/src/core/rewrite/rewrite_hooks.rs index a19382ac0..fdc774745 100644 --- a/git-branchless-lib/src/core/rewrite/rewrite_hooks.rs +++ b/git-branchless-lib/src/core/rewrite/rewrite_hooks.rs @@ -20,7 +20,7 @@ use crate::core::config::{get_hint_enabled, print_hint_suppression_notice, Hint} use crate::core::dag::Dag; use crate::core::effects::Effects; use crate::core::eventlog::{Event, EventLogDb, EventReplayer}; -use crate::core::formatting::{printable_styled_string, Pluralize}; +use crate::core::formatting::Pluralize; use crate::core::repo_ext::RepoExt; use crate::git::{ CategorizedReferenceName, GitRunInfo, MaybeZeroOid, NonZeroOid, ReferenceName, Repo, @@ -459,10 +459,9 @@ pub fn hook_drop_commit_if_empty( writeln!( effects.get_output_stream(), "Skipped now-empty commit: {}", - printable_styled_string( - effects.get_glyphs(), - head_commit.friendly_describe(effects.get_glyphs())? - )? + effects + .get_glyphs() + .render(head_commit.friendly_describe(effects.get_glyphs())?)? )?; repo.set_head(only_parent_oid)?; @@ -498,10 +497,9 @@ pub fn hook_skip_upstream_applied_commit( writeln!( effects.get_output_stream(), "Skipping commit (was already applied upstream): {}", - printable_styled_string( - effects.get_glyphs(), - commit.friendly_describe(effects.get_glyphs())? - )? + effects + .get_glyphs() + .render(commit.friendly_describe(effects.get_glyphs())?)? )?; if let Some(orig_head_reference) = repo.find_reference(&"ORIG_HEAD".into())? { diff --git a/git-branchless/src/commands/bug_report.rs b/git-branchless/src/commands/bug_report.rs index e2d14304d..562f6886b 100644 --- a/git-branchless/src/commands/bug_report.rs +++ b/git-branchless/src/commands/bug_report.rs @@ -15,7 +15,7 @@ use crate::commands::smartlog::{make_smartlog_graph, render_graph}; use lib::core::dag::Dag; use lib::core::effects::Effects; use lib::core::eventlog::{Event, EventCursor, EventLogDb, EventReplayer}; -use lib::core::formatting::{printable_styled_string, Glyphs}; +use lib::core::formatting::Glyphs; use lib::core::node_descriptors::{ BranchesDescriptor, CommitMessageDescriptor, CommitOidDescriptor, DifferentialRevisionDescriptor, ObsolescenceExplanationDescriptor, Redactor, @@ -156,7 +156,7 @@ fn describe_event_cursor( )?; let graph_lines = graph_lines .into_iter() - .map(|line| printable_styled_string(&glyphs, line)) + .map(|line| glyphs.render(line)) .try_collect()?; Ok([ diff --git a/git-branchless/src/commands/hide.rs b/git-branchless/src/commands/hide.rs index 465ff346b..7a65fd452 100644 --- a/git-branchless/src/commands/hide.rs +++ b/git-branchless/src/commands/hide.rs @@ -14,7 +14,7 @@ use lib::core::dag::{sorted_commit_set, union_all, Dag}; use lib::core::effects::Effects; use lib::core::eventlog::{CommitActivityStatus, Event}; use lib::core::eventlog::{EventLogDb, EventReplayer}; -use lib::core::formatting::{printable_styled_string, Glyphs, Pluralize}; +use lib::core::formatting::{Glyphs, Pluralize}; use lib::core::rewrite::move_branches; use lib::git::{CategorizedReferenceName, GitRunInfo, MaybeZeroOid, NonZeroOid, Repo}; @@ -83,7 +83,7 @@ pub fn hide( writeln!( effects.get_output_stream(), "Hid commit: {}", - printable_styled_string(&glyphs, commit.friendly_describe(&glyphs)?)?, + glyphs.render(commit.friendly_describe(&glyphs)?)?, )?; if let CommitActivityStatus::Obsolete = event_replayer.get_cursor_commit_activity_status(cursor, commit.get_oid()) @@ -235,7 +235,7 @@ pub fn unhide(effects: &Effects, revsets: Vec, recursive: bool) -> eyre: writeln!( effects.get_output_stream(), "Unhid commit: {}", - printable_styled_string(&glyphs, commit.friendly_describe(&glyphs)?)?, + glyphs.render(commit.friendly_describe(&glyphs)?)?, )?; if let CommitActivityStatus::Active = event_replayer.get_cursor_commit_activity_status(cursor, commit.get_oid()) diff --git a/git-branchless/src/commands/hooks.rs b/git-branchless/src/commands/hooks.rs index ecf2f2970..465e1ef8c 100644 --- a/git-branchless/src/commands/hooks.rs +++ b/git-branchless/src/commands/hooks.rs @@ -16,7 +16,7 @@ use itertools::Itertools; use tracing::{error, instrument, warn}; use lib::core::eventlog::{should_ignore_ref_updates, Event, EventLogDb}; -use lib::core::formatting::{printable_styled_string, Glyphs, Pluralize}; +use lib::core::formatting::{Glyphs, Pluralize}; use lib::core::gc::mark_commit_reachable; use lib::git::{CategorizedReferenceName, MaybeZeroOid, ReferenceName, Repo}; @@ -110,7 +110,7 @@ fn hook_post_commit_common(effects: &Effects, hook_name: &str) -> eyre::Result<( writeln!( effects.get_output_stream(), "branchless: processed commit: {}", - printable_styled_string(&glyphs, commit.friendly_describe(&glyphs)?)?, + glyphs.render(commit.friendly_describe(&glyphs)?)?, )?; Ok(()) diff --git a/git-branchless/src/commands/mod.rs b/git-branchless/src/commands/mod.rs index e4ac0d24f..07da1bff6 100644 --- a/git-branchless/src/commands/mod.rs +++ b/git-branchless/src/commands/mod.rs @@ -32,7 +32,6 @@ use cursive::theme::BaseColor; use cursive::utils::markup::StyledString; use eyre::Context; use itertools::Itertools; -use lib::core::formatting::printable_styled_string; use lib::core::rewrite::MergeConflictRemediation; use lib::git::Repo; use lib::git::RepoError; @@ -462,16 +461,13 @@ Here are some options: - To unset the configuration option, run: git config --unset extensions.worktreeConfig - This is safe unless you created another worktree also using a sparse checkout. - Try upgrading to Git v2.36+ and reinitializing your sparse checkout.", - error = printable_styled_string( - effects.get_glyphs(), - StyledString::styled( - "\ + error = effects.get_glyphs().render(StyledString::styled( + "\ Error: the Git configuration setting `extensions.worktreeConfig` is enabled in this repository. Due to upstream libgit2 limitations, git-branchless does not support repositories with this configuration option enabled.", - BaseColor::Red.light() - ) - )?, + BaseColor::Red.light() + ))?, )?; return Ok(Some(ExitCode(1))); } diff --git a/git-branchless/src/commands/navigation.rs b/git-branchless/src/commands/navigation.rs index 4419d9dc4..1bd391e69 100644 --- a/git-branchless/src/commands/navigation.rs +++ b/git-branchless/src/commands/navigation.rs @@ -21,7 +21,7 @@ use lib::core::config::get_next_interactive; use lib::core::dag::{sorted_commit_set, CommitSet, Dag}; use lib::core::effects::Effects; use lib::core::eventlog::{EventLogDb, EventReplayer}; -use lib::core::formatting::{printable_styled_string, Pluralize}; +use lib::core::formatting::Pluralize; use lib::core::node_descriptors::{ BranchesDescriptor, CommitMessageDescriptor, CommitOidDescriptor, DifferentialRevisionDescriptor, NodeDescriptor, Redactor, RelativeTimeDescriptor, @@ -231,16 +231,13 @@ fn advance( writeln!( effects.get_output_stream(), "{}", - printable_styled_string( - glyphs, - StyledString::styled( - format!( - "No more {} commits to go to after traversing {}.", - pluralize.unit.0, pluralize, - ), - BaseColor::Yellow.light() - ) - )? + glyphs.render(StyledString::styled( + format!( + "No more {} commits to go to after traversing {}.", + pluralize.unit.0, pluralize, + ), + BaseColor::Yellow.light() + ))? )?; if i == 0 { @@ -284,7 +281,7 @@ fn advance( effects.get_output_stream(), " {} {}{}", glyphs.bullet_point, - printable_styled_string(glyphs, child.friendly_describe(glyphs)?)?, + glyphs.render(child.friendly_describe(glyphs)?)?, descriptor )?; } diff --git a/git-branchless/src/commands/query.rs b/git-branchless/src/commands/query.rs index 25503f390..1f7f03469 100644 --- a/git-branchless/src/commands/query.rs +++ b/git-branchless/src/commands/query.rs @@ -5,7 +5,6 @@ use itertools::Itertools; use lib::core::dag::{commit_set_to_vec, Dag}; use lib::core::effects::{Effects, OperationType}; use lib::core::eventlog::{EventLogDb, EventReplayer}; -use lib::core::formatting::printable_styled_string; use lib::core::repo_ext::RepoExt; use lib::git::{CategorizedReferenceName, GitRunInfo, Repo}; use lib::util::ExitCode; @@ -83,10 +82,9 @@ pub fn query( writeln!( effects.get_output_stream(), "{}", - printable_styled_string( - effects.get_glyphs(), - commit.friendly_describe(effects.get_glyphs())? - )?, + effects + .get_glyphs() + .render(commit.friendly_describe(effects.get_glyphs())?)?, )?; } } diff --git a/git-branchless/src/commands/reword.rs b/git-branchless/src/commands/reword.rs index 5c9bbb27f..0217e7d50 100644 --- a/git-branchless/src/commands/reword.rs +++ b/git-branchless/src/commands/reword.rs @@ -24,7 +24,7 @@ use lib::core::config::{ use lib::core::dag::{sorted_commit_set, union_all, CommitSet, Dag}; use lib::core::effects::Effects; use lib::core::eventlog::{EventLogDb, EventReplayer}; -use lib::core::formatting::{printable_styled_string, Glyphs, Pluralize}; +use lib::core::formatting::{Glyphs, Pluralize}; use lib::core::node_descriptors::{render_node_descriptors, CommitOidDescriptor, NodeObject}; use lib::core::rewrite::{ execute_rebase_plan, BuildRebasePlanOptions, ExecuteRebasePlanOptions, ExecuteRebasePlanResult, @@ -656,8 +656,7 @@ fn render_status_report( writeln!( effects.get_output_stream(), "Reworded commit {} as {}", - printable_styled_string( - &glyphs, + glyphs.render( // Commit doesn't offer `friendly_describe_oid`, so we'll do it ourselves render_node_descriptors( &glyphs, @@ -667,7 +666,7 @@ fn render_status_report( &mut [&mut CommitOidDescriptor::new(true)?], )? )?, - printable_styled_string(&glyphs, replacement_commit.friendly_describe(&glyphs)?)? + glyphs.render(replacement_commit.friendly_describe(&glyphs)?)? )?; } diff --git a/git-branchless/src/commands/smartlog.rs b/git-branchless/src/commands/smartlog.rs index f2ad8bb58..864f39513 100644 --- a/git-branchless/src/commands/smartlog.rs +++ b/git-branchless/src/commands/smartlog.rs @@ -19,7 +19,7 @@ use tracing::instrument; use lib::core::dag::{CommitSet, Dag}; use lib::core::effects::Effects; use lib::core::eventlog::{EventLogDb, EventReplayer}; -use lib::core::formatting::{printable_styled_string, Pluralize}; +use lib::core::formatting::Pluralize; use lib::core::node_descriptors::{ BranchesDescriptor, CommitMessageDescriptor, CommitOidDescriptor, DifferentialRevisionDescriptor, ObsolescenceExplanationDescriptor, Redactor, @@ -650,7 +650,7 @@ pub fn smartlog( writeln!( effects.get_output_stream(), "{}", - printable_styled_string(effects.get_glyphs(), line)? + effects.get_glyphs().render(line)? )?; } diff --git a/git-branchless/src/commands/snapshot.rs b/git-branchless/src/commands/snapshot.rs index c976f4c4f..c8c7fdd76 100644 --- a/git-branchless/src/commands/snapshot.rs +++ b/git-branchless/src/commands/snapshot.rs @@ -11,7 +11,6 @@ use eyre::Context; use lib::core::check_out::{create_snapshot, restore_snapshot}; use lib::core::effects::Effects; use lib::core::eventlog::EventLogDb; -use lib::core::formatting::printable_styled_string; use lib::git::{GitRunInfo, GitRunResult, NonZeroOid, Repo, WorkingCopySnapshot}; use lib::util::ExitCode; @@ -45,13 +44,10 @@ pub fn create(effects: &Effects, git_run_info: &GitRunInfo) -> eyre::Result