Skip to content

Commit

Permalink
table: Refactor header() to consume self
Browse files Browse the repository at this point in the history
This makes it a bit clearer API-wise that tables can only have one header.
  • Loading branch information
vincentdephily committed Oct 29, 2024
1 parent baeb512 commit cda2217
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
50 changes: 25 additions & 25 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ pub fn cmd_log(gc: Conf, sc: ConfLog) -> Result<bool, Error> {
let mut unmerges: HashMap<String, i64> = HashMap::new();
let mut found = 0;
let mut sync_start: Option<i64> = None;
let mut tbl = Table::new(&gc).align_left(0).align_left(2).margin(2, " ").last(sc.last);
tbl.header(["Date", "Duration", "Package/Repo"]);
let h = ["Date", "Duration", "Package/Repo"];
let mut tbl =
Table::new(&gc).align_left(0).align_left(2).margin(2, " ").last(sc.last).header(h);
for p in hist {
match p {
Hist::MergeStart { ts, key, .. } => {
Expand Down Expand Up @@ -136,25 +137,25 @@ impl Times {
/// Then we compute the stats per ebuild, and print that.
pub fn cmd_stats(gc: Conf, sc: ConfStats) -> Result<bool, Error> {
let hist = get_hist(&gc.logfile, gc.from, gc.to, sc.show, &sc.search, sc.exact)?;
let mut tbls = Table::new(&gc).align_left(0).align_left(1).margin(1, " ");
tbls.header([sc.group.name(), "Repo", "Syncs", "Total time", "Predict time"]);
let mut tblp = Table::new(&gc).align_left(0).align_left(1).margin(1, " ");
tblp.header([sc.group.name(),
"Package",
"Merges",
"Total time",
"Predict time",
"Unmerges",
"Total time",
"Predict time"]);
let mut tblt = Table::new(&gc).align_left(0).margin(1, " ");
tblt.header([sc.group.name(),
"Merges",
"Total time",
"Average time",
"Unmerges",
"Total time",
"Average time"]);
let h = [sc.group.name(), "Repo", "Syncs", "Total time", "Predict time"];
let mut tbls = Table::new(&gc).align_left(0).align_left(1).margin(1, " ").header(h);
let h = [sc.group.name(),
"Package",
"Merges",
"Total time",
"Predict time",
"Unmerges",
"Total time",
"Predict time"];
let mut tblp = Table::new(&gc).align_left(0).align_left(1).margin(1, " ").header(h);
let h = [sc.group.name(),
"Merges",
"Total time",
"Average time",
"Unmerges",
"Total time",
"Average time"];
let mut tblt = Table::new(&gc).align_left(0).margin(1, " ").header(h);
let mut merge_start: HashMap<String, i64> = HashMap::new();
let mut unmerge_start: HashMap<String, i64> = HashMap::new();
let mut pkg_time: BTreeMap<String, (Times, Times)> = BTreeMap::new();
Expand Down Expand Up @@ -473,8 +474,8 @@ pub fn cmd_accuracy(gc: Conf, sc: ConfAccuracy) -> Result<bool, Error> {
let mut pkg_times: BTreeMap<String, Times> = BTreeMap::new();
let mut pkg_errs: BTreeMap<String, Vec<f64>> = BTreeMap::new();
let mut found = false;
let mut tbl = Table::new(&gc).align_left(0).align_left(1).last(sc.last);
tbl.header(["Date", "Package", "Real", "Predicted", "Error"]);
let h = ["Date", "Package", "Real", "Predicted", "Error"];
let mut tbl = Table::new(&gc).align_left(0).align_left(1).last(sc.last).header(h);
for p in hist {
match p {
Hist::MergeStart { ts, key, .. } => {
Expand Down Expand Up @@ -517,8 +518,7 @@ pub fn cmd_accuracy(gc: Conf, sc: ConfAccuracy) -> Result<bool, Error> {
}
drop(tbl);
if sc.show.tot {
let mut tbl = Table::new(&gc).align_left(0);
tbl.header(["Package", "Error"]);
let mut tbl = Table::new(&gc).align_left(0).header(["Package", "Error"]);
for (p, e) in pkg_errs {
let avg = e.iter().sum::<f64>() / e.len() as f64;
tbl.row([&[&gc.pkg, &p], &[&gc.cnt, &format!("{avg:.1}%")]]);
Expand Down
9 changes: 4 additions & 5 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<'a, const N: usize> Table<'a, N> {
}

/// Add a section header
pub fn header(&mut self, row: [&str; N]) {
pub fn header(mut self, row: [&str; N]) -> Self {
if self.conf.header {
let mut idxrow = [(0, 0, 0); N];
for i in 0..N {
Expand All @@ -84,6 +84,7 @@ impl<'a, const N: usize> Table<'a, N> {
}
self.header = Some(idxrow);
}
self
}

/// Is there actual data to flush ?
Expand Down Expand Up @@ -205,8 +206,7 @@ mod test {
assert_eq!(t.to_string(), "5\n6\n7\n8\n9\n");

// 5 max ignoring header
let mut t = Table::new(&conf).last(5);
t.header(["h"]);
let mut t = Table::new(&conf).last(5).header(["h"]);
for i in 1..10 {
t.row([&[&format!("{i}")]]);
}
Expand All @@ -229,8 +229,7 @@ mod test {
#[test]
fn align_longheader() {
let conf = Conf::from_str("emlop log --color=n --output=c -H");
let mut t = Table::<2>::new(&conf).align_left(0);
t.header(["heeeeeeeader", "d"]);
let mut t = Table::<2>::new(&conf).align_left(0).header(["heeeeeeeader", "d"]);
t.row([&[&"short"], &[&1]]);
t.row([&[&"high"], &[&9999]]);
let res = "heeeeeeeader d\n\
Expand Down

0 comments on commit cda2217

Please sign in to comment.