Skip to content

Commit

Permalink
refactor(formatting): printable_styled_string -> Glyphs::render
Browse files Browse the repository at this point in the history
  • Loading branch information
arxanas committed Oct 10, 2022
1 parent 3a275f9 commit b3a29fb
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 151 deletions.
18 changes: 7 additions & 11 deletions git-branchless-lib/src/core/check_out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down
46 changes: 23 additions & 23 deletions git-branchless-lib/src/core/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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::<eyre::Result<String>>()?;
Ok(result)
}
}

impl std::fmt::Debug for Glyphs {
Expand Down Expand Up @@ -379,26 +402,3 @@ fn render_style_as_ansi(content: &str, style: Style) -> eyre::Result<String> {

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<String> {
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::<eyre::Result<String>>()?;
Ok(result)
}
57 changes: 25 additions & 32 deletions git-branchless-lib/src/core/rewrite/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)?
)?
)?;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand All @@ -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): {}",
Expand Down Expand Up @@ -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)?
)?,
)?;
Expand All @@ -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)?
)?,
)?;
Expand Down
14 changes: 5 additions & 9 deletions git-branchless-lib/src/core/rewrite/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)?,)?,
)?;
}
}
Expand All @@ -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())?)?,
)?;
}

Expand Down
16 changes: 7 additions & 9 deletions git-branchless-lib/src/core/rewrite/rewrite_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -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())? {
Expand Down
4 changes: 2 additions & 2 deletions git-branchless/src/commands/bug_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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([
Expand Down
6 changes: 3 additions & 3 deletions git-branchless/src/commands/hide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -235,7 +235,7 @@ pub fn unhide(effects: &Effects, revsets: Vec<Revset>, 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())
Expand Down
4 changes: 2 additions & 2 deletions git-branchless/src/commands/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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(())
Expand Down
12 changes: 4 additions & 8 deletions git-branchless/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)));
}
Expand Down
Loading

0 comments on commit b3a29fb

Please sign in to comment.