Class: Rufio::ShellCommandCompletion

Inherits:
Object
  • Object
show all
Defined in:
lib/rufio/shell_command_completion.rb

Overview

シェルコマンド補完機能を提供するクラス

Instance Method Summary collapse

Instance Method Details

#complete_command(input) ⇒ Array<String>

PATHからコマンドを補完

Parameters:

  • input (String)

    入力されたコマンドの一部

Returns:

  • (Array<String>)

    補完候補のリスト



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>

コマンド履歴から補完

Parameters:

  • input (String)

    入力されたコマンドの一部

  • history (CommandHistory)

    コマンド履歴オブジェクト

Returns:

  • (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>

ファイルパスを補完

Parameters:

  • input (String)

    入力されたパスの一部

  • options (Hash) (defaults to: {})

    オプション

Options Hash (options):

  • :directories_only (Boolean)

    ディレクトリのみを補完

Returns:

  • (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, options = {})
  return [] if input.nil?

  # ~を展開
  expanded_input = File.expand_path(input) rescue input

  # 入力が/で終わる場合(ディレクトリ内を補完)
  if input.end_with?("/")
    dir = expanded_input
    pattern = File.join(dir, "*")
  else
    # ディレクトリ部分とファイル名部分を分離
    dir = File.dirname(expanded_input)
    basename = File.basename(expanded_input)
    pattern = File.join(dir, "#{basename}*")
  end

  # ディレクトリが存在しない場合は空の配列を返す
  return [] unless Dir.exist?(dir)

  # マッチするファイル/ディレクトリを取得
  candidates = Dir.glob(pattern)

  # ディレクトリのみのフィルタリング
  if options[: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