table block will not exceed terminal width
This commit is contained in:
parent
5353975725
commit
2de875d9c7
4 changed files with 20 additions and 8 deletions
|
@ -17,3 +17,5 @@ itertools = "0.9.0"
|
|||
termcolor = "1"
|
||||
anyhow = "1.0.32"
|
||||
serde_json = "1.0.58"
|
||||
term_size = "0.3.2"
|
||||
smart-default = "0.6.0"
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
#[macro_use]
|
||||
extern crate smart_default;
|
||||
|
||||
use asciify::AsciiBuilder;
|
||||
use itertools::Itertools;
|
||||
|
||||
|
|
|
@ -105,6 +105,10 @@ async fn main() -> Result<()> {
|
|||
|
||||
let mut table = Table::new();
|
||||
|
||||
if let Some((w, _)) = term_size::dimensions() {
|
||||
table.max_block_width = w;
|
||||
}
|
||||
|
||||
table.opt_big_entry(
|
||||
"Description",
|
||||
none_if_empty!(remove_formatting(&response.description.get_text())),
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
use std::{
|
||||
cmp::max,
|
||||
io::{self, Write},
|
||||
};
|
||||
use std::{io::{self, Write}, cmp::{min, max}};
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(SmartDefault)]
|
||||
pub struct Table {
|
||||
pub entries: Vec<Box<dyn TableEntry>>,
|
||||
pub small_entry_width: usize,
|
||||
#[default(usize::MAX)]
|
||||
pub max_block_width: usize,
|
||||
}
|
||||
|
||||
impl Table {
|
||||
|
@ -38,6 +37,7 @@ impl Table {
|
|||
self.entries.push(Box::new(BigTableEntry::new(
|
||||
name.to_string(),
|
||||
val.to_string(),
|
||||
self.max_block_width,
|
||||
)));
|
||||
}
|
||||
|
||||
|
@ -52,7 +52,7 @@ impl Table {
|
|||
|
||||
pub fn opt_big_entry(&mut self, name: impl ToString, val: Option<impl ToString>) {
|
||||
self.entries.push(Box::new(OptBigTableEntry(
|
||||
val.map(|t| BigTableEntry::new(name.to_string(), t.to_string())),
|
||||
val.map(|t| BigTableEntry::new(name.to_string(), t.to_string(), self.max_block_width)),
|
||||
)));
|
||||
}
|
||||
|
||||
|
@ -101,8 +101,11 @@ impl TableEntry for BigTableEntry {
|
|||
}
|
||||
|
||||
impl BigTableEntry {
|
||||
pub fn new(name: String, val: String) -> Self {
|
||||
let val_width = val.splitn(2, '\n').next().unwrap_or(&val).len();
|
||||
pub fn new(name: String, val: String, maxwidth: usize) -> Self {
|
||||
let val_width = min(
|
||||
val.lines().map(|s| s.len() + 4).max().unwrap_or_default(),
|
||||
maxwidth,
|
||||
);
|
||||
|
||||
Self {
|
||||
width: max(name.len(), val_width),
|
||||
|
|
Loading…
Reference in a new issue