Class: Rufio::ShellCommandCompletion
- Inherits:
-
Object
- Object
- Rufio::ShellCommandCompletion
- Defined in:
- lib/rufio/shell_command_completion.rb
Overview
シェルコマンド補完機能を提供するクラス
Instance Method Summary collapse
-
#complete_command(input) ⇒ Array<String>
PATHからコマンドを補完.
-
#complete_from_history(input, history) ⇒ Array<String>
コマンド履歴から補完.
-
#complete_path(input, options = {}) ⇒ Array<String>
ファイルパスを補完.
Instance Method Details
#complete_command(input) ⇒ Array<String>
PATHからコマンドを補完
9 10 11 12 13 14 |
# File 'lib/rufio/shell_command_completion.rb', line 9 def complete_command(input) return [] if input.nil? || input.empty? input_lower = input.downcase path_commands.select { |cmd| cmd.downcase.start_with?(input_lower) } end |
#complete_from_history(input, history) ⇒ Array<String>
コマンド履歴から補完
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rufio/shell_command_completion.rb', line 64 def complete_from_history(input, history) return [] if input.nil? || input.empty? return [] unless history # 履歴から全てのコマンドを取得 # CommandHistoryクラスから履歴を取得する方法が必要 # 現在の実装では、@historyインスタンス変数にアクセスできないため、 # 新しいメソッドを追加する必要がある # 一旦、空の配列を返す実装にして、後でCommandHistoryを拡張する commands = get_history_commands(history) input_lower = input.downcase commands.select { |cmd| cmd.downcase.start_with?(input_lower) }.uniq end |
#complete_path(input, options = {}) ⇒ Array<String>
ファイルパスを補完
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rufio/shell_command_completion.rb', line 21 def complete_path(input, = {}) return [] if input.nil? # ~を展開 = File.(input) rescue input # 入力が/で終わる場合(ディレクトリ内を補完) if input.end_with?("/") dir = pattern = File.join(dir, "*") else # ディレクトリ部分とファイル名部分を分離 dir = File.dirname() basename = File.basename() pattern = File.join(dir, "#{basename}*") end # ディレクトリが存在しない場合は空の配列を返す return [] unless Dir.exist?(dir) # マッチするファイル/ディレクトリを取得 candidates = Dir.glob(pattern) # ディレクトリのみのフィルタリング if [:directories_only] candidates.select! { |path| File.directory?(path) } end # 元の入力が~で始まる場合、結果も~で始まるように変換 if input.start_with?("~") home = ENV['HOME'] candidates.map! { |path| path.sub(home, "~") } end candidates.sort rescue StandardError [] end |