feat: add env syncing support for nushell

This commit is contained in:
YummyOreo 2024-04-28 21:40:29 -05:00
parent 36ddd98b6e
commit e4bd0e4894
No known key found for this signature in database
GPG Key ID: E8E87F141BD9D750
4 changed files with 68 additions and 47 deletions

View File

@ -10,6 +10,7 @@ pub mod bash;
pub mod fish;
pub mod xonsh;
pub mod zsh;
pub mod nu;
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Alias {

View File

@ -0,0 +1,7 @@
pub async fn var_config() -> String {
// Because nushell won't autoupdate, we just parse the output of `atuin dotfiles var list` in
// nushell and load the env vars that way
// we should only do this if the dotfiles are enabled
String::from(r#"atuin dotfiles var list | lines | parse "export {name}={value}" | reduce -f {} {|it, acc| $acc | upsert $it.name $it.value} | load-env"#)
}

View File

@ -7,6 +7,7 @@ use eyre::{Result, WrapErr};
mod bash;
mod fish;
mod nu;
mod xonsh;
mod zsh;
@ -38,51 +39,6 @@ pub enum Shell {
}
impl Cmd {
fn init_nu(&self) {
let full = include_str!("../../shell/atuin.nu");
println!("{full}");
if std::env::var("ATUIN_NOBIND").is_err() {
const BIND_CTRL_R: &str = r"$env.config = (
$env.config | upsert keybindings (
$env.config.keybindings
| append {
name: atuin
modifier: control
keycode: char_r
mode: [emacs, vi_normal, vi_insert]
event: { send: executehostcommand cmd: (_atuin_search_cmd) }
}
)
)";
const BIND_UP_ARROW: &str = r"
$env.config = (
$env.config | upsert keybindings (
$env.config.keybindings
| append {
name: atuin
modifier: none
keycode: up
mode: [emacs, vi_normal, vi_insert]
event: {
until: [
{send: menuup}
{send: executehostcommand cmd: (_atuin_search_cmd '--shell-up-key-binding') }
]
}
}
)
)
";
if !self.disable_ctrl_r {
println!("{BIND_CTRL_R}");
}
if !self.disable_up_arrow {
println!("{BIND_UP_ARROW}");
}
}
}
fn static_init(&self) {
match self.shell {
Shell::Zsh => {
@ -95,7 +51,7 @@ $env.config = (
fish::init_static(self.disable_up_arrow, self.disable_ctrl_r);
}
Shell::Nu => {
self.init_nu();
nu::init_static(self.disable_up_arrow, self.disable_ctrl_r);
}
Shell::Xonsh => {
xonsh::init_static(self.disable_up_arrow, self.disable_ctrl_r);
@ -143,7 +99,9 @@ $env.config = (
)
.await?;
}
Shell::Nu => self.init_nu(),
Shell::Nu => {
nu::init(self.disable_up_arrow, self.disable_ctrl_r).await?;
}
Shell::Xonsh => {
xonsh::init(
alias_store,

View File

@ -0,0 +1,55 @@
use eyre::Result;
const BIND_CTRL_R: &str = r"$env.config = (
$env.config | upsert keybindings (
$env.config.keybindings
| append {
name: atuin
modifier: control
keycode: char_r
mode: [emacs, vi_normal, vi_insert]
event: { send: executehostcommand cmd: (_atuin_search_cmd) }
}
)
)";
const BIND_UP_ARROW: &str = r"
$env.config = (
$env.config | upsert keybindings (
$env.config.keybindings
| append {
name: atuin
modifier: none
keycode: up
mode: [emacs, vi_normal, vi_insert]
event: {
until: [
{send: menuup}
{send: executehostcommand cmd: (_atuin_search_cmd '--shell-up-key-binding') }
]
}
}
)
)
";
pub fn init_static(disable_up_arrow: bool, disable_ctrl_r: bool) {
let full = include_str!("../../../shell/atuin.nu");
println!("{full}");
if !disable_ctrl_r && std::env::var("ATUIN_NOBIND").is_err() {
println!("{BIND_CTRL_R}");
}
if !disable_up_arrow && std::env::var("ATUIN_NOBIND").is_err() {
println!("{BIND_UP_ARROW}");
}
}
pub async fn init(disable_up_arrow: bool, disable_ctrl_r: bool) -> Result<()> {
init_static(disable_up_arrow, disable_ctrl_r);
let vars = atuin_dotfiles::shell::nu::var_config().await;
println!("{vars}");
Ok(())
}