Skip to content

feat: dynamic branch popup size #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
30 changes: 19 additions & 11 deletions src/components/select_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ impl DrawableComponent for SelectBranchComponent {
// Render a scrolllist of branches inside a box

if self.visible {
const SIZE: (u16, u16) = (50, 20);
let scroll_threshold = SIZE.1 / 3;
const PERCENT_SIZE: (u16, u16) = (60, 25);
const MIN_SIZE: (u16, u16) = (50, 20);

let area = ui::centered_rect(
PERCENT_SIZE.0,
PERCENT_SIZE.1,
f.size(),
);
let area = ui::rect_min(MIN_SIZE.0, MIN_SIZE.1, area);

let scroll_threshold = area.height / 3;
let scroll =
self.selection.saturating_sub(scroll_threshold);

let area =
ui::centered_rect_absolute(SIZE.0, SIZE.1, f.size());

f.render_widget(Clear, area);
f.render_widget(
Block::default()
Expand Down Expand Up @@ -275,15 +281,17 @@ impl SelectBranchComponent {
theme: &SharedTheme,
width_available: u16,
) -> Result<Text> {
const BRANCH_NAME_LENGTH: usize = 15;
const COMMIT_HASH_LENGTH: usize = 8;
const IS_HEAD_STAR_LENGTH: usize = 3; // "* "
const THREE_DOTS_LENGTH: usize = 3; // "..."

// branch name = 30% of area size
let branch_name_length: usize =
width_available as usize * 30 / 100;
// commit message takes up the remaining width
let commit_message_length: usize = (width_available as usize)
.saturating_sub(COMMIT_HASH_LENGTH)
.saturating_sub(BRANCH_NAME_LENGTH)
.saturating_sub(branch_name_length)
.saturating_sub(IS_HEAD_STAR_LENGTH)
.saturating_sub(THREE_DOTS_LENGTH);
let mut txt = Vec::new();
Expand All @@ -301,9 +309,9 @@ impl SelectBranchComponent {
}

let mut branch_name = displaybranch.name.clone();
if branch_name.len() > BRANCH_NAME_LENGTH {
if branch_name.len() > branch_name_length {
branch_name.truncate(
BRANCH_NAME_LENGTH
branch_name_length
.saturating_sub(THREE_DOTS_LENGTH),
);
branch_name += "...";
Expand All @@ -322,7 +330,7 @@ impl SelectBranchComponent {
format!(
">{:w$} ",
branch_name,
w = BRANCH_NAME_LENGTH
w = branch_name_length
),
theme.commit_author(true),
),
Expand All @@ -348,7 +356,7 @@ impl SelectBranchComponent {
format!(
" {:w$} ",
branch_name,
w = BRANCH_NAME_LENGTH
w = branch_name_length
),
theme.commit_author(false),
),
Expand Down