Skip to content

Commit

Permalink
feat(term): better table split (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
fioncat authored Oct 16, 2024
1 parent 517bcdb commit c0e33d3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
31 changes: 27 additions & 4 deletions src/cmd/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use console::style;

use crate::cmd::{Completion, Run};
use crate::config::Config;
use crate::term;
use crate::term::{self, TableCell, TableCellColor};
use crate::term::{BranchStatus, Cmd, GitBranch, Table};

/// Git branch operations
Expand Down Expand Up @@ -122,9 +122,32 @@ impl BranchArgs {
} else {
String::new()
};
let status = format!("{}", branch.status.display());
let row = vec![cur, branch.name.clone(), status];
table.add(row);
let status = match branch.status {
BranchStatus::Sync => {
TableCell::with_color(String::from("sync"), TableCellColor::Green)
}
BranchStatus::Gone => {
TableCell::with_color(String::from("gone"), TableCellColor::Red)
}
BranchStatus::Ahead => {
TableCell::with_color(String::from("ahead"), TableCellColor::Yellow)
}
BranchStatus::Behind => {
TableCell::with_color(String::from("behind"), TableCellColor::Yellow)
}
BranchStatus::Conflict => {
TableCell::with_color(String::from("conflict"), TableCellColor::Yellow)
}
BranchStatus::Detached => {
TableCell::with_color(String::from("detached"), TableCellColor::Red)
}
};
let row = vec![
TableCell::no_color(cur),
TableCell::no_color(branch.name.clone()),
status,
];
table.add_color(row);
}

if self.all {
Expand Down
19 changes: 10 additions & 9 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1445,26 +1445,27 @@ impl Table {
let mut max_size: usize = 0;
for row in self.rows.iter() {
let cell = row.get(coli).unwrap();
let raw_size = console::measure_text_width(&cell.text);
let size = if coli == self.ncol - 1 {
raw_size
} else {
raw_size + 2
};
let size = console::measure_text_width(&cell.text);
if size > max_size {
max_size = size
}
}
pads.push(max_size);
}

let total_pad: usize = pads.iter().sum();
let split = "-".repeat(total_pad);
let mut split = String::from("+");
for pad in pads.iter() {
for _ in 0..*pad + 2 {
split.push('-');
}
split.push('+');
}

for (rowi, row) in self.rows.into_iter().enumerate() {
if rowi == 0 || (self.foot_index > 0 && rowi >= self.foot_index) {
eprintln!("{split}");
}
eprint!("|");
for (coli, cell) in row.into_iter().enumerate() {
let pad = pads[coli];
let mut text = cell
Expand All @@ -1478,7 +1479,7 @@ impl Table {
};
text = format!("{style_text}");
}
eprint!("{text}");
eprint!(" {text} |");
}
eprintln!();

Expand Down

0 comments on commit c0e33d3

Please sign in to comment.