summaryrefslogtreecommitdiffstats
path: root/src/column.rs
diff options
context:
space:
mode:
authorBen S <ogham@bsago.me>2015-06-28 13:21:21 +0100
committerBen S <ogham@bsago.me>2015-06-28 13:21:21 +0100
commitccdf9ff4a679e00298f7e0fd9bd68318ded20cb8 (patch)
tree48c7fe31268fa44046e731af13a4d6a120309ace /src/column.rs
parent766279b803934522af6570c8e465b0d3959ac903 (diff)
Add --grid --long option
This commit adds --grid, which, when used with --long, will split the details into multiple columns. Currently this is just 2 columns, but in the future it will be based on the width of the terminal. In order to do this, I had to do two things: 1. Add a `links` parameter to the filename function, which disables the printing of the arrow and link target in the details view. When this is active, the columns get way too large, and it becomes not worth it. 2. Change the `print_table` function from actually printing the table to stdout to returning a list of `Cells` based on the table. This list then gets its width measured to calculate the width of the resulting table.
Diffstat (limited to 'src/column.rs')
-rw-r--r--src/column.rs40
1 files changed, 19 insertions, 21 deletions
diff --git a/src/column.rs b/src/column.rs
index 23e2a4b..6f4a1e7 100644
--- a/src/column.rs
+++ b/src/column.rs
@@ -1,5 +1,3 @@
-use std::iter::repeat;
-
use ansi_term::Style;
use unicode_width::UnicodeWidthStr;
@@ -58,25 +56,6 @@ impl Column {
}
}
-/// Pad a string with the given number of spaces.
-fn spaces(length: usize) -> String {
- repeat(" ").take(length).collect()
-}
-
-impl Alignment {
- /// Pad a string with the given alignment and number of spaces.
- ///
- /// This doesn't take the width the string *should* be, rather the number
- /// of spaces to add: this is because the strings are usually full of
- /// invisible control characters, so getting the displayed width of the
- /// string is not as simple as just getting its length.
- pub fn pad_string(&self, string: &str, padding: usize) -> String {
- match *self {
- Alignment::Left => format!("{}{}", string, spaces(padding)),
- Alignment::Right => format!("{}{}", spaces(padding), string),
- }
- }
-}
#[derive(PartialEq, Debug)]
pub struct Cell {
@@ -85,10 +64,29 @@ pub struct Cell {
}
impl Cell {
+ pub fn empty() -> Cell {
+ Cell {
+ text: String::new(),
+ length: 0,
+ }
+ }
+
pub fn paint(style: Style, string: &str) -> Cell {
Cell {
text: style.paint(string).to_string(),
length: UnicodeWidthStr::width(string),
}
}
+
+ pub fn add_spaces(&mut self, count: usize) {
+ self.length += count;
+ for _ in 0 .. count {
+ self.text.push(' ');
+ }
+ }
+
+ pub fn append(&mut self, other: &Cell) {
+ self.length += other.length;
+ self.text.push_str(&*other.text);
+ }
}