Add .config/fish/conf.d/you_should_use.fish

Add .config/fish/functions/__ysu__check_aliases.fish
Add .config/fish/functions/__ysu__check_git_aliases.fish
Add .config/fish/functions/__ysu__check_hardcore.fish
Add .config/fish/functions/__ysu__flush_buffer.fish
Add .config/fish/functions/__ysu__get_field.fish
Add .config/fish/functions/__ysu__message.fish
Add .config/fish/functions/__ysu__set_default.fish
Add .config/fish/functions/__ysu__set_field.fish
Add .config/fish/functions/__ysu__write_buffer.fish
This commit is contained in:
punkfairie 2024-04-20 11:11:33 -07:00
parent a1bcb57fe3
commit 60cb5740d7
Signed by: punkfairie
GPG key ID: A86AF57F837E320F
10 changed files with 115 additions and 0 deletions

View file

@ -0,0 +1,14 @@
set --global FISH_YOU_SHOULD_USE_VERSION "1.2.0"
__ysu__set_default NONE (set_color normal)
__ysu__set_default BOLD (set_color -o normal)
__ysu__set_default RED (set_color red)
__ysu__set_default YELLOW (set_color yellow)
__ysu__set_default VIOLET (set_color magenta)
set --global YSU__MESSAGE_POSITION "after"
set --global YSU__HARDCORE_MODE false
functions -q __ysu__check_aliases
functions -q __ysu__check_git_aliases
functions -q __ysu__flush_buffer

View file

@ -0,0 +1,24 @@
function __ysu__check_aliases \
--on-event fish_preexec
string match --quiet "sudo *" "$argv"; and return
set --local found false
alias | sort | while read entry
set entry (string replace "=" "" "$entry")
set --local tokens (string split --max 2 " " "$entry")
set --local key "$tokens[2]"
set --local escaped_key (string escape --no-quote --style=regex "$key")
string match --quiet --regex "$escaped_key" "$YSU__IGNORED_GLOBAL_ALIASES"; and continue
set --local value (string replace --regex '(?:[\"|\']([^,]*)[\"|\'])' '$1' "$tokens[3]")
set --local escaped_value (string escape --no-quote --style=regex "$value")
if string match --quiet --regex "(?<=^|\s)$escaped_value(?=\s|\$)" "$argv"
__ysu__message "alias" "$value" "$key"
set found true
end
end
"$found"; and __ysu__check_hardcore
end

View file

@ -0,0 +1,23 @@
function __ysu__check_git_aliases \
--on-event fish_preexec
string match --quiet "sudo *" "$argv"; and return
if string match --quiet --regex "git *" "$argv"
set --local found false
git config --get-regexp "^alias\..+\$" | sort | while read key value
set key (string split --max 2 "." "$key")[2]
set --local escaped_key (string escape --no-quote --style=regex "$key")
string match --quiet --regex "$escaped_key" "$YSU__IGNORED_GIT_ALIASES"; and continue
set --local escaped_value (string escape --no-quote --style=regex "$value")
if string match --quiet --regex "git $escaped_value(?=\s|\$)" "$argv"
__ysu__message "git alias" "$value" "git $key"
set found true
end
end
"$found"; and __ysu__check_hardcore
end
end

View file

@ -0,0 +1,6 @@
function __ysu__check_hardcore
if "$YSU__HARDCORE_MODE"
__ysu__write_buffer \
"$BOLD$REDYou Should Use hardcore mode enabled. Use your aliases!"
end
end

View file

@ -0,0 +1,8 @@
function __ysu__flush_buffer \
--on-event fish_prompt
if test -n "$__BUFFER"
printf "$__BUFFER"
end
set __BUFFER ""
end

View file

@ -0,0 +1,4 @@
function __ysu__get_field \
--argument-names dict key
eval echo \$$dict'__'$key
end

View file

@ -0,0 +1,7 @@
function __ysu__message \
--argument-names alias_type command alias
# zero-width space delinates the end of a variable and the beginning of the message
__ysu__write_buffer "\
$BOLD$YELLOWFound existing $alias_type for \"$VIOLET$command$YELLOW\".
You should use: \"$alias\"\n"
end

View file

@ -0,0 +1,7 @@
function __ysu__set_default \
--no-scope-shadowing \
--argument-names var default
if not set -q "$var"
set --global "$var" "$default"
end
end

View file

@ -0,0 +1,4 @@
function __ysu__set_field \
--argument-names dict key value
set --global $dict'__'$key $value
end

View file

@ -0,0 +1,18 @@
function __ysu__write_buffer \
--argument-names contents
set --global __BUFFER "$contents$__BUFFER\n"
set --local position "before"
if set -q YSU__MESSAGE_POSITION
set position "$YSU__MESSAGE_POSITION"
end
if [ "$position" = "before" ]
__ysu__flush_buffer
else if [ "$position" != "after" ]
echo "\
$RED$BOLDUnknown value for MESSAGE_POSITION $YSU__MESSAGE_POSITION
Expected value 'before' or 'after'"
__ysu__flush_buffer
end
end