Skip to content

Commit

Permalink
Merge pull request #13 from uzimaru0000/feature/align
Browse files Browse the repository at this point in the history
✨ align option
  • Loading branch information
uzimaru0000 authored Aug 28, 2021
2 parents 7907b2d + f3a86a8 commit 8a2bb29
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
56 changes: 54 additions & 2 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,34 @@ use unicode_width::UnicodeWidthStr;

type Json<'a> = HashMap<&'a str, serde_json::Value>;

#[derive(Debug)]
pub enum Align {
None,
Left,
Center,
Right,
}

impl Align {
pub fn new(s: String) -> Self {
match s.to_lowercase().as_str() {
"l" => Self::Left,
"left" => Self::Left,
"c" => Self::Center,
"center" => Self::Center,
"r" => Self::Right,
"right" => Self::Right,
_ => Self::None,
}
}
}

#[derive(Debug)]
pub struct Data<'a> {
data: Vec<Json<'a>>,
sort_key: Option<&'a str>,
is_plane: bool,
align: Align,
}

impl<'a> Data<'a> {
Expand All @@ -23,6 +46,7 @@ impl<'a> Data<'a> {
data: vec,
sort_key: None,
is_plane: false,
align: Align::None,
}),
Err(_) => Self::csv(s),
}
Expand All @@ -38,6 +62,11 @@ impl<'a> Data<'a> {
self
}

pub fn set_align(&mut self, a: Align) -> &mut Self {
self.align = a;
self
}

fn csv(s: &'a str) -> Result<Self> {
let mut lines = s.split("\n");
let sub = lines
Expand All @@ -58,6 +87,7 @@ impl<'a> Data<'a> {
data: maps,
sort_key: None,
is_plane: false,
align: Align::None,
})
}

Expand Down Expand Up @@ -143,7 +173,7 @@ impl<'a> Display for Data<'a> {
let border = pads
.clone()
.iter()
.map(|&x| "-".repeat(x))
.map(|&x| md_table_align(x, &self.align))
.collect::<Vec<_>>()
.join(separator);
write!(f, "{}{}{}\n", frame, border, frame)?;
Expand All @@ -154,10 +184,32 @@ impl<'a> Display for Data<'a> {
.map(|xs| {
xs.iter()
.zip(pads.clone())
.map(|(x, pad)| format!("{}{}", " ".repeat(pad - x.width()), x))
.map(|(x, pad)| cell_view(x, pad, &self.align))
.collect::<Vec<_>>()
.join(separator)
})
.try_for_each(|x| write!(f, "{}{}{}\n", frame, x, frame))
}
}

fn md_table_align(width: usize, align: &Align) -> String {
match align {
Align::None => "-".repeat(width),
Align::Left => format!(":{}", "-".repeat(width - 1)),
Align::Center => format!(":{}:", "-".repeat(width - 2)),
Align::Right => format!("{}:", "-".repeat(width - 1)),
}
}

fn cell_view(v: &String, width: usize, align: &Align) -> String {
match align {
Align::None | Align::Right => format!("{}{}", " ".repeat(width - v.width()), v),
Align::Left => format!("{}{}", v, " ".repeat(width - v.width())),
Align::Center => {
let pad = (width - v.width()) as f32 / 2.0;
let left_pad = pad.ceil() as usize;
let right_pad = pad.floor() as usize;
format!("{}{}{}", " ".repeat(left_pad), v, " ".repeat(right_pad))
}
}
}
22 changes: 20 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use anyhow::Result;
use clap::{App, Arg};
use tokio::io::BufReader;

use crate::data::Align;

mod data;
mod input;
mod utils;
Expand Down Expand Up @@ -30,6 +32,15 @@ impl<'a, 'b> Application<'a, 'b> {
.short("p")
.long("plane")
.help("Do not Display border"),
)
.arg(
Arg::with_name("align")
.short("a")
.long("align")
.value_name("left | center | right | none")
.help("Table alignment")
.takes_value(true)
.default_value("none"),
);

Self { app }
Expand All @@ -54,11 +65,18 @@ impl<'a, 'b> Application<'a, 'b> {
None => input::read_stdin().await,
}?;

let sort_key = matcher.value_of("sort");
let is_plane = matcher.is_present("plane");
let align = matcher
.value_of("align")
.map(String::from)
.map(Align::new)
.unwrap_or(Align::None);

let mut data = data::Data::from(&raw)?;
let sort_key = matcher.value_of("sort");
data.set_sort_key(sort_key).set_is_plane(is_plane);
data.set_sort_key(sort_key)
.set_is_plane(is_plane)
.set_align(align);

println!("{}", data);

Expand Down

0 comments on commit 8a2bb29

Please sign in to comment.