Add some log output / feedback to completion --install
This commit is contained in:
parent
c86978b5ac
commit
c5de9adcd3
|
@ -1,4 +1,30 @@
|
||||||
use std::cmp;
|
use owo_colors::{OwoColorize, Stream};
|
||||||
|
use std::{self, cmp, fmt::Display};
|
||||||
|
|
||||||
|
pub fn say<A>(what: A)
|
||||||
|
where
|
||||||
|
A: Display,
|
||||||
|
{
|
||||||
|
eprintln!("{}", what)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fmt_step<A>(
|
||||||
|
f: &mut std::fmt::Formatter,
|
||||||
|
title: &str,
|
||||||
|
payload: &A,
|
||||||
|
) -> std::result::Result<(), std::fmt::Error>
|
||||||
|
where
|
||||||
|
A: Display,
|
||||||
|
{
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{:>13} {}",
|
||||||
|
title
|
||||||
|
.if_supports_color(Stream::Stderr, |s| s.bold())
|
||||||
|
.if_supports_color(Stream::Stderr, |s| s.purple()),
|
||||||
|
payload.if_supports_color(Stream::Stderr, |s| s.bold())
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn ansi_len(s: &str) -> usize {
|
pub fn ansi_len(s: &str) -> usize {
|
||||||
String::from_utf8(strip_ansi_escapes::strip(s).unwrap())
|
String::from_utf8(strip_ansi_escapes::strip(s).unwrap())
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
use std::{fs::File, io::Write, path::Path};
|
use crate::{cmd::Cmd as MainCmd, pretty};
|
||||||
|
|
||||||
use crate::cmd::Cmd as MainCmd;
|
|
||||||
use clap::{Command, Subcommand};
|
use clap::{Command, Subcommand};
|
||||||
use clap_complete::{generate, Shell};
|
use clap_complete::{generate, Shell};
|
||||||
use std::fs::OpenOptions;
|
use std::{
|
||||||
|
fmt::{self, Display},
|
||||||
|
fs::{File, OpenOptions},
|
||||||
|
io::Write,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
/// Generates shell completion scripts
|
/// Generates shell completion scripts
|
||||||
#[derive(clap::Args)]
|
#[derive(clap::Args)]
|
||||||
|
@ -37,6 +40,7 @@ fn zsh() -> miette::Result<()> {
|
||||||
let oh_my_zsh_path = Path::new(&home).join(".oh-my-zsh");
|
let oh_my_zsh_path = Path::new(&home).join(".oh-my-zsh");
|
||||||
|
|
||||||
if oh_my_zsh_path.exists() {
|
if oh_my_zsh_path.exists() {
|
||||||
|
pretty::say(Log::Detecting("oh-my-zsh!"));
|
||||||
let completions_path = oh_my_zsh_path.join("completions");
|
let completions_path = oh_my_zsh_path.join("completions");
|
||||||
|
|
||||||
let aiken_completion_path = completions_path.join("_aiken");
|
let aiken_completion_path = completions_path.join("_aiken");
|
||||||
|
@ -46,7 +50,7 @@ fn zsh() -> miette::Result<()> {
|
||||||
"Cannot create directory: {completions_path.into_os_string().into_string()}",
|
"Cannot create directory: {completions_path.into_os_string().into_string()}",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
pretty::say(Log::Installing(&aiken_completion_path));
|
||||||
completion_file = File::create(aiken_completion_path)
|
completion_file = File::create(aiken_completion_path)
|
||||||
.expect("Cannot open file at: {aiken_completion_path.into_os_string().into_string()}");
|
.expect("Cannot open file at: {aiken_completion_path.into_os_string().into_string()}");
|
||||||
|
|
||||||
|
@ -55,32 +59,28 @@ fn zsh() -> miette::Result<()> {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if data_home.exists() {
|
let home_exists = data_home.exists();
|
||||||
let completion_path = xdg_dirs
|
|
||||||
.place_data_file("_aiken")
|
|
||||||
.expect("cannot create directory");
|
|
||||||
|
|
||||||
completion_file = File::create(completion_path)
|
|
||||||
.expect("Cannot open file at: {completion_path.into_os_string().into_string()}");
|
|
||||||
|
|
||||||
generate_wrapper(Shell::Zsh, &mut completion_file);
|
|
||||||
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
let completion_path = xdg_dirs
|
let completion_path = xdg_dirs
|
||||||
.place_data_file("_aiken")
|
.place_data_file("_aiken")
|
||||||
.expect("cannot create directory");
|
.expect("cannot create directory");
|
||||||
|
pretty::say(Log::Installing(&completion_path));
|
||||||
completion_file = File::create(completion_path)
|
completion_file = File::create(completion_path)
|
||||||
.expect("Cannot open file at: {completion_path.into_os_string().into_string()}");
|
.expect("Cannot open file at: {completion_path.into_os_string().into_string()}");
|
||||||
|
|
||||||
|
if home_exists {
|
||||||
|
generate_wrapper(Shell::Zsh, &mut completion_file);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
let mut zshrc = OpenOptions::new()
|
let mut zshrc = OpenOptions::new()
|
||||||
.write(true)
|
.write(true)
|
||||||
.append(true)
|
.append(true)
|
||||||
.open(format!("{}/.zshrc", home))
|
.open(format!("{}/.zshrc", home))
|
||||||
.expect(".zshrc file not found");
|
.expect(".zshrc file not found");
|
||||||
|
|
||||||
|
pretty::say(Log::Adjusting(".zshrc"));
|
||||||
|
|
||||||
if let Some(home) = data_home.to_str() {
|
if let Some(home) = data_home.to_str() {
|
||||||
let fpath: String = format!(r#"fpath=($fpath "{}")"#, home);
|
let fpath: String = format!(r#"fpath=($fpath "{}")"#, home);
|
||||||
|
|
||||||
|
@ -105,6 +105,8 @@ fn fish() -> miette::Result<()> {
|
||||||
.place_config_file("aiken.fish")
|
.place_config_file("aiken.fish")
|
||||||
.expect("Cannot create path");
|
.expect("Cannot create path");
|
||||||
|
|
||||||
|
pretty::say(Log::Installing(&completion_path));
|
||||||
|
|
||||||
let mut completion_file = File::create(completion_path)
|
let mut completion_file = File::create(completion_path)
|
||||||
.expect("Cannot open file at: {completion_path.into_os_string().into_string()}");
|
.expect("Cannot open file at: {completion_path.into_os_string().into_string()}");
|
||||||
|
|
||||||
|
@ -128,6 +130,7 @@ fn bash() -> miette::Result<()> {
|
||||||
let completion_path = xdg_dirs
|
let completion_path = xdg_dirs
|
||||||
.place_config_file(aiken_bash)
|
.place_config_file(aiken_bash)
|
||||||
.expect("Cannot create completion file {aiken_bash} under xdg directories");
|
.expect("Cannot create completion file {aiken_bash} under xdg directories");
|
||||||
|
pretty::say(Log::Installing(&completion_path));
|
||||||
|
|
||||||
let mut bashrc = OpenOptions::new()
|
let mut bashrc = OpenOptions::new()
|
||||||
.write(true)
|
.write(true)
|
||||||
|
@ -138,6 +141,8 @@ fn bash() -> miette::Result<()> {
|
||||||
if let Some(config) = config_home.to_str() {
|
if let Some(config) = config_home.to_str() {
|
||||||
let path: String = format!("source {config}");
|
let path: String = format!("source {config}");
|
||||||
|
|
||||||
|
pretty::say(Log::Adjusting(".bashrc"));
|
||||||
|
|
||||||
if let Err(e) = writeln!(bashrc, "{}", path) {
|
if let Err(e) = writeln!(bashrc, "{}", path) {
|
||||||
eprintln!("Couldn't write to file: {}", e);
|
eprintln!("Couldn't write to file: {}", e);
|
||||||
}
|
}
|
||||||
|
@ -171,9 +176,30 @@ fn completions_to_file(shell: Shell) -> miette::Result<()> {
|
||||||
pub fn exec(cmd_args: Args, shell: Shell) -> miette::Result<()> {
|
pub fn exec(cmd_args: Args, shell: Shell) -> miette::Result<()> {
|
||||||
if cmd_args.install {
|
if cmd_args.install {
|
||||||
completions_to_file(shell)?;
|
completions_to_file(shell)?;
|
||||||
|
pretty::say(Log::Done(&shell));
|
||||||
} else {
|
} else {
|
||||||
generate_wrapper(shell, &mut std::io::stdout());
|
generate_wrapper(shell, &mut std::io::stdout());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum Log<'a> {
|
||||||
|
Detecting(&'a str),
|
||||||
|
Installing(&'a PathBuf),
|
||||||
|
Adjusting(&'a str),
|
||||||
|
Done(&'a Shell),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Display for Log<'a> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {
|
||||||
|
match self {
|
||||||
|
Log::Detecting(what) => pretty::fmt_step(f, "Detecting", what),
|
||||||
|
Log::Installing(path) => pretty::fmt_step(f, "Creating", &path.display()),
|
||||||
|
Log::Adjusting(what) => pretty::fmt_step(f, "Adjusting", what),
|
||||||
|
Log::Done(shell) => {
|
||||||
|
pretty::fmt_step(f, "Done", &format!("installing {} auto-completion", shell))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue