Skip to content

Commit

Permalink
Bug fixes: formatting, ui behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ge3224 committed Apr 4, 2023
1 parent dd36bae commit dac804b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "neocities_cli"
version = "0.1.3"
author = ["Jacob Benison <[email protected]>"]
authors = ["Jacob Benison"]
description = "A CLI tool for managing websites hosted on Neocities."
keywords = ["neocities", "webmaster", "open-source", "static-site"]
repository = "https://github.com/ge3224/neocities"
Expand Down
38 changes: 22 additions & 16 deletions src/client/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,43 +38,46 @@ impl Delete {

fn warning(
&self,
args: &Vec<String>,
args: Vec<String>,
mut writer: impl std::io::Write,
) -> Result<bool, NeocitiesErr> {
let intro =
format!("\x1b[93mWarning.\x1b[0m Are you sure you want to delete the following files?");
self.write(intro.as_str(), &mut writer)?;
let warn = "\x1b[93mWarning.\x1b[0m Are you sure you want to delete the following files?\n";
self.write(warn, &mut writer)?;

for (i, arg) in args.iter().enumerate() {
let item = format!("{}: \x1b[92m{}\x1b[0m", i + 1, arg);
let item = format!("{}: \x1b[92m{}\x1b[0m\n", i + 1, arg);
self.write(item.as_str(), &mut writer)?;
}

self.write("Please input either Y or N.", &mut writer)?;
self.write("Please input either Y or N.\n", &mut writer)?;

let mut cancel_delete = true;

loop {
let mut input = String::new();

io::stdin().read_line(&mut input).unwrap();
io::stdin().read_line(&mut input)?;

let input = input.trim();

match input {
"Y" | "y" => {
self.write("Ok. Continuing with delete of files.", &mut writer)?;
self.write("Ok. Continuing with delete of files.\n", &mut writer)?;
cancel_delete = false;
break;
}
"N" | "n" => {
self.write("Canceling delete operation.", &mut writer)?;
return Ok(false);
self.write("Canceling delete operation.\n", &mut writer)?;
break;
}
_ => {
let err = format!("Invalid input: '{}'. Please try again.", input);
let err = format!("Invalid input: '{}'. Please try again.\n", input);
self.write(err.as_str(), &mut writer)?;
}
}
}
Ok(true)

Ok(cancel_delete)
}
}

