sbstp / kubie

A more powerful alternative to kubectx and kubens
https://blog.sbstp.ca/introducing-kubie/
zlib License
1.97k stars 101 forks source link

Feature Request: Consider support history maintaining to main-shell from sub-shells #81

Open Dentrax opened 3 years ago

Dentrax commented 3 years ago
$ wc -l .zsh_history
   1 .zsh_history

$ tail -6 .zsh_history
: 1626419269:0;echo "Hello, World!"
$ kubie ctx foo
$ kubectl get pods -l app=bar -o wide
$ exit  #  exit sub-shell
$ tail -6 .zsh_history
: 1626419269:0;echo "Hello, World!"
: 1626418505:0;kubie ctx
: 1626419248:0;tail -6 .zsh_history

It would be a great feature if we are able to pass our command history to main shell in order to keep history maintained from sub-shells. Currently, we do not have access to any previous commands after we exit the sub-shell.

Dentrax commented 3 years ago

@sbstp

As a workaround, I thought of a few possible solutions to handle this:

1. Merging the history of current session after exit

By creating a temp history file for the current session, we can easily cover this. We should append every command we write to that temporary file. In the merging stage, we can listen for the zshexit hook in order to append the whole $ZDOTDIR/.current_history to $HOME/.zsh_history. Rebasing by time is needed for the history file after merge operation?

2. Appending each command real-time

Is it make sense to append each command we write to under $HOME/.zsh_history? We can add a precmd hook for this.

3. Making sub-shell optional

Like the kubectx. Why wouldn't we make shell spawning optional? I might simply do not want to work under a new shell.

Dentrax commented 3 years ago

Any thoughts? @sbstp Looking forward to this feature. 😍

sbstp commented 3 years ago

This feels like something that should be implemented outside of kubie. The sub-shell is required to inject a bunch of stuff into .bashrc when we launch the shell.

I'm not sure if real time history merging between multiple terminals is something that people actually want. But you should be able to setup your shell to merge history when it exists using plain old shell settings, like shown here.

danielfoehrKn commented 3 years ago

I think the reason why that happens comes from using a sub-shell in the first place. I recently looked at kubie how it is doing terminal window isolation etc. Using a sub-shell definitely has advantages that I did not consider when starting to implement kubeswitch 1.5 years ago. However, not launching a sub-shell also avoids problems such as this one. The shell history works across terminals + seeing a history of selected contexts (via switch h) works.

Anyways, kubie is a great tool and I guess has trade-offs and advantages like a lot of other software.

Shaked commented 2 years ago

Actually I don't understand why this is so complicated. Even with a sub shell, you can just point HISTFILE to its default value, and then it that case it will work as expected.

The decision of merging/sharing/appending history is controlled by the user https://unix.stackexchange.com/questions/1288/preserve-bash-history-in-multiple-terminal-windows.

Currently it would be great if kubie will offer an option to use the default HISTFILE instead of creating a copy of it, as this creates a bug where ideally merged history just disappears.

I currently bypassed this by:

cp ~/.zsh_history ~/.zsh_history_bypass_kubie
vim ~/.zshrc 
HISTFILE=~/.zsh_history_bypass_kubie
HISTSIZE=500000
SAVEHIST=500000
setopt appendhistory
setopt INC_APPEND_HISTORY  
setopt SHARE_HISTORY
Dentrax commented 2 years ago

Kind ping here. 🤞 @sbstp

Dentrax commented 1 year ago

I'd like to see this feature in kubie. Anyone has a bandwidth to submit a PR for this?

cc @sbstp

Alveel commented 10 months ago

Currently it would be great if kubie will offer an option to use the default HISTFILE instead of creating a copy of it, as this creates a bug where ideally merged history just disappears.

If you explicitly configure HISTFILE, what exactly is the problem? I am not entirely sure if I understand. I'm trying to get this fixed, but I don't see a problem if you have export HISTFILE="$HOME/.zsh_history. Yes, the history file will be copied, but zsh will write to the original file.

Alveel commented 10 months ago

Sorry, cc @Shaked (2 years later 😅)