Module: OllamaChat::ConfigHandling
Overview
Provides functionality for handling configuration files and settings for OllamaChat. It loads configuration from YAML files, supports environment variable overrides, and offers methods to read and write configuration data. The module also ensures default values are set and validates configuration structure.
Instance Method Summary collapse
-
#config ⇒ ComplexConfig::Settings
The config method returns the configuration object associated with the class.
-
#config=(config) ⇒ Object
private
The config= method assigns a new configuration object to the class.
-
#display_config ⇒ Object
private
The display_config method renders the configuration and displays it using a pager.
-
#edit_config ⇒ Object
private
Edit the current configuration file in the editor defined by the environment variable
EDITOR. -
#fix_config(exception) ⇒ Object
private
The fix_config method handles configuration file errors by informing the user about the exception and prompting them to fix it.
-
#reload_config ⇒ Object
private
Reloads the current configuration by restarting the process.
Instance Method Details
#config ⇒ ComplexConfig::Settings
The config method returns the configuration object associated with the class.
19 20 21 |
# File 'lib/ollama_chat/config_handling.rb', line 19 def config self.class.config end |
#config=(config) ⇒ Object (private)
The config= method assigns a new configuration object to the class.
28 29 30 |
# File 'lib/ollama_chat/config_handling.rb', line 28 def config=(config) self.class.config = config end |
#display_config ⇒ Object (private)
The display_config method renders the configuration and displays it using a pager. It determines an appropriate pager command based on environment variables and available system commands, then uses Kramdown::ANSI::Pager to show the formatted configuration output.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ollama_chat/config_handling.rb', line 36 def display_config command = OC::PAGER? rendered = config.to_s Kramdown::ANSI::Pager.pager( lines: rendered.count(?\n), command: ) do |output| output.puts rendered end end |
#edit_config ⇒ Object (private)
Edit the current configuration file in the editor defined by the environment variable EDITOR.
-
Looks up the editor command via
OC::EDITOR. If the value isnilor empty, it prints an error message to STDERR and returns immediately. -
Invokes the editor with the path to the active configuration file (β@ollama_chat_config.filename`). The editor is launched via
systemso that the process inherits the current terminal, allowing inβplace editing. -
If editing was successful, prompts the user to restart
ollama_chatif desired.
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ollama_chat/config_handling.rb', line 85 def edit_config if result = edit_file(@ollama_chat_config.filename) if confirm?(prompt: "π Do you want to restart #{progname}? (y/n) ") =~ /y/i save_conversation(OC::XDG_CACHE_HOME + 'backup.json') save_history exec($0, *ARGV) else STDOUT.puts "Skipped reloading the config." end else STDERR.puts "Editor returned a non-zero status!" end end |
#fix_config(exception) ⇒ Object (private)
The fix_config method handles configuration file errors by informing the user about the exception and prompting them to fix it. It then executes a diff tool to compare the current config file with the default one. This method exits the program after handling the configuration error.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ollama_chat/config_handling.rb', line 54 def fix_config(exception) save_conversation(OC::XDG_CACHE_HOME + 'backup.json') STDOUT.puts "When reading the config file, a #{exception.class} "\ "exception was caught: #{exception..inspect}" unless diff_tool = OC::DIFF_TOOL? exit 1 end if confirm?(prompt: 'π Do you want to fix the config? (y/n) ') =~ /y/i system Shellwords.join([ diff_tool, @ollama_chat_config.filename, @ollama_chat_config.default_config_path, ]) exit 0 else exit 1 end end |
#reload_config ⇒ Object (private)
Reloads the current configuration by restarting the process.
103 104 105 106 107 108 109 110 111 |
# File 'lib/ollama_chat/config_handling.rb', line 103 def reload_config if confirm?(prompt: "π Do you want to restart #{progname}? (y/n) ") =~ /y/i save_conversation(OC::XDG_CACHE_HOME + 'backup.json') save_history exec($0, *ARGV) else STDOUT.puts "Skipped reloading the config." end end |