Class: Rufio::KeybindHandler

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

Constant Summary collapse

ASCII_PRINTABLE_START =

ASCII character range constants

32
ASCII_PRINTABLE_END =
127
MULTIBYTE_THRESHOLD =
1
CONFIRMATION_DIALOG_WIDTH =

Dialog size constants

45
DIALOG_BORDER_HEIGHT =
4
FILESYSTEM_SYNC_DELAY =

File system operation constants

0.01

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKeybindHandler

10ms wait for filesystem sync



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
86
87
88
89
90
91
92
93
94
95
# File 'lib/rufio/keybind_handler.rb', line 48

def initialize
  @directory_listing = nil
  @terminal_ui = nil
  @file_opener = FileOpener.new

  # Manager classes
  @filter_manager = FilterManager.new
  @selection_manager = SelectionManager.new
  @file_operations = FileOperations.new
  @dialog_renderer = DialogRenderer.new
  @bookmark_manager = BookmarkManager.new(Bookmark.new, @dialog_renderer)
  @zoxide_integration = ZoxideIntegration.new(@dialog_renderer)
  @notification_manager = NotificationManager.new
  @script_path_manager = ScriptPathManager.new(Config::SCRIPT_PATHS_YML)

  # Job mode
  @job_manager = JobManager.new(notification_manager: @notification_manager)
  @job_mode = JobMode.new(job_manager: @job_manager)

  # NavigationController(移動・フィルタ・プレビュースクロールを担当)
  @nav_controller = NavigationController.new(nil, @filter_manager)

  # FileOperationController(ファイル操作を担当)
  @file_op_controller = FileOperationController.new(
    nil, @file_operations, @dialog_renderer, @nav_controller, @selection_manager
  )

  # BookmarkController(ブックマーク・zoxide・スクリプトパス管理を担当)
  @bookmark_controller = BookmarkController.new(
    nil, @bookmark_manager, @dialog_renderer, @nav_controller,
    @script_path_manager, @notification_manager, @zoxide_integration
  )

  # SearchController(fzf・rga検索を担当)
  @search_controller = SearchController.new(nil, @file_opener)

  # Help mode
  @in_help_mode = false
  @pre_help_directory = nil

  # Log viewer mode
  @in_log_viewer_mode = false
  @pre_log_viewer_directory = nil
  @log_dir = File.join(Dir.home, '.config', 'rufio', 'logs')

  # キーマップを構築(ConfigLoader.keybinds から逆引きマップを作成)
  build_key_map
end

Instance Attribute Details

#job_managerObject (readonly)

ジョブマネージャを取得



646
647
648
# File 'lib/rufio/keybind_handler.rb', line 646

def job_manager
  @job_manager
end

#job_modeObject (readonly)

ジョブマネージャを取得



646
647
648
# File 'lib/rufio/keybind_handler.rb', line 646

def job_mode
  @job_mode
end

#notification_managerObject (readonly)

ジョブマネージャを取得



646
647
648
# File 'lib/rufio/keybind_handler.rb', line 646

def notification_manager
  @notification_manager
end

Instance Method Details

#bookmark_listObject

UIRenderer 用:Tab移動と同じブックマークリストを返す



384
385
386
# File 'lib/rufio/keybind_handler.rb', line 384

def bookmark_list
  @bookmark_manager.list
end

#current_entryObject



188
189
190
191
# File 'lib/rufio/keybind_handler.rb', line 188

def current_entry
  entries = get_active_entries
  entries[@nav_controller.current_index]
end

#current_indexObject

NavigationController への委譲



20
21
22
# File 'lib/rufio/keybind_handler.rb', line 20

def current_index
  @nav_controller.current_index
end

#enter_help_modeObject

ヘルプモードに入る



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/rufio/keybind_handler.rb', line 218

