Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/cli/adopt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::io;
use clap::Args;

use crate::core::adopt::{self, AdoptOptions, AdoptOutcome};
use crate::core::tree;

use super::CommandOutcome;
use super::common;
Expand All @@ -23,8 +22,7 @@ pub fn execute(args: AdoptArgs) -> io::Result<CommandOutcome> {
let outcome = adopt::apply(&plan)?;

if outcome.status.success() {
let view = tree::focused_context_view(&outcome.branch_name)?;
let rendered_tree = super::tree::render_stack_tree(&view);
let rendered_tree = super::tree::render_focused_context_tree(&outcome.branch_name, None)?;
let output = format_adopt_success_output(&outcome, &rendered_tree);
if !output.is_empty() {
println!("{output}");
Expand Down
7 changes: 4 additions & 3 deletions src/cli/orphan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::io;
use clap::Args;

use crate::core::orphan::{self, OrphanOptions, OrphanOutcome};
use crate::core::tree;

use super::CommandOutcome;
use super::common;
Expand All @@ -19,8 +18,10 @@ pub fn execute(args: OrphanArgs) -> io::Result<CommandOutcome> {
let outcome = orphan::apply(&plan)?;

if outcome.status.success() {
let view = tree::focused_context_view(&outcome.parent_branch_name)?;
let rendered_tree = super::tree::render_stack_tree(&view);
let rendered_tree = super::tree::render_focused_context_tree(
&outcome.parent_branch_name,
Some((&outcome.branch_name, "(orphaned)")),
)?;
let output = format_orphan_success_output(&outcome, &rendered_tree);
if !output.is_empty() {
println!("{output}");
Expand Down
4 changes: 1 addition & 3 deletions src/cli/reparent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::io;
use clap::Args;

use crate::core::reparent::{self, ReparentOptions, ReparentOutcome, ReparentPlan};
use crate::core::tree;

use super::CommandOutcome;
use super::common;
Expand All @@ -23,8 +22,7 @@ pub fn execute(args: ReparentArgs) -> io::Result<CommandOutcome> {
let outcome = reparent::apply(&plan)?;

if outcome.status.success() {
let view = tree::focused_context_view(&outcome.branch_name)?;
let rendered_tree = super::tree::render_stack_tree(&view);
let rendered_tree = super::tree::render_focused_context_tree(&outcome.branch_name, None)?;
let output = format_reparent_success_output(&outcome, &rendered_tree);
if !output.is_empty() {
println!("{output}");
Expand Down
14 changes: 8 additions & 6 deletions src/cli/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ pub fn execute(args: SyncArgs) -> io::Result<CommandOutcome> {
}
}
SyncCompletion::Adopt(adopt_outcome) if adopt_outcome.status.success() => {
let view = tree::focused_context_view(&adopt_outcome.branch_name)?;
let rendered_tree = super::tree::render_stack_tree(&view);
let rendered_tree =
super::tree::render_focused_context_tree(&adopt_outcome.branch_name, None)?;
let output =
super::adopt::format_adopt_success_output(adopt_outcome, &rendered_tree);
if !output.is_empty() {
Expand Down Expand Up @@ -102,17 +102,19 @@ pub fn execute(args: SyncArgs) -> io::Result<CommandOutcome> {
}
}
SyncCompletion::Orphan(orphan_outcome) if orphan_outcome.status.success() => {
let view = tree::focused_context_view(&orphan_outcome.parent_branch_name)?;
let rendered_tree = super::tree::render_stack_tree(&view);
let rendered_tree = super::tree::render_focused_context_tree(
&orphan_outcome.parent_branch_name,
Some((&orphan_outcome.branch_name, "(orphaned)")),
)?;
let output =
super::orphan::format_orphan_success_output(orphan_outcome, &rendered_tree);
if !output.is_empty() {
println!("{output}");
}
}
SyncCompletion::Reparent(reparent_outcome) if reparent_outcome.status.success() => {
let view = tree::focused_context_view(&reparent_outcome.branch_name)?;
let rendered_tree = super::tree::render_stack_tree(&view);
let rendered_tree =
super::tree::render_focused_context_tree(&reparent_outcome.branch_name, None)?;
let output = super::reparent::format_reparent_success_output(
reparent_outcome,
&rendered_tree,
Expand Down
3 changes: 3 additions & 0 deletions src/cli/sync/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ mod tests {
},
],
}],
current_branch_name: Some("main".into()),
is_current_visible: true,
current_branch_suffix: None,
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/cli/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ impl From<TreeArgs> for TreeOptions {
}
}

pub(super) fn render_focused_context_tree(
branch_name: &str,
suffix_for_current_branch: Option<(&str, &str)>,
) -> io::Result<String> {
let mut view = tree::focused_context_view(branch_name)?;

if let Some((current_branch_name, suffix)) = suffix_for_current_branch {
if view.current_branch_name.as_deref() == Some(current_branch_name) {
view.current_branch_suffix = Some(suffix.to_string());
}
}

Ok(render::render_stack_tree(&view))
}

#[cfg(test)]
mod tests {
use super::TreeArgs;
Expand Down
119 changes: 99 additions & 20 deletions src/cli/tree/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,30 @@ pub fn render_branch_lineage(lineage: &[BranchLineageNode]) -> String {
}

pub fn render_stack_tree(view: &TreeView) -> String {
common::render_tree(
let mut rendered = common::render_tree(
view.root_label.as_ref().map(format_tree_label),
&view.roots,
&|node| format_branch_label(&node.branch_name, node.is_current, node.pull_request_number),
&|node| node.children.as_slice(),
)
);

if !view.is_current_visible {
if let Some(current_branch) = &view.current_branch_name {
let label = match &view.current_branch_suffix {
Some(suffix) => format!("{current_branch} {suffix}"),
None => current_branch.clone(),
};

rendered.push_str("\n\n");
rendered.push_str(&format!(
"{} {}",
Accent::BranchRef.paint_ansi(markers::CURRENT_BRANCH),
Accent::BranchRef.paint_ansi(&label)
));
}
}

rendered
}

fn format_tree_label(root_label: &TreeLabel) -> String {
Expand Down Expand Up @@ -56,7 +74,7 @@ fn format_branch_label(
Accent::BranchRef.paint_ansi(&label)
)
} else {
label
format!("{} {}", markers::NON_CURRENT_BRANCH, label)
}
}

Expand All @@ -70,7 +88,7 @@ fn format_lineage_branch(branch: &BranchLineageNode, is_current: bool) -> String
Accent::BranchRef.paint_ansi(&label)
)
} else {
format!("{} {}", markers::NON_CURRENT_BRANCH, label)
format!("{} {}", markers::NON_CURRENT_BRANCH, label)
}
}

Expand Down Expand Up @@ -102,9 +120,9 @@ mod tests {
concat!(
"\u{1b}[32m✓\u{1b}[0m \u{1b}[32mfeature/api-followup\u{1b}[0m\n",
"│ \n",
"* feature/api\n",
"* feature/api\n",
"│ \n",
"* main"
"* main"
)
);
}
Expand Down Expand Up @@ -141,9 +159,9 @@ mod tests {
concat!(
"\u{1b}[32m✓\u{1b}[0m \u{1b}[32mfeature/api-followup (#43)\u{1b}[0m\n",
"│ \n",
"* feature/api (#42)\n",
"* feature/api (#42)\n",
"│ \n",
"* main"
"* main"
)
);
}
Expand Down Expand Up @@ -199,19 +217,22 @@ mod tests {
children: vec![],
},
],
current_branch_name: Some("feat/auth-ui".into()),
is_current_visible: true,
current_branch_suffix: None,
});