Expand All @@ -83,7 +86,7 @@ impl Executable for Delete {
let mut stdout = std::io::stdout();

if args.len() < 1 {
let output = format!("\n{}\nusage: {}\n", self.get_long_desc(), self.get_usage());
let output = format!("{}\nusage: {}\n", self.get_long_desc(), self.get_usage());
self.write(output.as_str(), &mut stdout)?;
return Ok(());
}
Expand All @@ -93,11 +96,14 @@ impl Executable for Delete {
return Ok(());
}

let proceed = self.warning(&args, &mut stdout)?;
let cancel = self.warning(args[..].to_vec(), &mut stdout)?;

if proceed == true {
if cancel == false {
let data = NcDelete::fetch(args)?;
let output = format!("{} - {}", data.result, data.message);
let output = format!(
"\x1b[93mStatus\x1b[0m: {} - {}\n",
data.result, data.message
);
self.write(output.as_str(), &mut stdout)?;
}

Expand Down
7 changes: 3 additions & 4 deletions src/client/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Key {
mut writer: impl std::io::Write,
) -> Result<Option<(String, String)>, NeocitiesErr> {
if let Some(key) = cred.get_api_key() {
let output = format!("{KEY_SET_MSG}: {}", key);
let output = format!("{KEY_SET_MSG}\n{}\n", key);
writer.write_all(output.as_bytes())?;
return Ok(None);
}
Expand Down Expand Up @@ -106,9 +106,8 @@ const DESC: &'static str = "Retrieve an API Key for your Neocities user account"

const DESC_SHORT: &'static str = "Neocities API Key";

const KEY_SET_MSG: &'static str = "
Your Neocities API key has already been set for the NEOCITIES_KEY environment variable
";
const KEY_SET_MSG: &'static str =
"Your Neocities API key is already set on your local machine, $NEOCITIES_KEY:";

const USE_KEY_MSG: &'static str = "
Use your API key by setting the following environment variable:
Expand Down
13 changes: 6 additions & 7 deletions src/client/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ impl List {
}

fn write(&self, msg: &str, mut writer: impl std::io::Write) -> Result<(), NeocitiesErr> {
let output = format!("\n{}\n", msg);
writer.write_all(output.as_bytes())?;
writer.write_all(msg.as_bytes())?;
Ok(())
}

Expand Down Expand Up @@ -75,7 +74,7 @@ impl List {
}
}
} else {
self.write("No files were found", &mut writer)?;
self.write("No files were found\n", &mut writer)?;
};

Ok(())
Expand Down Expand Up @@ -104,10 +103,10 @@ impl List {

let output: String;
if is_dir {
output = format!("{}{}/\x1b[90m {}\x1b[0m", self.dir_color, path, date);
output = format!("{}{}/\x1b[90m {}\x1b[0m\n", self.dir_color, path, date);
} else {
output = format!(
"{}{}\x1b[0m ({})\x1b[90m {}\x1b[0m",
"{}{}\x1b[0m ({})\x1b[90m {}\x1b[0m\n",
self.file_color, path, file_size, date
);
}
Expand All @@ -124,9 +123,9 @@ impl List {
) -> Result<(), NeocitiesErr> {
let output: String;
if is_dir {
output = format!("{}{}/\x1b[0m", self.dir_color, path);
output = format!("{}{}/\x1b[0m\n", self.dir_color, path);
} else {
output = format!("{}{}\x1b[0m", self.file_color, path,);
output = format!("{}{}\x1b[0m\n", self.file_color, path,);
}
self.write(output.as_str(), &mut writer)?;

Expand Down
9 changes: 6 additions & 3 deletions src/client/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Upload {
}

fn write(&self, msg: &str, mut writer: impl std::io::Write) -> Result<(), NeocitiesErr> {
let output = format!("\n{}\n", msg);
let output = format!("{}", msg);
writer.write_all(output.as_bytes())?;
Ok(())
}
Expand All @@ -42,7 +42,10 @@ impl Upload {
res: UploadResponse,
mut writer: impl std::io::Write,
) -> Result<(), NeocitiesErr> {
let output = format!("\n{} - {}\n", &res.result, &res.message);
let output = format!(
"\x1b[93mStatus\x1b[0m: {} - {}\n",
&res.result, &res.message
);
writer.write_all(output.as_bytes())?;
Ok(())
}
Expand All @@ -53,7 +56,7 @@ impl Executable for Upload {
let mut stdout = std::io::stdout();

if args.len() < 1 {
let output = format!("{}\nusage: {}", self.get_long_desc(), self.get_usage());
let output = format!("{}\nusage: {}\n", self.get_long_desc(), self.get_usage());
self.write(output.as_str(), &mut stdout)?;
return Ok(());
}
Expand Down
5 changes: 3 additions & 2 deletions src/client/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use std::io;

/// The string literal a user must type to run functionality in this module
pub const KEY: &'static str = "version";
const DESC: &'static str = "Show the version number of the neocities client";
const DESC_SHORT: &'static str = "Show neocities version";

/// An implementation of `Executable` that outputs the version of this `neocities_cli` application
pub struct Version {
Expand Down Expand Up @@ -53,6 +51,9 @@ impl Executable for Version {
}
}

const DESC: &'static str = "Show the version number of this Neocities client";
const DESC_SHORT: &'static str = "Show neocities version";

#[cfg(test)]
mod tests {
use super::{Version, DESC, DESC_SHORT, KEY};
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("foo bar baz");
Binary file added tests/fixtures/images/baz.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit dac804b

Please sign in to comment.