Skip to content

Commit

Permalink
♻️ Refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
uzimaru0000 committed Aug 31, 2021
1 parent 468979c commit 1ac2533
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.3.0"
authors = ["uzimaru0000 <[email protected]>"]
edition = "2018"
description = "Format json into table view"
homepag = "https://github.com/uzimaru0000/tv"
homepage = "https://github.com/uzimaru0000/tv"
keywords = [
"json",
"viewer",
Expand Down
90 changes: 90 additions & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use crate::input;
use anyhow::Result;
use clap::{App, Arg};
use tokio::io::BufReader;
use tv::data::{Align, Data};

pub struct Application<'a, 'b> {
app: App<'a, 'b>,
path: Option<String>,
sort_key: Option<String>,
is_plane: bool,
align: Align,
}

impl<'a, 'b> Application<'a, 'b> {
pub fn new() -> Self {
let app = App::new("tv")
.version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.global_setting(clap::AppSettings::ColoredHelp)
.arg(Arg::with_name("PATH").help("json file path").index(1))
.arg(
Arg::with_name("sort")
.short("s")
.long("sort")
.value_name("SORT_KEY")
.help("Options for sorting by key")
.takes_value(true),
)
.arg(
Arg::with_name("plane")
.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"),
);

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

Self {
app,
path,
sort_key,
is_plane,
align,
}
}

pub async fn run(&mut self) -> Result<()> {
if self.path.is_none() && !input::is_pipe() {
self.app.print_long_help()?;
return Ok(());
}

let raw = match self.path.clone() {
Some(p) => {
let file = tokio::fs::File::open(String::from(p)).await?;
let mut reader = BufReader::new(file);
input::read(&mut reader).await
}
None => input::read_stdin().await,
}?;

let mut data = Data::from(&raw)?;
data.set_sort_key(self.sort_key.clone())
.set_is_plane(self.is_plane)
.set_align(self.align);

println!("{}", data);

Ok(())
}
}
18 changes: 9 additions & 9 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum Json {
Value(serde_json::Value),
}

#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub enum Align {
None,
Left,
Expand All @@ -36,15 +36,15 @@ impl Align {
}

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

impl<'a> Data<'a> {
pub fn from(s: &'a str) -> Result<Self> {
impl Data {
pub fn from(s: &str) -> Result<Self> {
serde_json::from_str::<Vec<Json>>(s)
.map(|x| Self {
data: x,
Expand All @@ -55,7 +55,7 @@ impl<'a> Data<'a> {
.context("unsupported format")
}

pub fn set_sort_key(&mut self, s: Option<&'a str>) -> &mut Self {
pub fn set_sort_key(&mut self, s: Option<String>) -> &mut Self {
self.sort_key = s;
self
}
Expand Down Expand Up @@ -83,11 +83,11 @@ impl<'a> Data<'a> {
fn values(&self) -> Vec<Vec<String>> {
let keys = self.keys();

let data = if let Some(key) = self.sort_key {
let data = if let Some(key) = self.sort_key.clone() {
let mut data = self.data.clone();
data.sort_by_cached_key(|x| match x {
Json::Object(obj) => obj
.get(key)
.get(&key)
.as_deref()
.unwrap_or(&serde_json::Value::default())
.to_string(),
Expand Down Expand Up @@ -139,7 +139,7 @@ impl<'a> Data<'a> {
}
}

impl<'a> Display for Data<'a> {
impl Display for Data {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let keys = self.keys();
let values = self.values();
Expand Down
85 changes: 1 addition & 84 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,85 +1,2 @@
use anyhow::Result;
use clap::{App, Arg};
use tokio::io::BufReader;

use crate::data::Align;

mod data;
mod input;
pub mod data;
mod utils;

pub struct Application<'a, 'b> {
app: App<'a, 'b>,
}

impl<'a, 'b> Application<'a, 'b> {
pub fn new() -> Self {
let app = App::new("tv")
.version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.arg(Arg::with_name("PATH").help("json file path").index(1))
.arg(
Arg::with_name("sort")
.short("s")
.long("sort")
.value_name("SORT_KEY")
.help("Options for sorting by key")
.takes_value(true),
)
.arg(
Arg::with_name("plane")
.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 }
}

pub async fn run(&mut self) -> Result<()> {
let matcher = self.app.clone().get_matches();
let path = matcher.value_of("PATH").map(String::from);

if path.is_none() && !input::is_pipe() {
let mut out = std::io::stdout();
self.app.write_long_help(&mut out)?;
return Ok(());
}

let raw = match path {
Some(p) => {
let file = tokio::fs::File::open(String::from(p)).await?;
let mut reader = BufReader::new(file);
input::read(&mut reader).await
}
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)?;
data.set_sort_key(sort_key)
.set_is_plane(is_plane)
.set_align(align);

println!("{}", data);

Ok(())
}
}
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::application::Application;
use anyhow::Result;
use tv::Application;

mod application;
mod input;

#[tokio::main]
async fn main() -> Result<()> {
Expand Down

0 comments on commit 1ac2533

Please sign in to comment.