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 env syncing support for nushell #1985

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions crates/atuin-dotfiles/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::store::AliasStore;

pub mod bash;
pub mod fish;
pub mod nu;
pub mod xonsh;
pub mod zsh;

Expand Down
8 changes: 8 additions & 0 deletions crates/atuin-dotfiles/src/shell/nu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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

String::from(
r#"atuin dotfiles var list | lines | parse "export {name}={value}" | reduce -f {} {|it, acc| $acc | upsert $it.name $it.value} | load-env"#,
Copy link
Member

Choose a reason for hiding this comment

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

Does nushell have the same shellvar/env var difference?

atuin dotfiles var list will output

foo=bar

for a shellvar, and

export foo=var

for an env var

We'd probably want to handle both cases

Copy link
Contributor Author

@YummyOreo YummyOreo Apr 29, 2024

Choose a reason for hiding this comment

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

The only way I've found to do local variables would be to update the plugin file after it runs and waits till the user opens a new shell. That would fix the updating problem and alias problems.

I can start work on this if it is desired.

Copy link
Member

Choose a reason for hiding this comment

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

I think that makes the most sense, in lieu of being able to load anything dynamically!

)
}
52 changes: 5 additions & 47 deletions crates/atuin/src/command/client/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use eyre::{Result, WrapErr};

mod bash;
mod fish;
mod nu;
mod xonsh;
mod zsh;

Expand Down Expand Up @@ -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 => {
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
55 changes: 55 additions & 0 deletions crates/atuin/src/command/client/init/nu.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
Loading