Skip to content

Commit

Permalink
feat(ing list): align option
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Sep 19, 2023
1 parent 39c722b commit 2029850
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 42 deletions.
8 changes: 8 additions & 0 deletions src/args/cmd/ing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,13 @@ pub enum Cmd {
#[arg(value_name = "TYPE")]
#[arg(default_value = "public")]
r#type: Option<IngType>,

#[arg(verbatim_doc_comment)]
/// Align ing content to user name automatically
/// Example: cnb ing list --align
#[arg(long)]
#[arg(value_name = "BOOL")]
#[arg(default_value_t = true)]
align: bool,
},
}
7 changes: 4 additions & 3 deletions src/args/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ pub const fn logout(args: &Args) -> bool {
)
}

pub fn list_ing(args: &Args) -> Option<(usize, usize, IngType)> {
pub fn list_ing(args: &Args) -> Option<(usize, usize, IngType, bool)> {
match args {
Args {
cmd:
Some(Cmd::Ing(cmd::ing::Opt {
cmd: Some(cmd::ing::Cmd::List { r#type }),
cmd: Some(cmd::ing::Cmd::List { r#type, align }),
publish: None,
comment: None,
})),
Expand All @@ -141,7 +141,8 @@ pub fn list_ing(args: &Args) -> Option<(usize, usize, IngType)> {
} => {
let skip = get_skip(skip);
let take = get_take(take);
(skip, take, r#type.clone().unwrap_or(IngType::Public))
let r#type = r#type.clone().unwrap_or(IngType::Public);
(skip, take, r#type, *align)
}
_ => return None,
}
Expand Down
10 changes: 7 additions & 3 deletions src/display/colorful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn user_info(info: &Result<UserInfo>) {
}
}

pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool) {
pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool, align: bool) {
let ing_list = match ing_list {
Ok(o) => o,
Err(e) => return println_err(e),
Expand Down Expand Up @@ -83,8 +83,12 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: b
}
println!(" {} {}", "#".dimmed(), ing.id.to_string().dimmed());
let user_name_width = ing.user_name.width_cjk();
let content = fmt_content(&ing.content)
.replace('\n', &format!("\n{}", " ".repeat(user_name_width + 3)));
let content = if align {
fmt_content(&ing.content)
.replace('\n', &format!("\n{}", " ".repeat(user_name_width + 3)))
} else {
fmt_content(&ing.content)
};
println!(" {} {}", ing.user_name.cyan(), content);

let len = comment_list.len();
Expand Down
5 changes: 3 additions & 2 deletions src/display/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ pub fn list_ing(
style: &Style,
ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
rev: bool,
align: bool,
) {
match style {
Style::Colorful => colorful::list_ing(ing_list, rev),
Style::Normal => normal::list_ing(ing_list, rev),
Style::Colorful => colorful::list_ing(ing_list, rev, align),
Style::Normal => normal::list_ing(ing_list, rev, align),
Style::Json => json::list_ing(ing_list, rev),
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/display/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn user_info(info: &Result<UserInfo>) {
}
}

pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool) {
pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool, align: bool) {
let ing_list = match ing_list {
Ok(o) => o,
Err(e) => return println_err(e),
Expand Down Expand Up @@ -83,8 +83,12 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: b
}
println!(" # {}", ing.id);
let user_name_width = ing.user_name.width_cjk();
let content = fmt_content(&ing.content)
.replace('\n', &format!("\n{}", " ".repeat(user_name_width + 3)));
let content = if align {
fmt_content(&ing.content)
.replace('\n', &format!("\n{}", " ".repeat(user_name_width + 3)))
} else {
fmt_content(&ing.content)
};
println!(" {} {}", ing.user_name, content);

let len = comment_list.len();
Expand Down
62 changes: 31 additions & 31 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,88 +53,88 @@ async fn main() -> Result<()> {
match args {
_ if let Some(pat) = parser::login(&args) => {
let cfg_path = session::login(pat);
foe.then(||panic_if_err(&cfg_path));
quiet.not().then(||display::login(style, &cfg_path));
foe.then(|| panic_if_err(&cfg_path));
quiet.not().then(|| display::login(style, &cfg_path));
}
_ if parser::logout(&args) => {
let cfg_path = &session::logout();
foe.then(||panic_if_err(cfg_path));
quiet.not().then(||display::logout(style, cfg_path));
foe.then(|| panic_if_err(cfg_path));
quiet.not().then(|| display::logout(style, cfg_path));
}
_ if parser::user_info(&args) => {
let user_info = try {
User::new(pat?).get_info().await?
};
foe.then(||panic_if_err(&user_info));
quiet.not().then(||display::user_info(style, &user_info));
foe.then(|| panic_if_err(&user_info));
quiet.not().then(|| display::user_info(style, &user_info));
}
_ if let Some((skip, take, r#type)) = parser::list_ing(&args) => {
_ if let Some((skip, take, r#type, align)) = parser::list_ing(&args) => {
let ing_vec = try {
Ing::new(pat?).get_list(skip, take, &r#type).await?
};
foe.then(||panic_if_err(&ing_vec));
quiet.not().then(||display::list_ing(style, &ing_vec, rev));
foe.then(|| panic_if_err(&ing_vec));
quiet.not().then(|| display::list_ing(style, &ing_vec, rev, align));
}
_ if let Some(content) = parser::publish_ing(&args) => {
let content = try {
Ing::new(pat?).publish(content).await?;
content
};
foe.then(||panic_if_err(&content));
quiet.not().then(||display::publish_ing(style, &content));
foe.then(|| panic_if_err(&content));
quiet.not().then(|| display::publish_ing(style, &content));
}
_ if let Some((content, id))= parser::comment_ing(&args) => {
_ if let Some((content, id)) = parser::comment_ing(&args) => {
let content = try {
Ing::new(pat?).comment(id, content.clone(), None, None).await?;
content
};
foe.then(||panic_if_err(&content));
quiet.not().then(||display::comment_ing(style, &content));
foe.then(|| panic_if_err(&content));
quiet.not().then(|| display::comment_ing(style, &content));
}
_ if let Some(id) = parser::show_post(&args) => {
let entry = try { Post::new(pat?).get_one(id).await? };
foe.then(||panic_if_err(&entry));
quiet.not().then(||display::show_post(style, &entry));
foe.then(|| panic_if_err(&entry));
quiet.not().then(|| display::show_post(style, &entry));
}
_ if let Some(id) = parser::show_post_meta(&args) => {
let entry = try { Post::new(pat?).get_one(id).await? };
foe.then(||panic_if_err(&entry));
quiet.not().then(||display::show_post_meta(style, &entry));
foe.then(|| panic_if_err(&entry));
quiet.not().then(|| display::show_post_meta(style, &entry));
}
_ if let Some((skip, take)) = parser::list_post(&args) => {
let result = try { Post::new(pat?).get_meta_list(skip, take).await? };
foe.then(||panic_if_err(&result));
quiet.not().then(||display::list_post(style, &result, rev));
foe.then(|| panic_if_err(&result));
quiet.not().then(|| display::list_post(style, &result, rev));
}
_ if let Some(id) = parser::delete_post(&args) => {
let id = try {
Post::new(pat?).del_one(id).await?;
id
};
foe.then(||panic_if_err(&id));
quiet.not().then(||display::delete_post(style, &id));
foe.then(|| panic_if_err(&id));
quiet.not().then(|| display::delete_post(style, &id));
}
_ if let Some((kw, skip, take)) = parser::search_post(&args) => {
let result = try { Post::new(pat?).search(skip, take, kw).await? };
foe.then(||panic_if_err(&result));
quiet.not().then(||display::search_post(style, &result, rev));
foe.then(|| panic_if_err(&result));
quiet.not().then(|| display::search_post(style, &result, rev));
}
_ if let Some((title, body, publish)) = parser::create_post(&args) => {
let id = try { Post::new(pat?).create(title, body, publish).await? };
foe.then(||panic_if_err(&id));
quiet.not().then(||display::create_post(style, &id));
foe.then(|| panic_if_err(&id));
quiet.not().then(|| display::create_post(style, &id));
}
_ if let Some((id, title, body, publish)) = parser::update_post(&args) => {
let id = try { Post::new(pat?).update(id,title, body, publish).await? };
foe.then(||panic_if_err(&id));
quiet.not().then(||display::update_post(style, &id));
let id = try { Post::new(pat?).update(id, title, body, publish).await? };
foe.then(|| panic_if_err(&id));
quiet.not().then(|| display::update_post(style, &id));
}
_ if let Some((skip, take)) = parser::list_news(&args) => {
let news_vec = try {
News::new(pat?).get_list(skip, take).await?
};
foe.then(||panic_if_err(&news_vec));
quiet.not().then(||display::list_news(style, &news_vec, rev));
foe.then(|| panic_if_err(&news_vec));
quiet.not().then(|| display::list_news(style, &news_vec, rev));
}

_ if no_operation(&args) => {
Expand Down

0 comments on commit 2029850

Please sign in to comment.