Skip to content

Commit

Permalink
refactor(log): new color api (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
benpueschel committed Jul 25, 2024
1 parent 79f1f0b commit 08c1e7c
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 102 deletions.
14 changes: 9 additions & 5 deletions src/commands/auth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::args::Auth;
use crate::error::Result;
use crate::log::Highlight;
use crate::log::{Highlight, Paint};
use std::io::{stdout, Write};

use super::{get_input, load_config};
Expand All @@ -9,18 +9,22 @@ pub async fn auth(args: Auth, config: &Option<String>) -> Result<()> {
let Auth { remote } = &args;
print!(
"Enter your {} for remote {} (leave blank to use a token): ",
Highlight::Username("username"), Highlight::Remote(remote)
"username".paint(Highlight::Username),
remote.paint(Highlight::Remote)
);
stdout().flush()?;
let username = get_input()?;

print!("Enter your {}: ", Highlight::Special("password or token"));
print!(
"Enter your {}: ",
"password or token".paint(Highlight::Special)
);
stdout().flush()?;
let password = rpassword::read_password()?;

println!(
"Adding authentication to remote {}...",
Highlight::Remote(remote)
remote.paint(Highlight::Remote)
);
let mut config = load_config(config)?;
if !username.is_empty() {
Expand All @@ -29,7 +33,7 @@ pub async fn auth(args: Auth, config: &Option<String>) -> Result<()> {
config.store_token(remote, &password)?;
println!(
"Authentication added to remote {}.",
Highlight::Remote(remote)
remote.paint(Highlight::Remote)
);
Ok(())
}
33 changes: 18 additions & 15 deletions src/commands/create_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::io::{stdout, Write};

use crate::config::{AuthConfig, Config, GitRemoteConfig, InlineSecrets, Secrets};
use crate::error::{Error, Result};
use crate::log::{self, Highlight};
use crate::log::{self, Highlight, Paint};
use crate::remote;

use super::get_input;

pub async fn create_config(cfg: &Option<String>) -> Result<()> {
println!("Welcome to {}!", Highlight::Special("gritty"));
println!("Welcome to {}!", "gritty".paint(Highlight::Special));
println!("This command will ask you some questions to create a config file.");
println!();

Expand All @@ -21,12 +21,12 @@ pub async fn create_config(cfg: &Option<String>) -> Result<()> {
config.path.clone_from(path);
println!(
"Using provided config file path: {}",
Highlight::Path(&config.path)
&config.path.paint(Highlight::Path)
);
} else {
println!(
"Enter the path to the config file (default is {}):",
Highlight::Path(&config.path)
&config.path.paint(Highlight::Path)
);
let path = get_input()?;
if !path.is_empty() {
Expand All @@ -42,11 +42,14 @@ pub async fn create_config(cfg: &Option<String>) -> Result<()> {
let secrets = {
println!(
"{} Use the system keyring ({})",
Highlight::Special("1."),
Highlight::Special("highly recommended, default")
"1.".paint(Highlight::Special),
"highly recommended, default".paint(Highlight::Special)
);
println!("{} In a plaintext secrets file", Highlight::Special("2."));
println!("{} In the config file", Highlight::Special("3."));
println!(
"{} In a plaintext secrets file",
"2.".paint(Highlight::Special)
);
println!("{} In the config file", "3.".paint(Highlight::Special));
log::print("> ");

let num = get_input()?;
Expand All @@ -60,10 +63,10 @@ pub async fn create_config(cfg: &Option<String>) -> Result<()> {
let secrets = {
println!(
"{} In a plaintext secrets file ({})",
Highlight::Special("1."),
Highlight::Special("default")
"1.".paint(Highlight::Special),
"default".paint(Highlight::Special)
);
println!("{} In the config file", Highlight::Special("2."));
println!("{} In the config file", "2.".paint(Highlight::Special));

log::print("> ");
let num = get_input()?;
Expand Down Expand Up @@ -112,7 +115,7 @@ fn ask_for_remote() -> Result<(String, GitRemoteConfig, Option<AuthConfig>)> {
}
print!(
"Enter the name of the remote ({}): ",
Highlight::Remote("github/gitea")
"github/gitea".paint(Highlight::Remote)
);
// we need to flush stdout, this is the cleanest way to do it
log::print("");
Expand Down Expand Up @@ -143,7 +146,7 @@ fn ask_for_remote() -> Result<(String, GitRemoteConfig, Option<AuthConfig>)> {

print!(
"Enter the clone protocol ({}): ",
Highlight::Protocol("ssh/https")
"ssh/https".paint(Highlight::Protocol)
);
log::print("");
let clone_protocol = get_input()?;
Expand All @@ -167,7 +170,7 @@ fn ask_for_remote() -> Result<(String, GitRemoteConfig, Option<AuthConfig>)> {
log::print("Enter token: ");
stdout().flush()?;
let token = rpassword::read_password()?;
println!("Token added to remote {}.", Highlight::Remote(&name));
println!("Token added to remote {}.", &name.paint(Highlight::Remote));
Some(AuthConfig {
username: None,
password: None,
Expand Down Expand Up @@ -196,7 +199,7 @@ fn ask_for_secrets_file() -> Result<Secrets> {
let mut path = format!("{xdg_config}/gritty/secrets.toml");
print!(
"Enter the path to the secrets file (default is {}): ",
Highlight::Path(&path)
path.paint(Highlight::Path)
);
let input = get_input()?;
if !input.is_empty() {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/create_repository.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::args::Create;
use crate::error::Result;
use crate::log::Highlight;
use crate::log::{Highlight, Paint};
use crate::remote::RepoCreateInfo;

use super::load_remote;
Expand All @@ -18,7 +18,7 @@ pub async fn create_repository(args: Create, config: &Option<String>) -> Result<
remote,
} = args;
let remote = load_remote(&remote, config).await?;
println!("Creating repository {}...", Highlight::Repo(&name));
println!("Creating repository {}...", name.paint(Highlight::Repo));
let info = RepoCreateInfo {
name: name.clone(),
description,
Expand All @@ -27,7 +27,7 @@ pub async fn create_repository(args: Create, config: &Option<String>) -> Result<
private,
};
let url = remote.create_repo(info).await?;
println!("Repository created at: {}", Highlight::Url(&url));
println!("Repository created at: {}", url.paint(Highlight::Url));
if clone {
remote.clone_repo(&name, &name, recursive).await?;
} else if add_remote {
Expand Down
26 changes: 12 additions & 14 deletions src/commands/delete_repository.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::args::Delete;
use crate::error::{Error, Result};
use crate::log::{self, Highlight};
use crate::log::{self, Highlight, Paint};
use crate::remote::Repository;

use super::{get_input, load_remote};
Expand All @@ -22,40 +22,38 @@ pub async fn delete_repository(args: Delete, config: &Option<String>) -> Result<
}
};
if !force && !ask_for_confirmation(name, remote_name, &repo_info)? {
println!("{}", Highlight::Special("Operation cancelled."));
println!("{}", "Operation cancelled.".paint(Highlight::Special));
return Ok(());
}
remote.delete_repo(name).await?;
println!(
"Repository {} deleted on remote {}.",
Highlight::Repo(&name),
Highlight::Remote(&remote_name)
&name.paint(Highlight::Repo),
&remote_name.paint(Highlight::Remote)
);
Ok(())
}

fn ask_for_confirmation(name: &str, remote_name: &str, repo: &Repository) -> Result<bool> {
println!(
"{}: You are about to delete repository {} on remote {}.",
Highlight::Important("WARNING"),
Highlight::Repo(&name),
Highlight::Remote(&remote_name),
"WARNING".paint(Highlight::Important),
&name.paint(Highlight::Repo),
&remote_name.paint(Highlight::Remote),
);

if let Some(last) = repo.last_commits.first() {
// Only show the first line of the commit message
let message = last.message.split('\n').next().unwrap_or(&last.message);
println!(
"Last commit: {} - {} by {} on {}",
Highlight::Commit(last.sha.split_at(8).0),
Highlight::CommitMsg(message),
Highlight::Author(&last.author),
Highlight::Date(&last.date),
last.sha.split_at(8).0.paint(Highlight::Commit),
message.paint(Highlight::CommitMsg),
&last.author.paint(Highlight::Author),
&last.date.to_string().paint(Highlight::Date),
);
}
log::print(Highlight::Important(
"Are you sure you want to continue? (y/N): ",
));
log::print("Are you sure you want to continue? (y/N): ".paint(Highlight::Important));
let input = get_input()?;
// Only accept "y" or "Y" as confirmation, return false otherwise
Ok(input.eq_ignore_ascii_case("y"))
Expand Down
9 changes: 4 additions & 5 deletions src/commands/list_remotes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::Result;
use crate::log::{self, Highlight};
use crate::log::{self, Highlight, Paint};

use super::load_config;

Expand All @@ -17,13 +17,12 @@ pub async fn list_remotes(config: &Option<String>) -> Result<()> {
}
}
for (name, remote) in &config.remotes {
let name = Highlight::Remote(log::leftpad(name, longest_name));
let username = Highlight::Username(log::leftpad(&remote.username, longest_username));
let name = log::leftpad(name, longest_name).paint(Highlight::Remote);
let username = log::leftpad(&remote.username, longest_username).paint(Highlight::Username);
println!(
" {name} - username: {username} - url: {}",
Highlight::Url(&remote.url),
&remote.url.paint(Highlight::Url),
);
}
Ok(())
}

12 changes: 6 additions & 6 deletions src/commands/list_repositories.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::args::List;
use crate::error::Result;
use crate::log::{self, Highlight};
use crate::log::{self, Highlight, Paint};
use crate::remote::ListReposInfo;
use chrono::{DateTime, Local};

Expand All @@ -10,7 +10,7 @@ pub async fn list_repositories(args: List, config: &Option<String>) -> Result<()
let remote = &args.remote;
println!(
"Listing repositories on remote {}...",
Highlight::Remote(remote)
remote.paint(Highlight::Remote)
);

let remote = load_remote(remote, config).await?;
Expand Down Expand Up @@ -40,7 +40,7 @@ pub async fn list_repositories(args: List, config: &Option<String>) -> Result<()
print!(" ");
}

log::print(Highlight::Repo(log::leftpad(&repo.name, longest_name)));
log::print(log::leftpad(&repo.name, longest_name).paint(Highlight::Repo));

if repo.last_commits.is_empty() {
print!(" - no commits");
Expand All @@ -51,9 +51,9 @@ pub async fn list_repositories(args: List, config: &Option<String>) -> Result<()
let message = last.message.split('\n').next().unwrap_or(&last.message);
print!(
" - {}: {} - {}",
Highlight::Date(date),
Highlight::Commit(sha),
Highlight::CommitMsg(message)
date.to_string().paint(Highlight::Date),
sha.paint(Highlight::Commit),
message.paint(Highlight::CommitMsg)
);
}
println!();
Expand Down
Loading

0 comments on commit 08c1e7c

Please sign in to comment.