def enter_help_mode
  return false unless @directory_listing

  # 現在のディレクトリを保存
  @pre_help_directory = @directory_listing.current_path

  # info ディレクトリに移動
  rufio_root = File.expand_path('../..', __dir__)
  info_dir = File.join(rufio_root, 'info')

  # info ディレクトリが存在することを確認
  return false unless Dir.exist?(info_dir)

  # ヘルプモードを有効化
  @in_help_mode = true

  # info ディレクトリに移動
  navigate_to_directory(info_dir)

  true
end

#enter_job_modeObject

ジョブモードに入る



649
650
651
652
653
# File 'lib/rufio/keybind_handler.rb', line 649

def enter_job_mode
  @job_mode.activate
  @terminal_ui&.set_job_mode(@job_mode, @job_manager, @notification_manager) if @terminal_ui
  true
end

#enter_log_viewer_modeObject

ログビューワモードに入る



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/rufio/keybind_handler.rb', line 265

def enter_log_viewer_mode
  return false unless @directory_listing

  # 現在のディレクトリを保存
  @pre_log_viewer_directory = @directory_listing.current_path

  # log ディレクトリを作成(存在しない場合)
  FileUtils.mkdir_p(@log_dir) unless Dir.exist?(@log_dir)

  # log ディレクトリに移動
  navigate_to_directory(@log_dir)

  # ログビューワモードを有効化
  @in_log_viewer_mode = true

  true
end

#exit_help_modeObject

ヘルプモードを終了



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/rufio/keybind_handler.rb', line 241

def exit_help_mode
  return false unless @in_help_mode
  return false unless @pre_help_directory

  # ヘルプモードを無効化
  @in_help_mode = false

  # 元のディレクトリに戻る
  navigate_to_directory(@pre_help_directory)

  # 保存したディレクトリをクリア
  @pre_help_directory = nil

  true
end

#exit_job_modeObject

ジョブモードを終了



656
657
658
659
660
661
# File 'lib/rufio/keybind_handler.rb', line 656

def exit_job_mode
  @job_mode.deactivate
  @terminal_ui&.exit_job_mode if @terminal_ui
  @nav_controller.refresh
  true
end

#exit_log_viewer_modeObject

ログビューワモードを終了



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/rufio/keybind_handler.rb', line 284

def exit_log_viewer_mode
  return false unless @in_log_viewer_mode
  return false unless @pre_log_viewer_directory

  # ログビューワモードを無効化
  @in_log_viewer_mode = false

  # 元のディレクトリに戻る
  navigate_to_directory(@pre_log_viewer_directory)

  # 保存したディレクトリをクリア
  @pre_log_viewer_directory = nil

  true
end

#filter_active?Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/rufio/keybind_handler.rb', line 193

def filter_active?
  @filter_manager.filter_active?
end

#filter_queryObject



32
33
34
# File 'lib/rufio/keybind_handler.rb', line 32

def filter_query
  @filter_manager.filter_query
end

#focus_preview_paneObject

プレビューペイン関連メソッド(NavigationController に委譲)



345
346
347
# File 'lib/rufio/keybind_handler.rb', line 345

def focus_preview_pane
  @nav_controller.focus_preview_pane(current_entry)
end

#get_active_entriesObject



197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rufio/keybind_handler.rb', line 197

def get_active_entries
  entries = if @filter_manager.filter_active?
              @filter_manager.filtered_entries
            else
              @directory_listing&.list_entries || []
            end
  if @in_help_mode || @in_log_viewer_mode
    entries.reject { |e| e[:name] == '..' }
  else
    entries
  end
end

#goto_bookmark(number) ⇒ Object

数字キー(1-9): 指定番号のブックマークへジャンプ



389
390
391
# File 'lib/rufio/keybind_handler.rb', line 389

def goto_bookmark(number)
  @bookmark_controller.goto_bookmark(number)
end

#goto_next_bookmarkObject

Tabキー: 次のブックマークへ循環移動



374
375
376
# File 'lib/rufio/keybind_handler.rb', line 374

def goto_next_bookmark
  @bookmark_controller.goto_next_bookmark
end

#goto_prev_bookmarkObject

Shift+Tabキー: 前のブックマークへ循環移動



379
380
381
# File 'lib/rufio/keybind_handler.rb', line 379

