feat(zsh): switched back to zsh and histdb

This commit is contained in:
Patrick 2023-09-18 00:00:08 +02:00
parent 57ed20dcd6
commit 054407efb4
Signed by: patrick
GPG key ID: 451F95EFB8BECD0F
6 changed files with 76 additions and 102 deletions

View file

@ -103,8 +103,8 @@ in {
"${modifier}+f" = "floating toggle";
"${modifier}+space" = "focus mode_toggle";
"${modifier}+Comma" = "workspace prev";
"${modifier}+Period" = "workspace next";
"${modifier}+comma" = "workspace prev_on_output";
"${modifier}+period" = "workspace next_on_output";
"${modifier}+1" = "workspace number 1";
"${modifier}+2" = "workspace number 2";

View file

@ -19,9 +19,6 @@
optionals config.programs.firefox.enable [
".mozilla"
]
++ optionals config.programs.atuin.enable [
".local/share/atuin"
]
++ optionals config.programs.direnv.enable [
".local/share/direnv"
]

View file

@ -14,6 +14,7 @@
init.defaultBranch = "main";
pull.ff = "only";
pull.rebase = true;
push.autoSetupRemote = true;
};
signing = {
key = null;

View file

@ -1,4 +1,7 @@
{
# nu does not allow language access to completions
# which makes it impossible to use custom completion menus
# ZSH is still unbeatable with their completions
programs.atuin.enableNushellIntegration = false;
programs.nushell = {
enable = true;

View file

@ -1,55 +1,83 @@
{
pkgs,
lib,
config,
...
}: {
imports = [
../starfish.nix
];
# for zsh-histdb
# TODO replace sqlite inplace with nix path
home.packages = [pkgs.sqlite];
# save history in xdg data home
home.sessionVariables.HISTDB_FILE = "${config.xdg.dataHome}/zsh/history.db";
# has to be enabled to support zsh reverse search
programs.fzf.enable = true;
programs.atuin = {
enable = true;
settings.auto_sync = false;
};
programs.carapace.enable = true;
programs.command-not-found.enable = true;
programs.zsh = {
enable = true;
dotDir = ".config/zsh";
# Atuin makes completion and this really _really_ slows zsh startup time down
enableCompletion = false;
initExtra = lib.mkAfter (''
function atuin-prefix-search() {
if out=$(${pkgs.sqlite}/bin/sqlite3 -readonly ~/.local/share/atuin/history.db \
'SELECT command FROM history WHERE command LIKE cast('"x'$(str_to_hex "$_atuin_search_prefix")'"' as text) ||
"%" ORDER BY timestamp DESC LIMIT 1 OFFSET '"$_atuin_search_offset"); then
[[ -z "$out" ]] && return 1
BUFFER=$out
else
return 1
fi
}; zle -N atuin-prefix-search
''
+ (builtins.readFile ./zshrc));
history = {
extended = true;
path = "${config.xdg.dataHome}/zsh/zsh_history";
save = 1000000;
share = false;
};
initExtra = builtins.readFile ./zshrc;
plugins = [
{
name = "fzf-tab";
src = pkgs.fetchFromGitHub {
owner = "aloxaf";
repo = "fzf-tab";
rev = "5a81e13792a1eed4a03d2083771ee6e5b616b9ab";
sha256 = "0lfl4r44ci0wflfzlzzxncrb3frnwzghll8p365ypfl0n04bkxvl";
};
src = "${pkgs.zsh-fzf-tab}/share/fzf-tab";
}
{
name = "fast-syntax-highlighting";
src = "${pkgs.zsh-fast-syntax-highlighting}/share/zsh/site-functions";
}
{
# TODO change to separate packages
name = "zsh-histdb";
src = pkgs.stdenv.mkDerivation {
name = "zsh-histdb";
src = pkgs.fetchFromGitHub {
owner = "zdharma-continuum";
repo = "fast-syntax-highlighting";
rev = "13d7b4e63468307b6dcb2dadf6150818f242cbff";
sha256 = "0ghzqg1xfvqh9z23aga7aafrpxbp9bpy1r8vk4avi6b80p3iwsq2";
owner = "larkery";
repo = "zsh-histdb";
rev = "30797f0c50c31c8d8de32386970c5d480e5ab35d";
hash = "sha256-PQIFF8kz+baqmZWiSr+wc4EleZ/KD8Y+lxW2NT35/bg=";
};
patchPhase = ''
substituteInPlace "sqlite-history.zsh" "histdb-migrate" "histdb-merge" \
--replace "sqlite3" "${pkgs.sqlite}/bin/sqlite3"
'';
installPhase = ''
mkdir -p $out
cp -r * $out
'';
};
}
{
name = "zsh-histdb-skim";
src = pkgs.rustPlatform.buildRustPackage rec {
pname = "zsh-histd-skim";
version = "0.8.6";
buildInputs = [pkgs.sqlite];
src = pkgs.fetchFromGitHub {
owner = "m42e";
repo = "zsh-histdb-skim";
rev = "v${version}";
hash = "sha256-lJ2kpIXPHE8qP0EBnLuyvatWMtepBobNAC09e7itGas=";
};
cargoHash = "sha256-BMy9Shy9KAx5+VbvH2WaA0wMFUNM5dqU/dssUNE1NWY=";
postInstall = ''
substituteInPlace zsh-histdb-skim-vendored.zsh \
--replace "zsh-histdb-skim" "$out/bin/zsh-histdb-skim"
cp zsh-histdb-skim-vendored.zsh $out/zsh-histdb-skim.plugin.zsh
'';
};
}
];

View file

@ -1,54 +1,3 @@
function str_to_hex() {
local str="$1"
for (( i=0; i<${#str}; i++ )); do
printf "%02x" "'${str:$i:1}"
done
}
function atuin-prefix-search-any-end() {
if [[ -v _atuin_search_prev_buffer ]] && [[ $_atuin_search_prev_buffer == $BUFFER ]]; then
_atuin_search_offset=$((_atuin_search_offset + $direction))
else
[[ "$direction" == -1 ]] && return 1
_atuin_search_offset=0
_atuin_search_prefix=$LBUFFER
_atuin_search_orig_buffer=$BUFFER
_atuin_search_orig_cursor=$CURSOR
fi
if [[ "$_atuin_search_offset" -ge 0 ]] && zle atuin-prefix-search; then
zle .end-of-line
_atuin_search_prev_buffer=$BUFFER
else
if [[ $_atuin_search_offset -lt 0 ]];then
_atuin_search_offset=0
BUFFER=$_atuin_search_orig_buffer
CURSOR=$_atuin_search_orig_cursor
else
_atuin_search_offset=$((_atuin_search_offset - 1))
fi
return 1
fi
zle reset-prompt
}; zle -N atuin-prefix-search-any-end
function atuin-reset-search() {
unset _atuin_search_prev_buffer
}
autoload -U add-zle-hook-widget
add-zle-hook-widget line-init atuin-reset-search
function atuin-prefix-search-backward-end() {
direction=1 atuin-prefix-search-any-end
}; zle -N atuin-prefix-search-backward-end
function atuin-prefix-search-forward-end() {
direction=-1 atuin-prefix-search-any-end
}; zle -N atuin-prefix-search-forward-end
# Don't insert tabs when there is no completion (e.g. beginning of line)
zstyle ':completion:*' insert-tab false
@ -102,9 +51,6 @@ zstyle ':fzf-tab:complete:cd:*' fzf-preview 'ls -lhF --group-directories-first -
# preview windown min size
zstyle ':fzf-tab:complete:cd:*' popup_pad 20 0
# History settings
unset HISTFILE
# Emit an error when a glob has no match
setopt nomatch
# DisAllow extended globbing
@ -152,6 +98,11 @@ function zshaddhistory() {
[[ ! $1 =~ "$HISTORY_IGNORE_REGEX" ]]
}
if autoload history-search-end; then
zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end
fi
# Delete all keybinds and use emacs-like key bindings by default:
bindkey -d
bindkey -e
@ -210,6 +161,8 @@ function setup_keybinds() {
local keys_PageDown=( "${terminfo[knp]}" )
local keys_ShiftPageDown=( "${terminfo[kNXT]}" )
local keys_CtrlR=( "^R" )
bindkeys keys_Home beginning-of-line
bindkeys keys_End end-of-line
bindkeys keys_Insert nop
@ -225,12 +178,12 @@ function setup_keybinds() {
bindkeys keys_CtrlDelete kill-line
bindkeys keys_AltDelete delete-word
bindkeys keys_Up atuin-prefix-search-backward-end
bindkeys keys_Up history-beginning-search-backward-end
bindkeys keys_ShiftUp up-line
bindkeys keys_CtrlUp nop
bindkeys keys_AltUp nop
bindkeys keys_Down atuin-prefix-search-forward-end
bindkeys keys_Down history-beginning-search-forward-end
bindkeys keys_ShiftDown down-line
bindkeys keys_CtrlDown nop
bindkeys keys_AltDown nop
@ -251,15 +204,7 @@ function setup_keybinds() {
bindkeys keys_PageDown nop
bindkeys keys_ShiftPageDown nop
# atuin
bindkey '^r' _atuin_search_widget
# fzf file and directory related expansions and functions
bindkey '\ef' fzf-select-file-or-dir
bindkey '\eF' fzf-select-file-or-dir-hidden
bindkey '\ed' fzf-select-dir
bindkey '\eD' fzf-select-dir-hidden
bindkey '\ec' fzf-cd
bindkeys keys_CtrlR histdb-skim-widget
}
setup_keybinds
unfunction bindkeys