From f49146b4e97fc31cc70db021c784795c1d16e881 Mon Sep 17 00:00:00 2001 From: Bastian Fredriksson Date: Sat, 12 Oct 2024 17:17:25 +0200 Subject: [PATCH] feat: option to disable status emoji Currently, a status emoji is being displayed in the right prompt of shell. If you have customised your right prompt you may want to disable the status emoji. Closes: #64 --- README.md | 13 +++++++++++ conf.d/fish_ai.fish | 4 ++-- functions/_fish_ai_autocomplete.fish | 6 ++++- functions/_fish_ai_autocomplete_or_fix.fish | 4 ++-- functions/_fish_ai_codify.fish | 6 ++++- functions/_fish_ai_codify_or_explain.fish | 4 ++-- functions/_fish_ai_explain.fish | 6 ++++- functions/_fish_ai_fix.fish | 6 ++++- pyproject.toml | 3 ++- src/fish_ai/config.py | 25 +++++++++++++++++++++ src/fish_ai/engine.py | 21 ++--------------- 11 files changed, 68 insertions(+), 30 deletions(-) create mode 100644 src/fish_ai/config.py diff --git a/README.md b/README.md index 6d2f68c..65be6bf 100644 --- a/README.md +++ b/README.md @@ -226,6 +226,19 @@ history_size = 5 If you enable this option, consider the use of [`sponge`](https://github.com/meaningful-ooo/sponge) to automatically remove broken commands from your commandline history. +### Disable the status emoji + +By default, a status emoji is shown in the [right prompt](https://fishshell.com/docs/current/cmds/fish_right_prompt.html). +If you already use your right prompt for something else, or just don't like +emojis, you can disable them: + +```ini +[fish-ai] +status_emoji = False +``` + +Restart any open terminal windows for the change to take effect. + ## 🎭 Switch between contexts You can switch between different sections in the configuration using the diff --git a/conf.d/fish_ai.fish b/conf.d/fish_ai.fish index bf553e5..1d34a79 100644 --- a/conf.d/fish_ai.fish +++ b/conf.d/fish_ai.fish @@ -14,8 +14,8 @@ bind \cP _fish_ai_codify_or_explain bind -k nul _fish_ai_autocomplete_or_fix ## -## This section contains functionality for setting and clearing the status shown -## in the right prompt. +## This section contains functionality for clearing the status emoji +## shown in the right prompt. ## bind \r 'clear_status; commandline -f execute' bind \cc 'clear_status; commandline -f repaint; commandline -f cancel-commandline' diff --git a/functions/_fish_ai_autocomplete.fish b/functions/_fish_ai_autocomplete.fish index b38a390..17295a5 100755 --- a/functions/_fish_ai_autocomplete.fish +++ b/functions/_fish_ai_autocomplete.fish @@ -1,6 +1,10 @@ #!/usr/bin/env fish function _fish_ai_autocomplete --description "Autocomplete the current command using AI." --argument-names command cursor_position - set selected_completion (~/.fish-ai/bin/autocomplete "$command" "$cursor_position" 2> /dev/null) + if test (~/.fish-ai/bin/lookup_setting "debug") = True + set selected_completion (~/.fish-ai/bin/autocomplete "$command" "$cursor_position") + else + set selected_completion (~/.fish-ai/bin/autocomplete "$command" "$cursor_position" 2> /dev/null) + end echo -n "$selected_completion" end diff --git a/functions/_fish_ai_autocomplete_or_fix.fish b/functions/_fish_ai_autocomplete_or_fix.fish index c58f6da..1819105 100755 --- a/functions/_fish_ai_autocomplete_or_fix.fish +++ b/functions/_fish_ai_autocomplete_or_fix.fish @@ -1,7 +1,7 @@ #!/usr/bin/env fish -function fish_right_prompt - echo "$status_emoji" +if test (~/.fish-ai/bin/lookup_setting status_emoji) != False + eval "function fish_right_prompt; echo (string escape \$status_emoji); end" end function _fish_ai_autocomplete_or_fix --description "Autocomplete the current command or fix the previous command using AI." diff --git a/functions/_fish_ai_codify.fish b/functions/_fish_ai_codify.fish index d612052..0cd5df7 100755 --- a/functions/_fish_ai_codify.fish +++ b/functions/_fish_ai_codify.fish @@ -1,6 +1,10 @@ #!/usr/bin/env fish function _fish_ai_codify --description "Turn a comment into a command using AI." --argument-names comment - set output (~/.fish-ai/bin/codify "$comment" 2> /dev/null) + if test (~/.fish-ai/bin/lookup_setting "debug") = True + set output (~/.fish-ai/bin/codify "$comment") + else + set output (~/.fish-ai/bin/codify "$comment" 2> /dev/null) + end echo -n "$output" end diff --git a/functions/_fish_ai_codify_or_explain.fish b/functions/_fish_ai_codify_or_explain.fish index a0572f5..b34cc1b 100755 --- a/functions/_fish_ai_codify_or_explain.fish +++ b/functions/_fish_ai_codify_or_explain.fish @@ -1,7 +1,7 @@ #!/usr/bin/env fish -function fish_right_prompt - echo "$status_emoji" +if test (~/.fish-ai/bin/lookup_setting status_emoji) != False + eval "function fish_right_prompt; echo (string escape \$status_emoji); end" end function _fish_ai_codify_or_explain --description "Transform a command into a comment and vice versa using AI." diff --git a/functions/_fish_ai_explain.fish b/functions/_fish_ai_explain.fish index 21cd995..b9bea35 100755 --- a/functions/_fish_ai_explain.fish +++ b/functions/_fish_ai_explain.fish @@ -1,6 +1,10 @@ #!/usr/bin/env fish function _fish_ai_explain --description "Turn a command into a comment using AI." --argument-names command - set output (~/.fish-ai/bin/explain "$command" 2> /dev/null) + if test (~/.fish-ai/bin/lookup_setting "debug") = True + set output (~/.fish-ai/bin/explain "$command") + else + set output (~/.fish-ai/bin/explain "$command" 2> /dev/null) + end echo -n "$output" end diff --git a/functions/_fish_ai_fix.fish b/functions/_fish_ai_fix.fish index 64fbb0d..88cc91e 100755 --- a/functions/_fish_ai_fix.fish +++ b/functions/_fish_ai_fix.fish @@ -1,6 +1,10 @@ #!/usr/bin/env fish function _fish_ai_fix --description "Fix a command using AI." --argument-names previous_command - set output (~/.fish-ai/bin/fix "$previous_command" 2> /dev/null) + if test (~/.fish-ai/bin/lookup_setting "debug") = True + set output (~/.fish-ai/bin/fix "$previous_command") + else + set output (~/.fish-ai/bin/fix "$previous_command" 2> /dev/null) + end echo -n "$output" end diff --git a/pyproject.toml b/pyproject.toml index f3b7e78..df40275 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "fish_ai" -version = "0.10.2" +version = "0.11.0" authors = [{ name = "Bastian Fredriksson", email = "realiserad@gmail.com" }] description = "Provides core functionality for fish-ai, an AI plugin for the fish shell." readme = "README.md" @@ -35,6 +35,7 @@ codify = "fish_ai.codify:codify" explain = "fish_ai.explain:explain" autocomplete = "fish_ai.autocomplete:autocomplete" switch_context = "fish_ai.switch_context:switch_context" +lookup_setting = "fish_ai.config:lookup_setting" [tool.pytest.ini_options] minversion = "6.0" diff --git a/src/fish_ai/config.py b/src/fish_ai/config.py new file mode 100644 index 0000000..4683682 --- /dev/null +++ b/src/fish_ai/config.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +from os import path +import sys +from configparser import ConfigParser + +config = ConfigParser() +config.read(path.expanduser('~/.config/fish-ai.ini')) + + +def lookup_setting(): + print(get_config(sys.argv[1] or '')) + + +def get_config(key): + if not config.has_section('fish-ai'): + # There is no configuration file or the user made a mistake. + # Just return 'None' here to simplify testing. + return None + + active_section = config.get(section='fish-ai', option='configuration') + + if config.has_option(section=active_section, option=key): + return config.get(section=active_section, option=key) + + return config.get(section='fish-ai', option=key, fallback=None) diff --git a/src/fish_ai/engine.py b/src/fish_ai/engine.py index 4130405..7456fa9 100644 --- a/src/fish_ai/engine.py +++ b/src/fish_ai/engine.py @@ -4,8 +4,6 @@ from openai import AzureOpenAI import google.generativeai as genai from google.generativeai.types import GenerationConfig -from configparser import ConfigParser -from os import path from os.path import isfile import platform import logging @@ -21,9 +19,8 @@ import itertools from azure.ai.inference import ChatCompletionsClient from azure.core.credentials import AzureKeyCredential - -config = ConfigParser() -config.read(path.expanduser('~/.config/fish-ai.ini')) +from fish_ai.config import get_config +from os import path def get_args(): @@ -127,20 +124,6 @@ def get_system_prompt(): } -def get_config(key): - if not config.has_section('fish-ai'): - # There is no configuration file or the user made a mistake. - # Just return 'None' here to simplify testing. - return None - - active_section = config.get(section='fish-ai', option='configuration') - - if config.has_option(section=active_section, option=key): - return config.get(section=active_section, option=key) - - return config.get(section='fish-ai', option=key, fallback=None) - - def get_openai_client(): if (get_config('provider') == 'azure'): return AzureOpenAI(