Migrate to chezmoi

Move config files from config to chezmoi
Add script to auto install packages with DNF and Cargo
This commit is contained in:
Alexander Navarro 2024-03-01 20:25:09 -03:00
parent 110e0882c6
commit 224c7ed45c
1654 changed files with 470035 additions and 51 deletions

View file

@ -0,0 +1,315 @@
#!/usr/bin/env bash
platform="$(uname)"
# first check the version of bash
if ! type mapfile &> /dev/null; then
echo "error: extrakto needs a newer Bash"
if [[ $platform == Darwin ]]; then
echo "On macOS you need to install/update it with Homebrew."
fi
read # pause
exit 1
fi
PRJ_URL=https://github.com/laktak/extrakto
current_dir="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
trigger_pane=$1
launch_mode=$2
source "$current_dir/helpers.sh"
extrakto="$current_dir/../extrakto.py"
declare -Ar COLORS=(
[RED]=$'\033[0;31m'
[GREEN]=$'\033[0;32m'
[BLUE]=$'\033[0;34m'
[PURPLE]=$'\033[0;35m'
[CYAN]=$'\033[0;36m'
[WHITE]=$'\033[0;37m'
[YELLOW]=$'\033[0;33m'
[OFF]=$'\033[0m'
[BOLD]=$'\033[1m'
)
# options; note some of the values can be overwritten by capture()
grab_area=$(get_option "@extrakto_grab_area")
clip_tool=$(get_option "@extrakto_clip_tool")
clip_tool_run=$(get_option "@extrakto_clip_tool_run")
editor=$(get_option "@extrakto_editor")
fzf_tool=$(get_option "@extrakto_fzf_tool")
open_tool=$(get_option "@extrakto_open_tool")
copy_key=$(get_option "@extrakto_copy_key")
insert_key=$(get_option "@extrakto_insert_key")
filter_key=$(get_option "@extrakto_filter_key")
open_key=$(get_option "@extrakto_open_key")
edit_key=$(get_option "@extrakto_edit_key")
grab_key=$(get_option "@extrakto_grab_key")
help_key=$(get_option "@extrakto_help_key")
fzf_layout=$(get_option "@extrakto_fzf_layout")
fzf_unset_default_opts=$(get_option "@extrakto_fzf_unset_default_opts")
capture_pane_start=$(get_capture_pane_start "$grab_area")
original_grab_area=${grab_area} # keep this so we can cycle between alternatives on fzf
# avoid side effects from FZF_DEFAULT_OPTS
if [[ $fzf_unset_default_opts == "true" ]]; then
unset FZF_DEFAULT_OPTS
fi
if [[ "$clip_tool" == "auto" ]]; then
case "$platform" in
'Linux')
if [[ $(cat /proc/sys/kernel/osrelease) =~ Microsoft|microsoft ]]; then
clip_tool='clip.exe'
elif [[ $XDG_SESSION_TYPE == "wayland" ]]; then
clip_tool='wl-copy'
else
clip_tool='xclip -i -selection clipboard >/dev/null'
fi
;;
'Darwin') clip_tool='pbcopy' ;;
*) ;;
esac
fi
if [[ "$open_tool" == "auto" ]]; then
case "$platform" in
'Linux') open_tool='xdg-open >/dev/null' ;;
'Darwin') open_tool='open' ;;
*) open_tool='' ;;
esac
fi
if [[ -z $editor ]]; then
editor="${EDITOR:-vi}"
fi
copy() {
if [[ "$clip_tool_run" == "fg" ]]; then
# run in foreground as OSC-52 copying won't work otherwise
tmux set-buffer -- "$1"
tmux run-shell "tmux show-buffer|$clip_tool"
elif [[ "$clip_tool_run" == "tmux_osc52" ]]; then
# use native tmux 3.2 OSC 52 functionality
tmux set-buffer -w -- "$1"
else
# run in background as xclip won't work otherwise
tmux set-buffer -- "$1"
tmux run-shell -b "tmux show-buffer|$clip_tool"
fi
}
open() {
if [[ -n "$open_tool" ]]; then
tmux run-shell -b "cd -- $PWD; $open_tool $1"
return 0
fi
}
capture_panes() {
local pane captured
captured=""
if [[ $grab_area =~ ^window\ ]]; then
for pane in $(tmux list-panes -F "#{pane_active}:#{pane_id}"); do
# exclude the active (for split) and trigger panes
# in popup mode the active and tigger panes are the same
if [[ $pane =~ ^0: && ${pane:2} != "$trigger_pane" ]]; then
captured+="$(tmux capture-pane -pJS ${capture_pane_start} -t ${pane:2})"
captured+=$'\n'
fi
done
fi
captured+="$(tmux capture-pane -pJS ${capture_pane_start} -t $trigger_pane)"
echo "$captured"
}
has_single_pane() {
local num_panes
num_panes=$(tmux list-panes | wc -l)
if [[ $launch_mode == popup ]]; then
[[ $num_panes == 1 ]]
else
[[ $num_panes == 2 ]]
fi
}
show_fzf_error() {
echo "error: unable to extract - check/report errors above"
echo "You can also set the fzf path in options (see readme)."
read # pause
}
capture() {
local mode header_tmpl header out res key text query
mode=$(get_next_mode "initial")
header_tmpl="${COLORS[BOLD]}${insert_key}${COLORS[OFF]}=insert"
header_tmpl+=", ${COLORS[BOLD]}${copy_key}${COLORS[OFF]}=copy"
[[ -n "$open_tool" ]] && header_tmpl+=", ${COLORS[BOLD]}${open_key}${COLORS[OFF]}=open"
header_tmpl+=", ${COLORS[BOLD]}${edit_key}${COLORS[OFF]}=edit"
header_tmpl+=", ${COLORS[BOLD]}${filter_key}${COLORS[OFF]}=filter [${COLORS[YELLOW]}${COLORS[BOLD]}:filter:${COLORS[OFF]}]"
header_tmpl+=", ${COLORS[BOLD]}${grab_key}${COLORS[OFF]}=grab [${COLORS[YELLOW]}${COLORS[BOLD]}:ga:${COLORS[OFF]}]"
header_tmpl+=", ${COLORS[BOLD]}${help_key}${COLORS[OFF]}=help"
get_cap() {
case "$mode" in
"all")
capture_panes | $extrakto --warn-empty --alt --all --name -r
;;
"line")
capture_panes | $extrakto --warn-empty -r --lines
;;
"path")
capture_panes | $extrakto --warn-empty -r --paths
;;
"url")
capture_panes | $extrakto --warn-empty -r --urls
;;
"word")
capture_panes | $extrakto --warn-empty -r --words
;;
*)
# custom filters
capture_panes | $extrakto --warn-empty -ra $mode
;;
esac
}
while true; do
header=$header_tmpl
header=${header/:ga:/$grab_area}
header=${header/:filter:/$mode}
header=${header//ctrl-/^}
# for troubleshooting add
# tee /tmp/stageN | \
# between the commands
out="$(get_cap \
| $fzf_tool \
--multi \
--print-query \
--query="$query" \
--header="$header" \
--expect=${insert_key},${copy_key},${filter_key},${edit_key},${open_key},${grab_key},${help_key},ctrl-c,esc \
--tiebreak=index \
--layout="$fzf_layout" \
--no-info)"
res=$?
{
read query
read key
mapfile -t selection
} <<< "$out"
if [[ $res -gt 0 && -z "$key" ]]; then
show_fzf_error
exit 1
fi
case "$mode" in
all)
text="${selection[@]#*: }"
;;
line)
IFS=$'\n' text="${selection[*]}"
;;
*)
text="${selection[@]}"
;;
esac
case "$key" in
"${copy_key}")
copy "$text"
return 0
;;
"${insert_key}")
tmux set-buffer -- "$text"
tmux paste-buffer -p -t $trigger_pane
return 0
;;
"${filter_key}")
mode=$(get_next_mode $mode)
;;
"${grab_key}")
# cycle between options like this:
# recent -> full -> window recent -> window full -> custom (if any) -> recent ...
if [[ $grab_area == "recent" ]]; then
if has_single_pane; then
grab_area="full"
else
grab_area="window recent"
fi
elif [[ $grab_area == "window recent" ]]; then
grab_area="full"
elif [[ $grab_area == "full" ]]; then
if has_single_pane; then
grab_area="recent"
if [[ ! "$original_grab_area" =~ ^(window )?(recent|full)$ ]]; then
grab_area="$original_grab_area"
fi
else
grab_area="window full"
fi
elif [[ $grab_area == "window full" ]]; then
grab_area="recent"
if [[ ! "$original_grab_area" =~ ^(window )?(recent|full)$ ]]; then
grab_area="$original_grab_area"
fi
else
grab_area="recent"
fi
capture_pane_start=$(get_capture_pane_start "$grab_area")
;;
"${open_key}")
open "$text"
return 0
;;
"${edit_key}")
tmux send-keys -t $trigger_pane "$editor -- $text" 'C-m'
return 0
;;
"${help_key}")
clear
less -+EF $(realpath "$current_dir/../HELP.md")
echo -e "\nSince the help page is not 'extrakt'-able:"
read -p "Do you wish to [o]pen or [c]opy the GitHub page or [a]bort? [ocA]" -d'' -s -n1 confirm
if [[ $confirm == o ]]; then
open $PRJ_URL
elif [[ $confirm == c ]]; then
copy $PRJ_URL
fi
;;
*)
return 0
;;
esac
done
}
# Entry
if [[ $launch_mode != popup ]]; then
# check terminal size, zoom pane if too small
lines=$(tput lines)
if [[ $lines -lt 7 ]]; then
tmux resize-pane -Z
fi
fi
capture

