mirror of
https://github.com/LordMZTE/mcstat.git
synced 2024-05-07 23:11:09 +02:00
table block will not exceed terminal width
This commit is contained in:
parent
5353975725
commit
2de875d9c7
|
@ -17,3 +17,5 @@ itertools = "0.9.0"
|
||||||
termcolor = "1"
|
termcolor = "1"
|
||||||
anyhow = "1.0.32"
|
anyhow = "1.0.32"
|
||||||
serde_json = "1.0.58"
|
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 asciify::AsciiBuilder;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
|
|
@ -105,6 +105,10 @@ async fn main() -> Result<()> {
|
||||||
|
|
||||||
let mut table = Table::new();
|
let mut table = Table::new();
|
||||||
|
|
||||||
|
if let Some((w, _)) = term_size::dimensions() {
|
||||||
|
table.max_block_width = w;
|
||||||
|
}
|
||||||
|
|
||||||
table.opt_big_entry(
|
table.opt_big_entry(
|
||||||
"Description",
|
"Description",
|
||||||
none_if_empty!(remove_formatting(&response.description.get_text())),
|
none_if_empty!(remove_formatting(&response.description.get_text())),
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
use std::{
|
use std::{io::{self, Write}, cmp::{min, max}};
|
||||||
cmp::max,
|
|
||||||
io::{self, Write},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(SmartDefault)]
|
||||||
pub struct Table {
|
pub struct Table {
|
||||||
pub entries: Vec<Box<dyn TableEntry>>,
|
pub entries: Vec<Box<dyn TableEntry>>,
|
||||||
pub small_entry_width: usize,
|
pub small_entry_width: usize,
|
||||||
|
#[default(usize::MAX)]
|
||||||
|
pub max_block_width: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Table {
|
impl Table {
|
||||||
|
@ -38,6 +37,7 @@ impl Table {
|
||||||
self.entries.push(Box::new(BigTableEntry::new(
|
self.entries.push(Box::new(BigTableEntry::new(
|
||||||
name.to_string(),
|
name.to_string(),
|
||||||
val.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>) {
|
pub fn opt_big_entry(&mut self, name: impl ToString, val: Option<impl ToString>) {
|
||||||
self.entries.push(Box::new(OptBigTableEntry(
|
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 {
|
impl BigTableEntry {
|
||||||
pub fn new(name: String, val: String) -> Self {
|
pub fn new(name: String, val: String, maxwidth: usize) -> Self {
|
||||||
let val_width = val.splitn(2, '\n').next().unwrap_or(&val).len();
|
let val_width = min(
|
||||||
|
val.lines().map(|s| s.len() + 4).max().unwrap_or_default(),
|
||||||
|
maxwidth,
|
||||||
|
);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
width: max(name.len(), val_width),
|
width: max(name.len(), val_width),
|
||||||
|
|
Loading…
Reference in a new issue