Class: OllamaChat::Utils::PathCompleter
- Inherits:
-
Object
- Object
- OllamaChat::Utils::PathCompleter
- Defined in:
- lib/ollama_chat/utils/path_completer.rb
Overview
Utility for completing file system paths in the interactive shell.
The class is instantiated with the text that precedes the current completion request (‘pre`) and the raw input string (`input`).
It supports two common patterns used by the CLI:
* ``./foo`` – relative paths starting with a dot
* ``~/foo`` – home‑directory expansion
The public API is a single method:
* `#complete` – returns an array of matching paths that start with
the user’s current input.
The implementation is deliberately lightweight and does not depend on external libraries. It uses ‘Dir.glob` to gather candidates and then filters them with `String#start_with?`.
Example
completer = OllamaChat::Utils::PathCompleter.new("cd ", "./app")
completions = completer.complete
# => ["./app/models", "./app/controllers", ...]
The class is intentionally simple so that it can be reused by different parts of the chat application.
Instance Method Summary collapse
-
#complete ⇒ Array<String>
Return an array of path completions that match the current input.
-
#expand_path(path) ⇒ String
Expands a given filesystem path to an absolute path, resolving any relative components and symlinks.
-
#initialize(pre, input) ⇒ PathCompleter
constructor
A new instance of PathCompleter.
Constructor Details
#initialize(pre, input) ⇒ PathCompleter
Returns a new instance of PathCompleter.
29 30 31 |
# File 'lib/ollama_chat/utils/path_completer.rb', line 29 def initialize(pre, input) @pre, @input = pre, input end |
Instance Method Details
#complete ⇒ Array<String>
Return an array of path completions that match the current input.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ollama_chat/utils/path_completer.rb', line 36 def complete before = [@pre, @input].join before =~ %r(([.~])\/\S*) path, first = $&, $1 case first when ?. # relative path starting with . dir = File.join(File.directory?(path) ? path : File.dirname(path), '') Dir.glob(dir + ?*).select { |f| f.start_with?(@input) } when ?~ = (path) dir = File.join(File.directory?() ? : File.dirname(), '') Dir.glob(dir + ?*).select { |f| f.start_with?((@input)) } .map { |f| f.sub((?~), ?~) } else [] end end |
#expand_path(path) ⇒ String
Expands a given filesystem path to an absolute path, resolving any relative components and symlinks
59 60 61 |
# File 'lib/ollama_chat/utils/path_completer.rb', line 59 def (path) File.(path) end |