This commit is contained in:
Christoph J. Scherr 2024-06-27 13:50:12 +02:00
parent a4814d43e0
commit 8becdb231b
1 changed files with 9 additions and 8 deletions

View File

@ -3,14 +3,15 @@ use diesel_demo::models::{Post, PostDraft};
use libpt::log::{self, debug, error, trace, warn};
const HELP_TEXT: &str = "\
help - show this menu\n\
help|? - show this menu\n\
exit - exit the application\n\
list - list all posts\n\
publish [id] - publish the post with the id [id]\n\
unpublish [id] - make the post with the id [id] a draft\n\
delete [id] - delete the post with the id [id]\n\
read [id] - display the post with the id [id]\n\
read|show [id] - display the post with the id [id]\n\
new - create a new post";
const USAGE_TEXT: &str = "Bad input: try 'help'";
use colored::*;
@ -40,9 +41,9 @@ fn repl(conn: &mut SqliteConnection) -> anyhow::Result<()> {
loop {
lib::read_buf_interactive(&mut buf)?;
buf = buf.to_uppercase();
if buf.starts_with("HELP") {
if buf.starts_with("HELP") || buf.starts_with("?") {
println!("{}", HELP_TEXT.bright_blue())
} else if buf.starts_with("EXIT") {
} else if buf.starts_with("EXIT") || buf.is_empty() {
break;
} else if buf.starts_with("UNPUBLISH") {
let id: i32 = match get_id(&buf) {
@ -70,7 +71,7 @@ fn repl(conn: &mut SqliteConnection) -> anyhow::Result<()> {
}
}
};
} else if buf.starts_with("READ") {
} else if buf.starts_with("READ") || buf.starts_with("SHOW") {
let id: i32 = match get_id(&buf) {
Some(i) => i,
None => continue,
@ -107,7 +108,7 @@ fn repl(conn: &mut SqliteConnection) -> anyhow::Result<()> {
} else if buf.starts_with("NEW") {
let post = PostDraft::interactive_create()?;
let _ = post.post(conn).inspect_err(|e| {
error!("Could not submit the post: {e:?}");
error!("Could not submit the post: {}", e.to_string());
});
} else {
usage()
@ -118,7 +119,7 @@ fn repl(conn: &mut SqliteConnection) -> anyhow::Result<()> {
}
fn usage() {
println!("Bad input: try 'help'");
println!("{}", USAGE_TEXT.red().bold());
}
fn get_id(buf: &str) -> Option<i32> {
@ -126,7 +127,7 @@ fn get_id(buf: &str) -> Option<i32> {
Some(s) => match s.parse() {
Ok(i) => Some(i),
Err(e) => {
error!("could not parse the id: {e:?}");
error!("could not parse the id: {}", e.to_string());
None
}
},