improve tables and remove fzf nonsense
This commit is contained in:
parent
6e3086c460
commit
e5a51be87e
8 changed files with 139 additions and 117 deletions
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "cli"
|
name = "cli"
|
||||||
version = "1.0.0"
|
version = "1.1.0"
|
||||||
authors = ["LordMZTE <lord@mzte.de>"]
|
authors = ["LordMZTE <lord@mzte.de>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "GPL-3.0"
|
license = "GPL-3.0"
|
||||||
|
@ -20,17 +20,19 @@ path = "src/main.rs"
|
||||||
anyhow = "1.0.52"
|
anyhow = "1.0.52"
|
||||||
chrono = "0.4.19"
|
chrono = "0.4.19"
|
||||||
clap = "2.34.0"
|
clap = "2.34.0"
|
||||||
|
comfy-table = "5.0.0"
|
||||||
env_logger = "0.9.0"
|
env_logger = "0.9.0"
|
||||||
fuzzy-matcher = "0.3.7"
|
fuzzy-matcher = "0.3.7"
|
||||||
indicatif = "0.16.2"
|
indicatif = "0.16.2"
|
||||||
libjens = { git = "https://tilera.xyz/git/LordMZTE/libjens.git", rev = "1.0.0" }
|
libjens = { git = "https://tilera.xyz/git/LordMZTE/libjens.git", rev = "1.0.1" }
|
||||||
log = "0.4.14"
|
log = "0.4.14"
|
||||||
opener = "0.5.0"
|
opener = "0.5.0"
|
||||||
reqwest = { version = "0.11.8", features = ["stream", "multipart"] }
|
reqwest = { version = "0.11.8", features = ["stream", "multipart"] }
|
||||||
serde_json = "1.0.73"
|
serde_json = "1.0.73"
|
||||||
structopt = "0.3.25"
|
structopt = "0.3.25"
|
||||||
term-table = "1.3.2"
|
|
||||||
term_size = "0.3.2"
|
term_size = "0.3.2"
|
||||||
tokio = { version = "1.15.0", features = ["macros", "fs", "process", "rt-multi-thread"] }
|
tokio = { version = "1.15.0", features = ["macros", "fs", "process", "rt-multi-thread"] }
|
||||||
tokio-util = { version = "0.6.9", features = ["codec"] }
|
tokio-util = { version = "0.6.9", features = ["codec"] }
|
||||||
url = "2.2.2"
|
url = "2.2.2"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
|
|
@ -1,18 +1,17 @@
|
||||||
use crate::table::{self, AsTableRow};
|
use crate::table::{list_table, JMTableEntry, TableExt};
|
||||||
use libjens::JMClient;
|
use libjens::{api::Category, JMClient};
|
||||||
|
|
||||||
pub async fn run(client: &JMClient) -> anyhow::Result<()> {
|
pub async fn run(client: &JMClient) -> anyhow::Result<()> {
|
||||||
// clone required, because for sorting the immutable reference will not work
|
// clone required, because for sorting the immutable reference will not work
|
||||||
let mut cats = client.get_cats().await?.as_ref().clone();
|
let mut cats = client.get_cats().await?.as_ref().clone();
|
||||||
cats.sort_by(|a, b| a.id.cmp(&b.id));
|
cats.sort_by(|a, b| a.id.cmp(&b.id));
|
||||||
|
|
||||||
let mut table = table::list_table();
|
println!(
|
||||||
|
"{}",
|
||||||
for cat in &cats {
|
list_table()
|
||||||
table.add_row(cat.as_table_row());
|
.type_header::<Category>()
|
||||||
}
|
.add_rows(cats.into_iter().map(JMTableEntry))
|
||||||
|
);
|
||||||
println!("{}", table.render());
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,15 @@
|
||||||
use anyhow::{Context, Result};
|
|
||||||
use std::{
|
|
||||||
io::Write,
|
|
||||||
process::{Command, Stdio},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
table::{self, AsTableRow},
|
table::{list_table, JMTableEntry, TableExt},
|
||||||
util,
|
util,
|
||||||
};
|
};
|
||||||
use libjens::{util::MemeSorting, JMClient};
|
use anyhow::Result;
|
||||||
|
use libjens::{api::Meme, util::MemeSorting, JMClient};
|
||||||
|
|
||||||
pub async fn run(
|
pub async fn run(
|
||||||
client: &JMClient,
|
client: &JMClient,
|
||||||
cat: Option<String>,
|
cat: Option<String>,
|
||||||
user: Option<String>,
|
user: Option<String>,
|
||||||
sorting: Option<MemeSorting>,
|
sorting: Option<MemeSorting>,
|
||||||
fzf: bool,
|
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// This needs to be done so both users, memes and categories will be requested
|
// This needs to be done so both users, memes and categories will be requested
|
||||||
// at once
|
// at once
|
||||||
|
@ -47,43 +41,12 @@ pub async fn run(
|
||||||
s.sort_with(&mut memes);
|
s.sort_with(&mut memes);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut table = table::list_table();
|
println!(
|
||||||
|
"{}",
|
||||||
for m in memes.iter() {
|
list_table()
|
||||||
table.add_row(m.as_table_row());
|
.type_header::<Meme>()
|
||||||
}
|
.add_rows(memes.into_iter().cloned().map(JMTableEntry))
|
||||||
|
);
|
||||||
let table_str = table.render();
|
|
||||||
|
|
||||||
if fzf {
|
|
||||||
let mut child = Command::new("fzf")
|
|
||||||
.args(&["--delimiter", "\\t", "--with-nth", "2"])
|
|
||||||
.stdout(Stdio::piped())
|
|
||||||
.stdin(Stdio::piped())
|
|
||||||
.spawn()
|
|
||||||
.context("Failed to spawn FZF")?;
|
|
||||||
let stdin = child.stdin.as_mut().context("could not get FZF stdin")?;
|
|
||||||
|
|
||||||
for (idx, line) in table_str.lines().enumerate() {
|
|
||||||
stdin
|
|
||||||
.write(format!("{}\t{}\n", idx, line).as_bytes())
|
|
||||||
.context("Failed to write to FZF")?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let out = child.wait_with_output()?;
|
|
||||||
let out_str = String::from_utf8(out.stdout).context("FZF output is invalid UTF-8")?;
|
|
||||||
let idx = out_str
|
|
||||||
.split('\t')
|
|
||||||
.next()
|
|
||||||
.and_then(|s| s.parse::<usize>().ok())
|
|
||||||
.context("Failed to parse FZF output")?;
|
|
||||||
let meme = memes
|
|
||||||
.get(idx)
|
|
||||||
.context("Falied to retrieve meme FZF returned")?;
|
|
||||||
println!("{}", meme.link);
|
|
||||||
} else {
|
|
||||||
println!("{}", table_str);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,10 @@ use log::info;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
table::{self, AsTableRow},
|
table::{list_table, JMTableEntry, TableExt},
|
||||||
util,
|
util,
|
||||||
};
|
};
|
||||||
use libjens::JMClient;
|
use libjens::{api::Meme, JMClient};
|
||||||
|
|
||||||
pub async fn run(
|
pub async fn run(
|
||||||
client: &JMClient,
|
client: &JMClient,
|
||||||
|
@ -53,14 +53,11 @@ pub async fn run(
|
||||||
|
|
||||||
matches.sort_by(|a, b| b.1.cmp(&a.1));
|
matches.sort_by(|a, b| b.1.cmp(&a.1));
|
||||||
let res = match (firsturl, cat) {
|
let res = match (firsturl, cat) {
|
||||||
(false, false) => {
|
(false, false) => list_table()
|
||||||
let mut table = table::list_table();
|
.type_header::<Meme>()
|
||||||
|
.add_rows(matches.into_iter().map(|(m, _)| JMTableEntry(m.clone())))
|
||||||
for m in matches {
|
.to_string()
|
||||||
table.add_row(m.0.as_table_row());
|
.into_bytes(),
|
||||||
}
|
|
||||||
table.render().into_bytes()
|
|
||||||
},
|
|
||||||
|
|
||||||
(true, _) => matches
|
(true, _) => matches
|
||||||
.first()
|
.first()
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
use crate::table::{self, AsTableRow};
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use libjens::JMClient;
|
use libjens::{api::User, JMClient};
|
||||||
|
|
||||||
|
use crate::table::{list_table, JMTableEntry, TableExt};
|
||||||
|
|
||||||
pub async fn run(client: &JMClient) -> Result<()> {
|
pub async fn run(client: &JMClient) -> Result<()> {
|
||||||
let users = client.get_users().await?;
|
let users = client.get_users().await?;
|
||||||
let mut table = table::list_table();
|
|
||||||
|
|
||||||
for u in &*users {
|
println!(
|
||||||
table.add_row(u.as_table_row())
|
"{}",
|
||||||
}
|
list_table()
|
||||||
|
.type_header::<User>()
|
||||||
println!("{}", table.render());
|
.add_rows(users.iter().cloned().map(JMTableEntry))
|
||||||
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,9 +77,6 @@ enum Cmd {
|
||||||
help = "how to sort the results. can be id, user, category, timestamp or link"
|
help = "how to sort the results. can be id, user, category, timestamp or link"
|
||||||
)]
|
)]
|
||||||
sort: Option<MemeSorting>,
|
sort: Option<MemeSorting>,
|
||||||
|
|
||||||
#[structopt(long, short, help = "search memes with FZF")]
|
|
||||||
fzf: bool,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
#[structopt(about = "Lists all users")]
|
#[structopt(about = "Lists all users")]
|
||||||
|
@ -116,8 +113,7 @@ async fn main() -> Result<()> {
|
||||||
category,
|
category,
|
||||||
user,
|
user,
|
||||||
sort,
|
sort,
|
||||||
fzf,
|
} => commands::list::run(&client, category, user, sort).await?,
|
||||||
} => commands::list::run(&client, category, user, sort, fzf).await?,
|
|
||||||
Cmd::Users => commands::users::run(&client).await?,
|
Cmd::Users => commands::users::run(&client).await?,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
124
cli/src/table.rs
124
cli/src/table.rs
|
@ -1,45 +1,109 @@
|
||||||
use chrono::{Local, TimeZone};
|
use chrono::{Local, TimeZone};
|
||||||
|
use comfy_table::{presets::UTF8_NO_BORDERS, ContentArrangement, Row, Table};
|
||||||
use libjens::api::{Category, Meme, User};
|
use libjens::api::{Category, Meme, User};
|
||||||
use term_table::{row::Row, table_cell::TableCell, Table, TableBuilder, TableStyle};
|
|
||||||
|
|
||||||
/// returns an empty table with the correct format settings for lists
|
// pub trait AsTableRow {
|
||||||
pub fn list_table<'a>() -> Table<'a> {
|
// fn as_table_row(&self) -> term_table::row::Row;
|
||||||
TableBuilder::new()
|
//}
|
||||||
.style(TableStyle::simple())
|
// impl AsTableRow for Category {
|
||||||
.separate_rows(false)
|
// fn as_table_row(&self) -> Row<'_> {
|
||||||
.has_top_boarder(false)
|
// Row::new(vec![TableCell::new(&self.id), TableCell::new(&self.name)])
|
||||||
.has_bottom_boarder(false)
|
// }
|
||||||
.build()
|
//}
|
||||||
|
// impl AsTableRow for Meme {
|
||||||
|
// fn as_table_row(&self) -> Row<'_> {
|
||||||
|
// Row::new(vec![
|
||||||
|
// TableCell::new(&self.link),
|
||||||
|
// TableCell::new(&self.category),
|
||||||
|
// TableCell::new(&self.user),
|
||||||
|
// TableCell::new(Local.timestamp(self.timestamp, 0).format("%F
|
||||||
|
// %R")), TableCell::new(&self.id),
|
||||||
|
// ])
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
// impl AsTableRow for User {
|
||||||
|
// fn as_table_row(&self) -> Row<'_> {
|
||||||
|
// Row::new(vec![
|
||||||
|
// TableCell::new(&self.name),
|
||||||
|
// TableCell::new(&self.get_id().map(String::as_ref).unwrap_or("[No
|
||||||
|
// ID]")), TableCell::new(&self.dayuploads),
|
||||||
|
// ])
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
pub fn list_table() -> Table {
|
||||||
|
let mut table = Table::new();
|
||||||
|
|
||||||
|
table
|
||||||
|
.load_preset(UTF8_NO_BORDERS)
|
||||||
|
.set_content_arrangement(ContentArrangement::Dynamic);
|
||||||
|
|
||||||
|
table
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait AsTableRow {
|
pub trait TableHeader {
|
||||||
fn as_table_row(&self) -> term_table::row::Row;
|
fn header() -> Row;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsTableRow for Category {
|
macro_rules! impl_table_header {
|
||||||
fn as_table_row(&self) -> Row<'_> {
|
($t:ident, $e:expr) => {
|
||||||
Row::new(vec![TableCell::new(&self.id), TableCell::new(&self.name)])
|
impl TableHeader for $t {
|
||||||
|
fn header() -> Row {
|
||||||
|
$e.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
impl_table_header!(User, vec!["Name", "ID"]);
|
||||||
|
impl_table_header!(Category, vec!["Name", "ID"]);
|
||||||
|
impl_table_header!(Meme, vec!["Link", "Category", "User", "Timestamp"]);
|
||||||
|
|
||||||
|
/// a newtype wrapper to convert libjens types to table rows
|
||||||
|
pub struct JMTableEntry<T>(pub T);
|
||||||
|
|
||||||
|
impl From<JMTableEntry<User>> for Row {
|
||||||
|
fn from(e: JMTableEntry<User>) -> Self {
|
||||||
|
vec![e.0.name, e.0.id.unwrap_or_else(String::new)].into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsTableRow for Meme {
|
impl From<JMTableEntry<Category>> for Row {
|
||||||
fn as_table_row(&self) -> Row<'_> {
|
fn from(e: JMTableEntry<Category>) -> Self {
|
||||||
Row::new(vec![
|
vec![e.0.name, e.0.id].into()
|
||||||
TableCell::new(&self.link),
|
|
||||||
TableCell::new(&self.category),
|
|
||||||
TableCell::new(&self.user),
|
|
||||||
TableCell::new(Local.timestamp(self.timestamp, 0).format("%F %R")),
|
|
||||||
TableCell::new(&self.id),
|
|
||||||
])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsTableRow for User {
|
impl From<JMTableEntry<Meme>> for Row {
|
||||||
fn as_table_row(&self) -> Row<'_> {
|
fn from(e: JMTableEntry<Meme>) -> Self {
|
||||||
Row::new(vec![
|
vec![
|
||||||
TableCell::new(&self.name),
|
e.0.link,
|
||||||
TableCell::new(&self.get_id().map(String::as_ref).unwrap_or("[No ID]")),
|
e.0.category,
|
||||||
TableCell::new(&self.dayuploads),
|
e.0.user,
|
||||||
])
|
Local
|
||||||
|
.timestamp(e.0.timestamp, 0)
|
||||||
|
.format("%F %R")
|
||||||
|
.to_string(),
|
||||||
|
]
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait TableExt {
|
||||||
|
fn add_rows<T: Into<Row>>(&mut self, iter: impl IntoIterator<Item = T>) -> &mut Self;
|
||||||
|
|
||||||
|
fn type_header<T: TableHeader>(&mut self) -> &mut Self;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TableExt for Table {
|
||||||
|
fn add_rows<T: Into<Row>>(&mut self, iter: impl IntoIterator<Item = T>) -> &mut Self {
|
||||||
|
for i in iter.into_iter() {
|
||||||
|
self.add_row(i);
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn type_header<T: TableHeader>(&mut self) -> &mut Self {
|
||||||
|
self.set_header(T::header())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,10 +16,10 @@ name = "jmtoken"
|
||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.34"
|
anyhow = "1.0.52"
|
||||||
clap = "2.33.3"
|
clap = "2.34.0"
|
||||||
md5 = "0.7.0"
|
md5 = "0.7.0"
|
||||||
reqwest = "0.10.8"
|
reqwest = "0.11.8"
|
||||||
serde = { version = "1.0.117", features = ["derive"] }
|
serde = { version = "1.0.132", features = ["derive"] }
|
||||||
serde_json = "1.0.59"
|
serde_json = "1.0.73"
|
||||||
tokio = { version = "~0.2", features = ["full"] }
|
tokio = { version = "1.15.0", features = ["full"] }
|
||||||
|
|
Loading…
Reference in a new issue