Class: Toys::Completion::FileSystem
Overview
A Completion that returns candidates from the local file system.
Instance Attribute Summary collapse
-
#cwd ⇒ String
readonly
Path to the starting directory.
-
#include_directories ⇒ boolean
readonly
Whether directories are included in the completion candidates.
-
#include_files ⇒ boolean
readonly
Whether files are included in the completion candidates.
-
#prefix_constraint ⇒ String, Regexp
readonly
Constraint on the fragment prefix.
Instance Method Summary collapse
-
#call(context) ⇒ Array<Toys::Completion::Candidate>
Returns candidates for the current completion.
-
#initialize(cwd: nil, omit_files: false, omit_directories: false, prefix_constraint: "") ⇒ FileSystem
constructor
Create a completion that gets candidates from names in the local file system.
Constructor Details
#initialize(cwd: nil, omit_files: false, omit_directories: false, prefix_constraint: "") ⇒ FileSystem
Create a completion that gets candidates from names in the local file system.
254 255 256 257 258 259 260 |
# File 'lib/toys/completion.rb', line 254 def initialize(cwd: nil, omit_files: false, omit_directories: false, prefix_constraint: "") super() @cwd = cwd || ::Dir.pwd @include_files = !omit_files @include_directories = !omit_directories @prefix_constraint = prefix_constraint end |
Instance Attribute Details
#cwd ⇒ String (readonly)
Path to the starting directory.
284 285 286 |
# File 'lib/toys/completion.rb', line 284 def cwd @cwd end |
#include_directories ⇒ boolean (readonly)
Whether directories are included in the completion candidates.
272 273 274 |
# File 'lib/toys/completion.rb', line 272 def include_directories @include_directories end |
#include_files ⇒ boolean (readonly)
Whether files are included in the completion candidates.
266 267 268 |
# File 'lib/toys/completion.rb', line 266 def include_files @include_files end |
#prefix_constraint ⇒ String, Regexp (readonly)
Constraint on the fragment prefix.
278 279 280 |
# File 'lib/toys/completion.rb', line 278 def prefix_constraint @prefix_constraint end |
Instance Method Details
#call(context) ⇒ Array<Toys::Completion::Candidate>
Returns candidates for the current completion.
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/toys/completion.rb', line 293 def call(context) return [] unless @prefix_constraint === context.fragment_prefix substring = context.fragment prefix, name = if substring.empty? || substring.end_with?("/") [substring, ""] else ::File.split(substring) end dir = ::File.(prefix, @cwd) return [] unless ::File.directory?(dir) prefix = nil if [".", ""].include?(prefix) children = [] if context[:shell] == :bash # Bash completion requires that you handle globs explicitly. ::Dir.glob(name, base: dir).each do |child| children << child unless /^\.{0,2}$/.match?(child) end end ::Dir.children(dir).each do |child| children << child if child.start_with?(name) end generate_candidates(children.uniq.sort, prefix, dir) end |