Module: OllamaChat::ConfigHandling

Extended by:
Tins::Concern
Included in:
Chat
Defined in:
lib/ollama_chat/config_handling.rb

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

Instance Method Details

#configComplexConfig::Settings

The config method returns the configuration object associated with the class.

Returns:

  • (ComplexConfig::Settings)

    the configuration instance



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.

Parameters:

  • config (ComplexConfig::Settings)

    the configuration object to be set



28
29
30
# File 'lib/ollama_chat/config_handling.rb', line 28

def config=(config)
  self.class.config = config
end

#display_configObject (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_configObject (private)

Edit the current configuration file in the editor defined by the environment variable EDITOR.

  1. Looks up the editor command via OC::EDITOR. If the value is nil or empty, it prints an error message to STDERR and returns immediately.

  2. Invokes the editor with the path to the active configuration file (β€˜@ollama_chat_config.filename`). The editor is launched via system so that the process inherits the current terminal, allowing in‑place editing.

  3. If editing was successful, prompts the user to restart ollama_chat if 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.

Parameters:

  • exception (Exception)

    the exception that occurred while reading the config file



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.message.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_configObject (private)

Reloads the current configuration by restarting the process.

Examples:

Restarting the app after confirmation

config.reload_config  # => restarts if user answers "y"


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