Skip to content

Commit 2029850

Browse files
committed
feat(ing list): align option
1 parent 39c722b commit 2029850

File tree

6 files changed

+60
-42
lines changed

6 files changed

+60
-42
lines changed

src/args/cmd/ing.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,13 @@ pub enum Cmd {
4343
#[arg(value_name = "TYPE")]
4444
#[arg(default_value = "public")]
4545
r#type: Option<IngType>,
46+
47+
#[arg(verbatim_doc_comment)]
48+
/// Align ing content to user name automatically
49+
/// Example: cnb ing list --align
50+
#[arg(long)]
51+
#[arg(value_name = "BOOL")]
52+
#[arg(default_value_t = true)]
53+
align: bool,
4654
},
4755
}

src/args/parser.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ pub const fn logout(args: &Args) -> bool {
120120
)
121121
}
122122

123-
pub fn list_ing(args: &Args) -> Option<(usize, usize, IngType)> {
123+
pub fn list_ing(args: &Args) -> Option<(usize, usize, IngType, bool)> {
124124
match args {
125125
Args {
126126
cmd:
127127
Some(Cmd::Ing(cmd::ing::Opt {
128-
cmd: Some(cmd::ing::Cmd::List { r#type }),
128+
cmd: Some(cmd::ing::Cmd::List { r#type, align }),
129129
publish: None,
130130
comment: None,
131131
})),
@@ -141,7 +141,8 @@ pub fn list_ing(args: &Args) -> Option<(usize, usize, IngType)> {
141141
} => {
142142
let skip = get_skip(skip);
143143
let take = get_take(take);
144-
(skip, take, r#type.clone().unwrap_or(IngType::Public))
144+
let r#type = r#type.clone().unwrap_or(IngType::Public);
145+
(skip, take, r#type, *align)
145146
}
146147
_ => return None,
147148
}

src/display/colorful.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn user_info(info: &Result<UserInfo>) {
4949
}
5050
}
5151

52-
pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool) {
52+
pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool, align: bool) {
5353
let ing_list = match ing_list {
5454
Ok(o) => o,
5555
Err(e) => return println_err(e),
@@ -83,8 +83,12 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: b
8383
}
8484
println!(" {} {}", "#".dimmed(), ing.id.to_string().dimmed());
8585
let user_name_width = ing.user_name.width_cjk();
86-
let content = fmt_content(&ing.content)
87-
.replace('\n', &format!("\n{}", " ".repeat(user_name_width + 3)));
86+
let content = if align {
87+
fmt_content(&ing.content)
88+
.replace('\n', &format!("\n{}", " ".repeat(user_name_width + 3)))
89+
} else {
90+
fmt_content(&ing.content)
91+
};
8892
println!(" {} {}", ing.user_name.cyan(), content);
8993

9094
let len = comment_list.len();

src/display/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ pub fn list_ing(
3838
style: &Style,
3939
ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
4040
rev: bool,
41+
align: bool,
4142
) {
4243
match style {
43-
Style::Colorful => colorful::list_ing(ing_list, rev),
44-
Style::Normal => normal::list_ing(ing_list, rev),
44+
Style::Colorful => colorful::list_ing(ing_list, rev, align),
45+
Style::Normal => normal::list_ing(ing_list, rev, align),
4546
Style::Json => json::list_ing(ing_list, rev),
4647
}
4748
}

src/display/normal.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn user_info(info: &Result<UserInfo>) {
4949
}
5050
}
5151

52-
pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool) {
52+
pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool, align: bool) {
5353
let ing_list = match ing_list {
5454
Ok(o) => o,
5555
Err(e) => return println_err(e),
@@ -83,8 +83,12 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: b
8383
}
8484
println!(" # {}", ing.id);
8585
let user_name_width = ing.user_name.width_cjk();
86-
let content = fmt_content(&ing.content)
87-
.replace('\n', &format!("\n{}", " ".repeat(user_name_width + 3)));
86+
let content = if align {
87+
fmt_content(&ing.content)
88+
.replace('\n', &format!("\n{}", " ".repeat(user_name_width + 3)))
89+
} else {
90+
fmt_content(&ing.content)
91+
};
8892
println!(" {} {}", ing.user_name, content);
8993

9094
let len = comment_list.len();

src/main.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,88 +53,88 @@ async fn main() -> Result<()> {
5353
match args {
5454
_ if let Some(pat) = parser::login(&args) => {
5555
let cfg_path = session::login(pat);
56-
foe.then(||panic_if_err(&cfg_path));
57-
quiet.not().then(||display::login(style, &cfg_path));
56+
foe.then(|| panic_if_err(&cfg_path));
57+
quiet.not().then(|| display::login(style, &cfg_path));
5858
}
5959
_ if parser::logout(&args) => {
6060
let cfg_path = &session::logout();
61-
foe.then(||panic_if_err(cfg_path));
62-
quiet.not().then(||display::logout(style, cfg_path));
61+
foe.then(|| panic_if_err(cfg_path));
62+
quiet.not().then(|| display::logout(style, cfg_path));
6363
}
6464
_ if parser::user_info(&args) => {
6565
let user_info = try {
6666
User::new(pat?).get_info().await?
6767
};
68-
foe.then(||panic_if_err(&user_info));
69-
quiet.not().then(||display::user_info(style, &user_info));
68+
foe.then(|| panic_if_err(&user_info));
69+
quiet.not().then(|| display::user_info(style, &user_info));
7070
}
71-
_ if let Some((skip, take, r#type)) = parser::list_ing(&args) => {
71+
_ if let Some((skip, take, r#type, align)) = parser::list_ing(&args) => {
7272
let ing_vec = try {
7373
Ing::new(pat?).get_list(skip, take, &r#type).await?
7474
};
75-
foe.then(||panic_if_err(&ing_vec));
76-
quiet.not().then(||display::list_ing(style, &ing_vec, rev));
75+
foe.then(|| panic_if_err(&ing_vec));
76+
quiet.not().then(|| display::list_ing(style, &ing_vec, rev, align));
7777
}
7878
_ if let Some(content) = parser::publish_ing(&args) => {
7979
let content = try {
8080
Ing::new(pat?).publish(content).await?;
8181
content
8282
};
83-
foe.then(||panic_if_err(&content));
84-
quiet.not().then(||display::publish_ing(style, &content));
83+
foe.then(|| panic_if_err(&content));
84+
quiet.not().then(|| display::publish_ing(style, &content));
8585
}
86-
_ if let Some((content, id))= parser::comment_ing(&args) => {
86+
_ if let Some((content, id)) = parser::comment_ing(&args) => {
8787
let content = try {
8888
Ing::new(pat?).comment(id, content.clone(), None, None).await?;
8989
content
9090
};
91-
foe.then(||panic_if_err(&content));
92-
quiet.not().then(||display::comment_ing(style, &content));
91+
foe.then(|| panic_if_err(&content));
92+
quiet.not().then(|| display::comment_ing(style, &content));
9393
}
9494
_ if let Some(id) = parser::show_post(&args) => {
9595
let entry = try { Post::new(pat?).get_one(id).await? };
96-
foe.then(||panic_if_err(&entry));
97-
quiet.not().then(||display::show_post(style, &entry));
96+
foe.then(|| panic_if_err(&entry));
97+
quiet.not().then(|| display::show_post(style, &entry));
9898
}
9999
_ if let Some(id) = parser::show_post_meta(&args) => {
100100
let entry = try { Post::new(pat?).get_one(id).await? };
101-
foe.then(||panic_if_err(&entry));
102-
quiet.not().then(||display::show_post_meta(style, &entry));
101+
foe.then(|| panic_if_err(&entry));
102+
quiet.not().then(|| display::show_post_meta(style, &entry));
103103
}
104104
_ if let Some((skip, take)) = parser::list_post(&args) => {
105105
let result = try { Post::new(pat?).get_meta_list(skip, take).await? };
106-
foe.then(||panic_if_err(&result));
107-
quiet.not().then(||display::list_post(style, &result, rev));
106+
foe.then(|| panic_if_err(&result));
107+
quiet.not().then(|| display::list_post(style, &result, rev));
108108
}
109109
_ if let Some(id) = parser::delete_post(&args) => {
110110
let id = try {
111111
Post::new(pat?).del_one(id).await?;
112112
id
113113
};
114-
foe.then(||panic_if_err(&id));
115-
quiet.not().then(||display::delete_post(style, &id));
114+
foe.then(|| panic_if_err(&id));
115+
quiet.not().then(|| display::delete_post(style, &id));
116116
}
117117
_ if let Some((kw, skip, take)) = parser::search_post(&args) => {
118118
let result = try { Post::new(pat?).search(skip, take, kw).await? };
119-
foe.then(||panic_if_err(&result));
120-
quiet.not().then(||display::search_post(style, &result, rev));
119+
foe.then(|| panic_if_err(&result));
120+
quiet.not().then(|| display::search_post(style, &result, rev));
121121
}
122122
_ if let Some((title, body, publish)) = parser::create_post(&args) => {
123123
let id = try { Post::new(pat?).create(title, body, publish).await? };
124-
foe.then(||panic_if_err(&id));
125-
quiet.not().then(||display::create_post(style, &id));
124+
foe.then(|| panic_if_err(&id));
125+
quiet.not().then(|| display::create_post(style, &id));
126126
}
127127
_ if let Some((id, title, body, publish)) = parser::update_post(&args) => {
128-
let id = try { Post::new(pat?).update(id,title, body, publish).await? };
129-
foe.then(||panic_if_err(&id));
130-
quiet.not().then(||display::update_post(style, &id));
128+
let id = try { Post::new(pat?).update(id, title, body, publish).await? };
129+
foe.then(|| panic_if_err(&id));
130+
quiet.not().then(|| display::update_post(style, &id));
131131
}
132132
_ if let Some((skip, take)) = parser::list_news(&args) => {
133133
let news_vec = try {
134134
News::new(pat?).get_list(skip, take).await?
135135
};
136-
foe.then(||panic_if_err(&news_vec));
137-
quiet.not().then(||display::list_news(style, &news_vec, rev));
136+
foe.then(|| panic_if_err(&news_vec));
137+
quiet.not().then(|| display::list_news(style, &news_vec, rev));
138138
}
139139

140140
_ if no_operation(&args) => {

0 commit comments

Comments
 (0)