diff --git a/dot_config/fish/conf.d/you_should_use.fish b/dot_config/fish/conf.d/you_should_use.fish new file mode 100644 index 0000000..6053b01 --- /dev/null +++ b/dot_config/fish/conf.d/you_should_use.fish @@ -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 diff --git a/dot_config/fish/functions/__ysu__check_aliases.fish b/dot_config/fish/functions/__ysu__check_aliases.fish new file mode 100644 index 0000000..45432fa --- /dev/null +++ b/dot_config/fish/functions/__ysu__check_aliases.fish @@ -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 diff --git a/dot_config/fish/functions/__ysu__check_git_aliases.fish b/dot_config/fish/functions/__ysu__check_git_aliases.fish new file mode 100644 index 0000000..6fed729 --- /dev/null +++ b/dot_config/fish/functions/__ysu__check_git_aliases.fish @@ -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 diff --git a/dot_config/fish/functions/__ysu__check_hardcore.fish b/dot_config/fish/functions/__ysu__check_hardcore.fish new file mode 100644 index 0000000..1f11de8 --- /dev/null +++ b/dot_config/fish/functions/__ysu__check_hardcore.fish @@ -0,0 +1,6 @@ +function __ysu__check_hardcore + if "$YSU__HARDCORE_MODE" + __ysu__write_buffer \ + "$BOLD$RED​You Should Use hardcore mode enabled. Use your aliases!" + end +end diff --git a/dot_config/fish/functions/__ysu__flush_buffer.fish b/dot_config/fish/functions/__ysu__flush_buffer.fish new file mode 100644 index 0000000..a6828db --- /dev/null +++ b/dot_config/fish/functions/__ysu__flush_buffer.fish @@ -0,0 +1,8 @@ +function __ysu__flush_buffer \ + --on-event fish_prompt + if test -n "$__BUFFER" + printf "$__BUFFER" + end + + set __BUFFER "" +end diff --git a/dot_config/fish/functions/__ysu__get_field.fish b/dot_config/fish/functions/__ysu__get_field.fish new file mode 100644 index 0000000..daeca0c --- /dev/null +++ b/dot_config/fish/functions/__ysu__get_field.fish @@ -0,0 +1,4 @@ +function __ysu__get_field \ + --argument-names dict key + eval echo \$$dict'__'$key +end diff --git a/dot_config/fish/functions/__ysu__message.fish b/dot_config/fish/functions/__ysu__message.fish new file mode 100644 index 0000000..aa5e820 --- /dev/null +++ b/dot_config/fish/functions/__ysu__message.fish @@ -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$YELLOW​Found existing $alias_type for \"$VIOLET$command$YELLOW\". +You should use: \"$alias\"\n" +end diff --git a/dot_config/fish/functions/__ysu__set_default.fish b/dot_config/fish/functions/__ysu__set_default.fish new file mode 100644 index 0000000..7de37bf --- /dev/null +++ b/dot_config/fish/functions/__ysu__set_default.fish @@ -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 diff --git a/dot_config/fish/functions/__ysu__set_field.fish b/dot_config/fish/functions/__ysu__set_field.fish new file mode 100644 index 0000000..9df9fb1 --- /dev/null +++ b/dot_config/fish/functions/__ysu__set_field.fish @@ -0,0 +1,4 @@ +function __ysu__set_field \ + --argument-names dict key value + set --global $dict'__'$key $value +end diff --git a/dot_config/fish/functions/__ysu__write_buffer.fish b/dot_config/fish/functions/__ysu__write_buffer.fish new file mode 100644 index 0000000..c2ccc0e --- /dev/null +++ b/dot_config/fish/functions/__ysu__write_buffer.fish @@ -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$BOLD​Unknown value for MESSAGE_POSITION $YSU__MESSAGE_POSITION +Expected value 'before' or 'after'" + __ysu__flush_buffer + end +end