assert_eq!(
rendered,
concat!(
"main\n",
"├── feat/auth\n",
"│ ├── feat/auth-api\n",
"│ │ └── feat/auth-api-tests\n",
"* main\n",
"├── * feat/auth\n",
"│ ├── * feat/auth-api\n",
"│ │ └── * feat/auth-api-tests\n",
"│ └── \u{1b}[32m✓\u{1b}[0m \u{1b}[32mfeat/auth-ui\u{1b}[0m\n",
"├── feat/billing\n",
"│ └── feat/billing-retry\n",
"└── docs/readme"
"├── * feat/billing\n",
"│ └── * feat/billing-retry\n",
"└── * docs/readme"
)
);
}
Expand Down Expand Up @@ -243,14 +264,17 @@ mod tests {
children: vec![],
},
],
current_branch_name: Some("feat/auth-ui".into()),
is_current_visible: true,
current_branch_suffix: None,
});

assert_eq!(
rendered,
concat!(
"feat/auth\n",
"├── feat/auth-api\n",
"│ └── feat/auth-api-tests\n",
"* feat/auth\n",
"├── * feat/auth-api\n",
"│ └── * feat/auth-api-tests\n",
"└── \u{1b}[32m✓\u{1b}[0m \u{1b}[32mfeat/auth-ui\u{1b}[0m"
)
);
Expand Down Expand Up @@ -278,15 +302,70 @@ mod tests {
children: vec![],
},
],
current_branch_name: Some("feat/auth-ui".into()),
is_current_visible: true,
current_branch_suffix: None,
});

assert_eq!(
rendered,
concat!(
"feat/auth (#42)\n",
"├── feat/auth-api (#43)\n",
"* feat/auth (#42)\n",
"├── * feat/auth-api (#43)\n",
"└── \u{1b}[32m✓\u{1b}[0m \u{1b}[32mfeat/auth-ui (#44)\u{1b}[0m"
)
);
}

#[test]
fn renders_hidden_tracked_current_branch_at_bottom() {
let rendered = render_stack_tree(&TreeView {
root_label: Some(TreeLabel {
branch_name: "feat/billing".into(),
is_current: false,
pull_request_number: None,
}),
roots: vec![],
current_branch_name: Some("feat/auth-ui".into()),
is_current_visible: false,
current_branch_suffix: None,
});

assert_eq!(
rendered,
concat!(
"* feat/billing\n\n",
"\u{1b}[32m✓\u{1b}[0m \u{1b}[32mfeat/auth-ui\u{1b}[0m"
)
);
}

#[test]
fn renders_hidden_orphaned_current_branch_at_bottom() {
let rendered = render_stack_tree(&TreeView {
root_label: Some(TreeLabel {
branch_name: "main".into(),
is_current: false,
pull_request_number: None,
}),
roots: vec![TreeNode {
branch_name: "feat/tracked".into(),
is_current: false,
pull_request_number: None,
children: vec![],
}],
current_branch_name: Some("feat/untracked".into()),
is_current_visible: false,
current_branch_suffix: Some("(orphaned)".into()),
});

assert_eq!(
rendered,
concat!(
"* main\n",
"└── * feat/tracked\n\n",
"\u{1b}[32m✓\u{1b}[0m \u{1b}[32mfeat/untracked (orphaned)\u{1b}[0m"
)
);
}
}
Loading