feat(integration): Add simple integration for nushell
This commit is contained in:
parent
44dd99e543
commit
ef191c6a40
@ -13,6 +13,7 @@ const APP_ORG: &str = "valeth";
|
|||||||
static CONFIG: OnceLock<PathBuf> = OnceLock::new();
|
static CONFIG: OnceLock<PathBuf> = OnceLock::new();
|
||||||
static DATA: OnceLock<PathBuf> = OnceLock::new();
|
static DATA: OnceLock<PathBuf> = OnceLock::new();
|
||||||
static CACHE: OnceLock<PathBuf> = OnceLock::new();
|
static CACHE: OnceLock<PathBuf> = OnceLock::new();
|
||||||
|
static SELECTED_PROJECT_FILE: OnceLock<PathBuf> = OnceLock::new();
|
||||||
|
|
||||||
|
|
||||||
pub fn config_path() -> &'static PathBuf {
|
pub fn config_path() -> &'static PathBuf {
|
||||||
@ -27,6 +28,10 @@ pub fn cache_path() -> &'static PathBuf {
|
|||||||
CACHE.get_or_init(|| init("XDG_CACHE_HOME", ".cache").unwrap())
|
CACHE.get_or_init(|| init("XDG_CACHE_HOME", ".cache").unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn selected_project_file() -> &'static PathBuf {
|
||||||
|
SELECTED_PROJECT_FILE.get_or_init(|| cache_path().join("selected-project"))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn init(xdg_var: &str, fallback_home_path: &str) -> Result<PathBuf> {
|
fn init(xdg_var: &str, fallback_home_path: &str) -> Result<PathBuf> {
|
||||||
let path = xdg_path_with_fallback(xdg_var, fallback_home_path)?;
|
let path = xdg_path_with_fallback(xdg_var, fallback_home_path)?;
|
||||||
|
11
src/integrations/nushell.nu
Normal file
11
src/integrations/nushell.nu
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
def --wrapped --env p [...rest: string] {
|
||||||
|
let selected_project = "{%SELECTED_PROJECT_FILE%}" | path expand
|
||||||
|
|
||||||
|
run-external {%PROJ_EXE%} ...$rest
|
||||||
|
|
||||||
|
# The file will not exist if the prompt was aborted
|
||||||
|
if ($selected_project | path exists) {
|
||||||
|
let dir = open $selected_project
|
||||||
|
cd $dir
|
||||||
|
}
|
||||||
|
}
|
41
src/main.rs
41
src/main.rs
@ -10,6 +10,28 @@ use anyhow::{ensure, Result};
|
|||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, clap::ValueEnum)]
|
||||||
|
enum ShellType {
|
||||||
|
Nu,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ShellType {
|
||||||
|
pub fn integration(&self) -> String {
|
||||||
|
let template = match self {
|
||||||
|
Self::Nu => include_str!("./integrations/nushell.nu"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let file_path = dbg!(dirs::selected_project_file().to_str().unwrap());
|
||||||
|
let current_exe = std::env::current_exe().unwrap();
|
||||||
|
let current_exe = current_exe.to_str().unwrap();
|
||||||
|
|
||||||
|
template
|
||||||
|
.replace("{%SELECTED_PROJECT_FILE%}", file_path)
|
||||||
|
.replace("{%PROJ_EXE%}", current_exe)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
@ -23,6 +45,9 @@ enum Command {
|
|||||||
|
|
||||||
/// List existing projects
|
/// List existing projects
|
||||||
List,
|
List,
|
||||||
|
|
||||||
|
/// Initialize shell integration
|
||||||
|
Init { shell: ShellType },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -71,12 +96,20 @@ fn handle_subcommands(mut projects: Projects, cmd: &Command) -> Result<()> {
|
|||||||
Command::List => {
|
Command::List => {
|
||||||
list_projects(&projects)?;
|
list_projects(&projects)?;
|
||||||
}
|
}
|
||||||
|
Command::Init { shell } => {
|
||||||
|
print_shell_integration(shell);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn print_shell_integration(shell: &ShellType) {
|
||||||
|
println!("{}", shell.integration());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn add_project<P>(projects: &mut Projects, path: P) -> Result<()>
|
fn add_project<P>(projects: &mut Projects, path: P) -> Result<()>
|
||||||
where
|
where
|
||||||
P: Into<PathBuf>,
|
P: Into<PathBuf>,
|
||||||
@ -105,9 +138,9 @@ fn list_projects(projects: &Projects) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn clear_selected_project_file() -> Result<()> {
|
fn clear_selected_project_file() -> Result<()> {
|
||||||
let cache_file = dirs::cache_path().join("selected-project");
|
let cache_file = dirs::selected_project_file();
|
||||||
|
|
||||||
if fs::exists(&cache_file)? {
|
if fs::exists(cache_file)? {
|
||||||
fs::remove_file(cache_file)?;
|
fs::remove_file(cache_file)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,13 +148,15 @@ fn clear_selected_project_file() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn write_selected_project_file(path: PathBuf) -> Result<()> {
|
fn write_selected_project_file(path: PathBuf) -> Result<()> {
|
||||||
let cache_file = dirs::cache_path().join("selected-project");
|
let cache_file = dirs::selected_project_file();
|
||||||
|
|
||||||
let mut file = fs::OpenOptions::new()
|
let mut file = fs::OpenOptions::new()
|
||||||
.write(true)
|
.write(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.truncate(true)
|
.truncate(true)
|
||||||
.open(cache_file)?;
|
.open(cache_file)?;
|
||||||
write!(file, "{}", path.display())?;
|
write!(file, "{}", path.display())?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user