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



21
22
23
# File 'lib/ollama_chat/config_handling.rb', line 21

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



60
61
62
# File 'lib/ollama_chat/config_handling.rb', line 60

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

#diff_configBoolean? (private)

Opens a diff tool to compare the current config file with the default config, allowing the user to visually inspect differences.

Returns:

  • (Boolean, nil)

    the result of system if a diff tool was found, or nil if no diff tool is configured.



84
85
86
87
88
89
90
91
92
93
# File 'lib/ollama_chat/config_handling.rb', line 84

def diff_config
  diff_tool = OC::DIFF_TOOL? or
    return STDERR.puts 'No diff tool configured (OC::DIFF_TOOL).'
  cmd = [
    diff_tool,
    @ollama_chat_config.filename,
    @ollama_chat_config.default_config_path,
  ].map(&:to_s)
  system(*cmd)
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.



68
69
70
71
72
73
74
75
76
77
# File 'lib/ollama_chat/config_handling.rb', line 68

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.


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ollama_chat/config_handling.rb', line 166

def edit_config
  if result = edit_file(@ollama_chat_config.filename)
    if confirm?(
        prompt: "๐Ÿ”” Do you want to restart #{progname}? (y/n) ",
        yes: /\Ay/i
      )
    then
      session_close
      exec($0, *fix_session(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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ollama_chat/config_handling.rb', line 102

def fix_config(exception)
  STDOUT.puts "When reading the config file, a #{exception.class} " \
    "exception was caught: #{exception.message.inspect}"
  unless OC::DIFF_TOOL?
    exit 1
  end
  if confirm?(
      prompt: '๐Ÿ”” Do you want to fix the config? (y/n) ',
      yes: /\Ay/i
    )
  then
    diff_config
    exit 0
  else
    exit 1
  end
end

#fix_session(argv) ⇒ Array<String> (private)

Adjusts the command-line argument array to ensure the current session is explicitly reloaded by injecting the -l <session_id> flag.

This method handles the following argument scenarios to prevent breaking existing flags:

  1. Missing Flag: If -l is not present, it appends -l and the current session ID to the end of the array.
  2. Empty Flag: If -l is present but the next element is another flag (starts with -), it inserts the session ID immediately after -l. (This case should actually never happen.)
  3. Existing Flag: If -l is present and followed by a value, it replaces that value with the current session ID to ensure continuity.

Parameters:

  • argv (Array<String>)

    The array of command-line arguments (typically ARGV) to be modified.

Returns:

  • (Array<String>)

    A new, duplicated array containing the updated arguments, ensuring the original ARGV remains unmodified.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ollama_chat/config_handling.rb', line 139

def fix_session(argv)
  argv = argv.dup
  if i = argv.index('-l')
    j = i + 1
    unless argv[j]&.start_with?(?-)
      argv[j] = session.id.to_s
    else
      argv.insert(j, session.id.to_s)
    end
  else
    argv << '-l' << session.id.to_s
  end
  argv
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"


187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ollama_chat/config_handling.rb', line 187

def reload_config
  if confirm?(
      prompt: "๐Ÿ”” Do you want to restart #{progname}? (y/n) ",
      yes: /\Ay/i
    )
  then
    session_close
    exec($0, *fix_session(ARGV))
  else
    STDOUT.puts "Skipped reloading the config."
  end
end

#run_syntax_check(checker, path) ⇒ Hash?

Runs the syntax checker on the given file and returns a result hash.

Parameters:

  • checker (ComplexConfig::Settings)

    the resolved checker config

  • path (String, Pathname)

    the file to check

Returns:

  • (Hash, nil)

    { status:, output: } or nil if binary is missing



45
46
47
48
49
50
51
52
53
# File 'lib/ollama_chat/config_handling.rb', line 45

def run_syntax_check(checker, path)
  _stdout, stderr, status = Open3.capture3(*checker.cmd, path.to_s)
  {
    status: status.success? ? 'pass' : 'fail',
    output: stderr.strip
  }
rescue Errno::ENOENT
  nil
end

#syntax_checker_for(path) ⇒ ComplexConfig::Settings?

Returns the first enabled syntax checker whose suffixes match the given file's extension, or nil if no match.

Parameters:

  • path (String, Pathname)

    the file path to check

Returns:

  • (ComplexConfig::Settings, nil)

    the matching checker config



30
31
32
33
34
35
36
37
38
# File 'lib/ollama_chat/config_handling.rb', line 30

def syntax_checker_for(path)
  path = Pathname.new(path)
  ext = path.extname.delete_prefix(?.).full? or return
  config.syntax_checkers.attribute_values.each do |checker|
    next unless checker.enabled
    return checker if checker.suffixes.include?(ext)
  end
  nil
end