def goto_prev_bookmark
  @bookmark_controller.goto_prev_bookmark
end

#handle_job_mode_key(key) ⇒ Object

ジョブモード中のキー処理



664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'lib/rufio/keybind_handler.rb', line 664

def handle_job_mode_key(key)
  result = @job_mode.handle_key(key)

  case result
  when :exit
    exit_job_mode
    true
  when :show_log
    @terminal_ui&.trigger_job_mode_redraw if @terminal_ui
    true
  when true, false
    @terminal_ui&.trigger_job_mode_redraw if @terminal_ui
    result
  else
    result
  end
end

#handle_key(key) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rufio/keybind_handler.rb', line 130

def handle_key(key)
  return false unless @directory_listing

  # ジョブモード中の特別処理
  if @job_mode.active?
    return handle_job_mode_key(key)
  end

  # プレビューペインフォーカス中の特別処理
  if @nav_controller.preview_focused?
    return @nav_controller.handle_preview_focus_key(key)
  end

  # ヘルプモード中のESCキー特別処理
  if @in_help_mode && key == "\e"
    return exit_help_mode
  end

  # ログビューワモード中のESCキー特別処理
  if @in_log_viewer_mode && key == "\e"
    return exit_log_viewer_mode
  end

  # フィルターモード中は他のキーバインドを無効化
  return @nav_controller.handle_filter_input(key) if @filter_manager.filter_mode

  entries = get_active_entries

  # Enter キー(特殊処理: プレビューペインフォーカスまたはナビゲーション)
  if key == "\r" || key == "\n"
    return @nav_controller.handle_enter_key(current_entry, @in_help_mode, @in_log_viewer_mode)
  end

  # ESC キー(特殊処理: フィルタクリア)
  if key == "\e"
    if @filter_manager.filter_active?
      @nav_controller.clear_filter_mode
      return true
    else
      return false
    end
  end

  # 数字キー(1-9): ブックマーク番号によるジャンプ(設定化の対象外)
  return goto_bookmark(key.to_i) if key.match?(/^[1-9]$/)

  # キーマップでアクションを検索してdispatch
  action = @key_map[key]
  return false unless action

  dispatch(action, entries)
end

#has_jobs?Boolean

ジョブがあるかどうか

Returns:

  • (Boolean)


688
689
690
# File 'lib/rufio/keybind_handler.rb', line 688

def has_jobs?
  @job_manager.has_jobs?
end

#help_mode?Boolean

ヘルプモード中かどうか

Returns:

  • (Boolean)


213
214
215
# File 'lib/rufio/keybind_handler.rb', line 213

def help_mode?
  @in_help_mode
end

#in_job_mode?Boolean

ジョブモード中かどうか

Returns:

  • (Boolean)


683
684
685
# File 'lib/rufio/keybind_handler.rb', line 683

def in_job_mode?
  @job_mode.active?
end

#is_selected?(entry_name) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
127
128
# File 'lib/rufio/keybind_handler.rb', line 119

def is_selected?(entry_name)
  # Only show as selected if we're in the same directory where selection happened
  current_path = @directory_listing&.current_path || Dir.pwd
  source_path = @selection_manager.source_directory

  # If no source directory or different directory, nothing is selected
  return false if source_path.nil? || current_path != source_path

  @selection_manager.selected?(entry_name)
end

#job_status_bar_textObject

ジョブのステータスバーテキストを取得



693
694
695
696
697
# File 'lib/rufio/keybind_handler.rb', line 693

def job_status_bar_text
  return nil unless @job_manager.has_jobs?

  @job_manager.status_bar_text
end

#log_viewer_mode?Boolean

ログビューワモード中かどうか

Returns:

  • (Boolean)


260
261
262
# File 'lib/rufio/keybind_handler.rb', line 260

def log_viewer_mode?
  @in_log_viewer_mode
end

ヘルプモード時の制限付き親ディレクトリナビゲーション



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/rufio/keybind_handler.rb', line 301

