2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-02 09:40:27 +00:00

set default key bindings for fzf

This commit is contained in:
kevin zhuang 2020-04-07 16:51:07 +10:00
parent aaf1d9c675
commit ad454671cf
4 changed files with 45 additions and 14 deletions

View File

@ -4,15 +4,14 @@
#
# @params
# Globals
# ${mydir}: string, directory of the executing script, used for sourcing helpers
# ${action_command}: string, determine which script to call
# Arguments
# command: command to run general git command/fadd/frm/fpop etc
# $opt: options flag -h -p etc
mydir="${0%/*}"
[[ -z "${DOTBARE_DIR}" ]] && DOTBARE_DIR="$HOME/.cfg/"
[[ -z "${DOTBARE_TREE}" ]] && DOTBARE_TREE="$HOME"
source "${mydir}"/helper/set_variable
function usage() {
echo "help"

View File

@ -11,12 +11,12 @@
# search_type: string, f or d, search file or directory
function fd_exists() {
wtf -V &>/dev/null
fd -V &>/dev/null
echo "$?"
}
function tree_exists() {
wtf -V &>/dev/null
tree -version &>/dev/null
echo "$?"
}
@ -25,18 +25,18 @@ function search_file() {
local exe_fd="$(fd_exists)"
if [[ "${search_type}" == "f" ]]; then
if [[ "${exe_fd}" -eq 0 ]]; then
fd -H -d 1 -t f | fzf --multi --exit-0 --preview "head -50 {}"
fd -H -d 1 -t f | fzf --multi --preview "head -50 {}"
else
find . -maxdepth 1 -type f | sed "s|\./||g" | fzf --multi --exit-0 --preview "head -50 {}"
find . -maxdepth 1 -type f | sed "s|\./||g" | fzf --multi --preview "head -50 {}"
fi
elif [[ "${search_type}" == "d" ]]; then
exe_tree="$(tree_exists)"
if [[ "${exe_fd}" -eq 0 && "${exe_tree}" -eq 0 ]]; then
fd -H -d 1 -t d -E .git | fzf --multi --exit-0 --preview "tree -L 1 -C --dirsfirst {}"
fd -H -d 1 -t d -E .git | fzf --multi --preview "tree -L 1 -C --dirsfirst {}"
elif [[ "${exe_tree}" -eq 0 ]]; then
find . -maxdepth 1 -type d | awk '{if ($0 != "." && $0 != "./.git"){print $0}}' | sed "s|\./||g" | fzf --multi --exit-0 --preview "tree -L 1 -C --dirsfirst {}"
find . -maxdepth 1 -type d | awk '{if ($0 != "." && $0 != "./.git"){print $0}}' | sed "s|\./||g" | fzf --multi --preview "tree -L 1 -C --dirsfirst {}"
else
find . -maxdepth 1 -type d | awk '{if ($0 != "." && $0 != "./.git"){print $0}}' | sed "s|\./||g" | fzf --multi --exit-0
find . -maxdepth 1 -type d | awk '{if ($0 != "." && $0 != "./.git"){print $0}}' | sed "s|\./||g" | fzf --multi
fi
fi
}

33
helper/set_variable Normal file
View File

@ -0,0 +1,33 @@
#!/bin/bash
#
# check env variables and set default variables
#
# @params
# Globals
# DOTBARE_DIR: string, location of the bare repo
# DOTBARE_TREE: string, which folder is the bare repo tracking
# DOTBARE_KEY: string, defualt key bindings
# FZF_DEFAULT_OPTS: string, update FZF_DEFAULT_OPTS to reflect dotbare changes
# DOTBARE_FZF_DEFAULT_OPTS: string, user custom setting for dotbare
[[ -z "${DOTBARE_DIR}" ]] && DOTBARE_DIR="$HOME/.cfg/"
[[ -z "${DOTBARE_TREE}" ]] && DOTBARE_TREE="$HOME"
if [[ -z "${DOTBARE_KEY}" ]]; then
DOTBARE_KEY="
--bind=alt-a:toggle-all
--bind=alt-w:jump
--bind=alt-0:top
--bind=alt-o:clear-query
--bind=alt-s:toggle-sort
--bind=alt-t:toggle-preview
"
fi
FZF_DEFAULT_OPTS="
$FZF_DEFAULT_OPTS
--ansi
--cycle
--exit-0
$DOTBARE_FZF_DEFAULT_OPTS
$DOTBARE_KEY
"

View File

@ -4,15 +4,14 @@
#
# @params
# Globals
# ${mydir}: string, current directory of the executing script
# ${new_file}: new file path to stage
# ${new_folder}: new folder to stage
# ${stage_file}: changed file to stage
mydir="${0%/*}"
source "${mydir}"/../helper/search_file
[[ -z "${DOTBARE_DIR}" ]] && DOTBARE_DIR="$HOME/.cfg/"
[[ -z "${DOTBARE_TREE}" ]] && DOTBARE_TREE="$HOME"
source "${mydir}"/../helper/set_variable
function usage() {
echo -e "Usage: dotbare fadd [-h] [-f] [-d] ...\n"
@ -74,7 +73,7 @@ elif [[ -n "${new_folder}" ]]; then
else
selected_files=$(/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" diff --name-status | \
awk '{if ($1 == "D") {print "\033[31m" $1 " " $2} else {print "\033[33m" $1 " " $2}}' | \
fzf --ansi --multi --exit-0 --preview "echo {} | awk '{print \$2}' | \
fzf --multi --preview "echo {} | awk '{print \$2}' | \
xargs -I __ /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" diff --color=always $HOME/__" | \
awk -v home=$HOME '{print home "/" $2}')
while IFS= read -r line; do