├── .prettierrc.json ├── functions ├── _sponge_on_exit.fish ├── _sponge_on_prompt.fish ├── _sponge_clear_state.fish ├── sponge_filter_matched.fish ├── _sponge_remove_from_history.fish ├── sponge_filter_failed.fish ├── _sponge_on_preexec.fish └── _sponge_on_postexec.fish ├── LICENSE ├── conf.d └── sponge.fish └── README.md /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "tabWidth": 2 3 | } 4 | -------------------------------------------------------------------------------- /functions/_sponge_on_exit.fish: -------------------------------------------------------------------------------- 1 | function _sponge_on_exit --on-event fish_exit 2 | sponge_delay=0 _sponge_remove_from_history 3 | end 4 | -------------------------------------------------------------------------------- /functions/_sponge_on_prompt.fish: -------------------------------------------------------------------------------- 1 | function _sponge_on_prompt --on-event fish_prompt 2 | if test $sponge_purge_only_on_exit = false 3 | _sponge_remove_from_history 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /functions/_sponge_clear_state.fish: -------------------------------------------------------------------------------- 1 | function _sponge_clear_state 2 | set --erase --global _sponge_current_command 3 | set --erase --global _sponge_current_command_exit_code 4 | set --erase --global _sponge_current_command_previously_in_history 5 | end 6 | -------------------------------------------------------------------------------- /functions/sponge_filter_matched.fish: -------------------------------------------------------------------------------- 1 | function sponge_filter_matched \ 2 | --argument-names command 3 | 4 | for pattern in $sponge_regex_patterns 5 | if string match --regex --quiet $pattern -- $command 6 | return 7 | end 8 | end 9 | 10 | return 1 11 | end 12 | -------------------------------------------------------------------------------- /functions/_sponge_remove_from_history.fish: -------------------------------------------------------------------------------- 1 | function _sponge_remove_from_history 2 | 3 | while test (count $_sponge_queue) -gt $sponge_delay 4 | builtin history delete --case-sensitive --exact -- $_sponge_queue[-1] 5 | set --erase _sponge_queue[-1] 6 | end 7 | 8 | builtin history save 9 | end 10 | -------------------------------------------------------------------------------- /functions/sponge_filter_failed.fish: -------------------------------------------------------------------------------- 1 | function sponge_filter_failed \ 2 | --argument-names command exit_code previously_in_history 3 | 4 | if test $previously_in_history = true -a $sponge_allow_previously_successful = true 5 | return 1 6 | end 7 | 8 | if contains $exit_code $sponge_successful_exit_codes 9 | return 1 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /functions/_sponge_on_preexec.fish: -------------------------------------------------------------------------------- 1 | function _sponge_on_preexec --on-event fish_preexec \ 2 | --argument-names command 3 | _sponge_clear_state 4 | 5 | set --global _sponge_current_command $command 6 | 7 | builtin history search --case-sensitive --exact --max=1 --null $command \ 8 | | read --local --null found_entries 9 | 10 | # If a command is in the history and in the queue, ignore it, like if it wasn’t in the history 11 | if test (count $found_entries) -ne 0; and not contains $command $_sponge_queue 12 | set --global _sponge_current_command_previously_in_history true 13 | else 14 | set --global _sponge_current_command_previously_in_history false 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /functions/_sponge_on_postexec.fish: -------------------------------------------------------------------------------- 1 | function _sponge_on_postexec --on-event fish_postexec 2 | set --global _sponge_current_command_exit_code $status 3 | 4 | # Remove command from the queue if it's been added previously 5 | if set --local index (contains --index -- $_sponge_current_command $_sponge_queue) 6 | set --erase _sponge_queue[$index] 7 | end 8 | 9 | # Ignore empty commands 10 | if test -n $_sponge_current_command 11 | set --local command '' 12 | # Run filters 13 | for filter in $sponge_filters 14 | if $filter \ 15 | $_sponge_current_command \ 16 | $_sponge_current_command_exit_code \ 17 | $_sponge_current_command_previously_in_history 18 | set command $_sponge_current_command 19 | break 20 | end 21 | end 22 | set --prepend --global _sponge_queue $command 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Andrei Borisov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /conf.d/sponge.fish: -------------------------------------------------------------------------------- 1 | # Sponge version 2 | set --global sponge_version 1.1.0 3 | 4 | # Allow to repeat previous command by default 5 | if not set --query --universal sponge_delay 6 | set --universal sponge_delay 2 7 | end 8 | 9 | # Purge entries both after `sponge_delay` entries and on exit by default 10 | if not set --query --universal sponge_purge_only_on_exit 11 | set --universal sponge_purge_only_on_exit false 12 | end 13 | 14 | # Add default filters 15 | if not set --query --universal sponge_filters 16 | set --universal sponge_filters sponge_filter_failed sponge_filter_matched 17 | end 18 | 19 | # Don't filter out commands that already have been in the history by default 20 | if not set --query --universal sponge_allow_previously_successful 21 | set --universal sponge_allow_previously_successful true 22 | end 23 | 24 | # Consider `0` the only successful exit code by default 25 | if not set --query --universal sponge_successful_exit_codes 26 | set --universal sponge_successful_exit_codes 0 27 | end 28 | 29 | # No active regex patterns by default 30 | if not set --query --universal sponge_regex_patterns 31 | set --universal sponge_regex_patterns 32 | end 33 | 34 | # Attach event handlers 35 | functions --query \ 36 | _sponge_on_prompt \ 37 | _sponge_on_preexec \ 38 | _sponge_on_postexec \ 39 | _sponge_on_exit 40 | 41 | # Initialize empty state for the first run 42 | function _sponge_install --on-event sponge_install 43 | set --global _sponge_current_command '' 44 | set --global _sponge_current_command_exit_code 0 45 | set --global _sponge_current_command_previously_in_history false 46 | end 47 | 48 | # Clean up variables 49 | function _sponge_uninstall --on-event sponge_uninstall 50 | _sponge_clear_state 51 | set --erase sponge_version 52 | end 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🧽 Sponge [](https://github.com/meaningful-ooo/sponge/releases/latest) 2 | 3 |