def navigate_parent_with_restriction
  if @in_help_mode
    # info ディレクトリより上には移動できない
    rufio_root = File.expand_path('../..', __dir__)
    info_dir = File.join(rufio_root, 'info')

    current_path = @directory_listing.current_path

    # 現在のパスが info ディレクトリ以下でない場合は移動を許可しない
    unless current_path.start_with?(info_dir)
      return false
    end

    # 現在のパスが info ディレクトリそのものの場合は移動を許可しない
    if current_path == info_dir
      return false
    end

    # info ディレクトリ配下であれば、通常のナビゲーションを実行
    navigate_parent
  elsif @in_log_viewer_mode
    # log ディレクトリより上には移動できない
    current_path = @directory_listing.current_path

    # 現在のパスが log ディレクトリ以下でない場合は移動を許可しない
    unless current_path.start_with?(@log_dir)
      return false
    end

    # 現在のパスが log ディレクトリそのものの場合は移動を許可しない
    if current_path == @log_dir
      return false
    end

    # log ディレクトリ配下であれば、通常のナビゲーションを実行
    navigate_parent
  else
    # ヘルプモード・ログビューワモード外では通常のナビゲーション
    navigate_parent
  end
end

#preview_focused?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/rufio/keybind_handler.rb', line 24

def preview_focused?
  @nav_controller.preview_focused?
end

#preview_scroll_offsetObject



28
29
30
# File 'lib/rufio/keybind_handler.rb', line 28

def preview_scroll_offset
  @nav_controller.preview_scroll_offset
end

#reset_preview_scrollObject



369
370
371
# File 'lib/rufio/keybind_handler.rb', line 369

def reset_preview_scroll
  @nav_controller.reset_preview_scroll
end

#scroll_preview_downObject



353
354
355
# File 'lib/rufio/keybind_handler.rb', line 353

def scroll_preview_down
  @nav_controller.scroll_preview_down
end

#scroll_preview_page_downObject



361
362
363
# File 'lib/rufio/keybind_handler.rb', line 361

def scroll_preview_page_down
  @nav_controller.scroll_preview_page_down
end

#scroll_preview_page_upObject



365
366
367
# File 'lib/rufio/keybind_handler.rb', line 365

def scroll_preview_page_up
  @nav_controller.scroll_preview_page_up
end

#scroll_preview_upObject



357
358
359
# File 'lib/rufio/keybind_handler.rb', line 357

def scroll_preview_up
  @nav_controller.scroll_preview_up
end

#select_index(index) ⇒ Object



183
184
185
186
# File 'lib/rufio/keybind_handler.rb', line 183

def select_index(index)
  entries = get_active_entries
  @nav_controller.select_index([[index, 0].max, entries.length - 1].min)
end

#selected_itemsObject



115
116
117
# File 'lib/rufio/keybind_handler.rb', line 115

def selected_items
  @selection_manager.selected_items
end

#set_directory_listing(directory_listing) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/rufio/keybind_handler.rb', line 97

def set_directory_listing(directory_listing)
  @directory_listing = directory_listing
  @nav_controller.set_directory_listing(directory_listing)
  @file_op_controller.set_directory_listing(directory_listing)
  @bookmark_controller.set_directory_listing(directory_listing)
  @search_controller.set_directory_listing(directory_listing)
end

#set_terminal_ui(terminal_ui) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/rufio/keybind_handler.rb', line 105

def set_terminal_ui(terminal_ui)
  @terminal_ui = terminal_ui
  @nav_controller.set_terminal_ui(terminal_ui)
  @file_op_controller.set_terminal_ui(terminal_ui)
  @bookmark_controller.set_terminal_ui(terminal_ui)
  # terminal_ui が設定されたら、bookmark_manager と zoxide_integration にも渡す
  @bookmark_manager.set_terminal_ui(terminal_ui)
  @zoxide_integration.set_terminal_ui(terminal_ui)
end

#unfocus_preview_paneObject



349
350
351
# File 'lib/rufio/keybind_handler.rb', line 349

def unfocus_preview_pane
  @nav_controller.unfocus_preview_pane
end