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.
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) ⇒ 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) ⇒ FileSystem
Create a completion that gets candidates from names in the local file system.
263 264 265 266 267 268 |
# File 'lib/toys/completion.rb', line 263 def initialize(cwd: nil, omit_files: false, omit_directories: false) super() @cwd = cwd || ::Dir.pwd @include_files = !omit_files @include_directories = !omit_directories end |
Instance Attribute Details
#cwd ⇒ String (readonly)
Path to the starting directory.
286 287 288 |
# File 'lib/toys/completion.rb', line 286 def cwd @cwd end |
#include_directories ⇒ boolean (readonly)
Whether directories are included in the completion candidates.
280 281 282 |
# File 'lib/toys/completion.rb', line 280 def include_directories @include_directories end |
#include_files ⇒ boolean (readonly)
Whether files are included in the completion candidates.
274 275 276 |
# File 'lib/toys/completion.rb', line 274 def include_files @include_files end |
Instance Method Details
#call(context) ⇒ Array<Toys::Completion::Candidate>
Returns candidates for the current completion.
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/toys/completion.rb', line 295 def call(context) 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 |