View file

@ -0,0 +1,36 @@
#!/usr/bin/env bash
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$current_dir/helpers.sh"
extrakto="$current_dir/extrakto.sh"
pane_id=$1
split_direction=$(get_option "@extrakto_split_direction")
if [[ $split_direction == a ]]; then
if [[ -n $(tmux list-commands popup) ]]; then
split_direction=p
else
split_direction=v
fi
fi
if [[ $split_direction == p ]]; then
IFS=, read popup_width popup_height <<< "$(get_option "@extrakto_popup_size")"
IFS=, read popup_x popup_y <<< "$(get_option "@extrakto_popup_position")"
rc=129
while [ $rc -eq 129 ]; do
tmux popup \
-w ${popup_width} \
-h ${popup_height:-$popup_width} \
-x ${popup_x} \
-y ${popup_y:-$popup_x} \
-E "${extrakto} ${pane_id} popup"
rc=$?
done
exit $rc
else
split_size=$(get_option "@extrakto_split_size")
tmux split-window -${split_direction} -l ${split_size} "tmux setw remain-on-exit off; ${extrakto} ${pane_id} split"
fi

View file

@ -0,0 +1,197 @@
#!/bin/bash
get_tmux_option() {
local option default_value option_value
option=$1
default_value=$2
option_value=$(tmux show-option -gqv "$option")
if [[ -z "$option_value" ]]; then
echo "$default_value"
else
echo "$option_value"
fi
}
get_option() {
local option=$1
case "$option" in
"@extrakto_key")
echo $(get_tmux_option $option "tab")
;;
"@extrakto_split_direction")
echo $(get_tmux_option $option "a")
;;
"@extrakto_split_size")
echo $(get_tmux_option $option "7")
;;
"@extrakto_grab_area")
echo $(get_tmux_option $option "window full")
;;
"@extrakto_clip_tool")
echo $(get_tmux_option $option "auto")
;;
"@extrakto_fzf_tool")
echo $(get_tmux_option $option "fzf")
;;
"@extrakto_open_tool")
echo $(get_tmux_option $option "auto")
;;
"@extrakto_copy_key")
echo $(get_tmux_option $option "enter")
;;
"@extrakto_insert_key")
echo $(get_tmux_option $option "tab")
;;
"@extrakto_filter_key")
echo $(get_tmux_option $option "ctrl-f")
;;
"@extrakto_open_key")
echo $(get_tmux_option $option "ctrl-o")
;;
"@extrakto_edit_key")
echo $(get_tmux_option $option "ctrl-e")
;;
"@extrakto_grab_key")
echo $(get_tmux_option $option "ctrl-g")
;;
"@extrakto_help_key")
echo $(get_tmux_option $option "ctrl-h")
;;
"@extrakto_clip_tool_run")
echo $(get_tmux_option $option "bg")
;;
"@extrakto_popup_size")
echo $(get_tmux_option $option "90%")
;;
"@extrakto_popup_position")
echo $(get_tmux_option $option "C")
;;
"@extrakto_fzf_layout")
echo $(get_tmux_option $option "default")
;;
"@extrakto_filter_order")
echo $(get_tmux_option $option "word all line")
;;
"@extrakto_fzf_unset_default_opts")
echo $(get_tmux_option $option "true")
;;
esac
}
# This returns the start point parameter for `tmux capture-pane`.
# The result will depend on how the user has set the grab area and grab size.
get_capture_pane_start() {
local grab_area capture_start history_limit
grab_area="$1"
if [[ "$grab_area" == "recent" || "$grab_area" == "window recent" ]]; then
capture_start="-10"
elif [[ "$grab_area" == "full" || "$grab_area" == "window full" ]]; then
# use the history limit, this is all the data on the pane
# if not set just go with tmux's default
history_limit=$(get_tmux_option "history-limit" "2000")
capture_start="-${history_limit}"
elif [[ "$grab_area" =~ ^window\ ]]; then
# use the user defined limit for how much to grab from every pane in the current window
capture_start="-${grab_area:7}"
else
# use the user defined limit for how much to grab from the current pane
capture_start="-${grab_area}"
fi
echo "$capture_start"
}
# Implement ordered filters list as defined by @extrakto_filter_order
declare -A next_mode
declare -a modes_list
# If there is a bogus filter mode name in the list,
# fall back to the default list of "word all line"
sanitize_modes_list() {
# in case of further "first class" filter modes implemented in the future
# add their names to the following default list and valid_modes set
local -a default=("word" "all" "line")
local -A valid_modes=(["word"]=1
["all"]=1
["line"]=1
["url"]=1
["path"]=1
["quote"]=1
["s-quote"]=1)
local invalid=false
for mode in ${modes_list[@]}; do
if [[ ${valid_modes[$mode]} -ne 1 ]]; then
invalid=true
break
fi
done
if [[ $invalid == true ]]; then
# change $modes_list to $default
for i in ${!default[@]}; do
modes_list[$i]=${default[$i]}
done
fi
}
# transform the modes_list acquired from @extrakto_filter_order into
# the more usable form of associative array
mk_next_mode_map() {
for i in ${!modes_list[@]}; do
if [[ $i -eq $((${#modes_list[@]} - 1)) ]]; then
next_mode+=([${modes_list[$i]}]=${modes_list[0]})
else
next_mode+=([${modes_list[$i]}]=${modes_list[$((i + 1))]})
fi
done
}
# initialize
modes_list_init() {
readarray -td ' ' modes_list <<< "$(get_option @extrakto_filter_order) "
unset 'modes_list[-1]'
sanitize_modes_list
mk_next_mode_map
}
# get next mode in order defined by @extrakto_filter_order
get_next_mode() {
if [[ ${#modes_list[@]} -eq 0 || ${#next_mode[@]} -le ${#modes_list[@]} ]]; then
modes_list_init
fi
local next=$1
if [ $next == "initial" ]; then
echo ${modes_list[0]}
else
echo ${next_mode[$next]}
fi
}