Class: Rufio::SearchController

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

Overview

検索(fzf・rga)専用コントローラKeybindHandler から fzf/rga 検索系メソッドを分離し、単一責任原則に準拠

Instance Method Summary collapse

Constructor Details

#initialize(directory_listing, file_opener) ⇒ SearchController

Returns a new instance of SearchController.



9
10
11
12
# File 'lib/rufio/search_controller.rb', line 9

def initialize(directory_listing, file_opener)
  @directory_listing = directory_listing
  @file_opener = file_opener
end

Instance Method Details

#fzf_available?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rufio/search_controller.rb', line 40

def fzf_available?
  system('which fzf > /dev/null 2>&1')
end

#fzf_searchObject

fzf 検索



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rufio/search_controller.rb', line 22

def fzf_search
  return false unless fzf_available?

  current_path = @directory_listing&.current_path || Dir.pwd

  selected_file = nil
  Dir.chdir(current_path) do
    selected_file = `find . -type f | fzf --preview 'cat {}'`.strip
  end

  if !selected_file.empty?
    full_path = File.expand_path(selected_file, current_path)
    @file_opener.open_file(full_path) if File.exist?(full_path)
  end

  :needs_refresh
end

#rga_available?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/rufio/search_controller.rb', line 87

def rga_available?
  system('which rga > /dev/null 2>&1')
end

#rga_searchObject

rga 検索



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rufio/search_controller.rb', line 48

def rga_search
  return false unless rga_available?

  current_path = @directory_listing&.current_path || Dir.pwd

  print ConfigLoader.message('keybind.search_text')
  search_query = STDIN.gets.chomp
  return false if search_query.empty?

  search_results = nil
  Dir.chdir(current_path) do
    escaped_query = Shellwords.escape(search_query)
    search_results = `rga --line-number --with-filename #{escaped_query} . 2>/dev/null`
  end

  if search_results.empty?
    puts "\n#{ConfigLoader.message('keybind.no_matches')}"
    print ConfigLoader.message('keybind.press_any_key')
    STDIN.getch
    return true
  end

  selected_result = IO.popen('fzf', 'r+') do |fzf|
    fzf.write(search_results)
    fzf.close_write
    fzf.read.strip
  end

  if !selected_result.empty? && selected_result.match(/^(.+?):(\d+):/)
    file_path = ::Regexp.last_match(1)
    line_number = ::Regexp.last_match(2).to_i
    full_path = File.expand_path(file_path, current_path)

    @file_opener.open_file_with_line(full_path, line_number) if File.exist?(full_path)
  end

  :needs_refresh
end

#set_directory_listing(directory_listing) ⇒ Object



14
15
16
# File 'lib/rufio/search_controller.rb', line 14

def set_directory_listing(directory_listing)
  @directory_listing = directory_listing
end