Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ps1 overwrite to the completion script. #201

Merged
merged 3 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use super::util::IndicatifWriter;
use crate::progress;
use clap::{CommandFactory, Parser};
use clap_complete::Shell;
use clap_complete;
use clap_verbosity_flag::Verbosity;
use miette::IntoDiagnostic;

use crate::progress;
use rattler_shell::shell::{Shell, ShellEnum};
use std::io::Write;
use std::str::FromStr;
use tracing_subscriber::{filter::LevelFilter, util::SubscriberInitExt, EnvFilter};

pub mod add;
Expand Down Expand Up @@ -35,7 +37,7 @@ struct Args {
pub struct CompletionCommand {
/// The shell to generate a completion script for (defaults to 'bash').
#[arg(short, long)]
shell: Option<Shell>,
shell: Option<clap_complete::Shell>,
}

#[derive(Parser, Debug)]
Expand All @@ -58,13 +60,39 @@ pub enum Command {
}

fn completion(args: CompletionCommand) -> miette::Result<()> {
let clap_shell = args
.shell
.or(clap_complete::Shell::from_env())
.unwrap_or(clap_complete::Shell::Bash);
clap_complete::generate(
args.shell.or(Shell::from_env()).unwrap_or(Shell::Bash),
clap_shell,
&mut Args::command(),
"pixi",
&mut std::io::stdout(),
);

// Create PS1 overwrite command
// TODO: Also make this work for non bourne shells.
let mut script = String::new();
let shell = ShellEnum::from_str(clap_shell.to_string().as_str()).into_diagnostic()?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should check if this works for all supported shells

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't ill make an issue for that.

// Generate a shell agnostic command to add the PIXI_PROMPT to the PS1 variable.
shell
.set_env_var(
&mut script,
"PS1",
format!(
"{}{}",
shell.format_env_var("PIXI_PROMPT"),
shell.format_env_var("PS1")
)
.as_str(),
)
.unwrap();
// Just like the clap autocompletion code write directly to the stdout
std::io::stdout()
.write_all(script.as_bytes())
.into_diagnostic()?;

Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions src/project/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn get_metadata_env(project: &Project) -> HashMap<String, String> {
format!("{ENV_PREFIX}ROOT"),
project.root().to_string_lossy().into_owned(),
),
(format!("{ENV_PREFIX}NAME"), project.name().to_string()),
(
format!("{ENV_PREFIX}MANIFEST"),
project.manifest_path().to_string_lossy().into_owned(),
Expand All @@ -44,5 +45,6 @@ pub fn get_metadata_env(project: &Project) -> HashMap<String, String> {
format!("{ENV_PREFIX}VERSION"),
project.version().to_string(),
),
("PIXI_PROMPT".to_string(), format!("({}) ", project.name())),
])
}
Loading