Class: Rufio::UIRenderer

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

Overview

UIレンダリング専用クラスTerminalUI から draw_*_to_buffer 系メソッドを分離し、単一責任原則に準拠

  • ディレクトリリスト・ファイルプレビュー・フッター・タブ描画を担当

  • キャッシュ(プレビュー・ブックマーク)を管理

  • シンタックスハイライト(bat 連携)を担当

Constant Summary collapse

2
CONTENT_START_LINE =

Header(1行) + Footer(1行) 分のマージン

1
CURSOR_OFFSET =

コンテンツ開始行(フッタ1行: Y=0)

1
ICON_SIZE_PADDING =

カーソル位置のオフセット

12
BOOKMARK_HIGHLIGHT_DURATION =

アイコン、選択マーク、サイズ情報分

0.5
TAB_SEPARATOR =

ブックマークハイライト表示時間(秒)

">"
KILOBYTE =

File display constants

1024
MEGABYTE =
KILOBYTE * 1024
GIGABYTE =
MEGABYTE * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(screen_width:, screen_height:, keybind_handler: nil, directory_listing: nil, file_preview: nil, background_executor: nil, test_mode: false, left_panel_ratio: 0.5, preview_enabled: true) ⇒ UIRenderer

Returns a new instance of UIRenderer.



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rufio/ui_renderer.rb', line 34

def initialize(screen_width:, screen_height:,
               keybind_handler: nil, directory_listing: nil,
               file_preview: nil, background_executor: nil,
               test_mode: false,
               left_panel_ratio: 0.5,
               preview_enabled: true)
  @screen_width = screen_width
  @screen_height = screen_height
  @keybind_handler = keybind_handler
  @directory_listing = directory_listing
  @file_preview = file_preview
  @background_executor = background_executor
  @test_mode = test_mode
  @left_panel_ratio = left_panel_ratio
  @preview_enabled = preview_enabled

  # Preview cache
  @preview_cache = {}
  @last_preview_path = nil

  # Syntax highlighter(bat が利用可能な場合のみ動作)
  @syntax_highlighter = SyntaxHighlighter.new
  @highlight_updated = false

  # Bookmark cache(毎フレームのファイルI/Oを回避)
  @cached_bookmarks = nil
  @cached_bookmark_time = nil
  @bookmark_cache_ttl = 1.0

  # Bookmark highlight (Tab ジャンプ時に 500ms ハイライト)
  @highlighted_bookmark_index = nil
  @highlighted_bookmark_time = nil

  # Completion lamp
  @completion_lamp_message = nil
  @completion_lamp_time = nil

  # Tab mode manager
  @tab_mode_manager = TabModeManager.new
end

Instance Attribute Details

#background_executorObject

Returns the value of attribute background_executor.



26
27
28
# File 'lib/rufio/ui_renderer.rb', line 26

def background_executor
  @background_executor
end

#completion_lamp_messageObject

Returns the value of attribute completion_lamp_message.



27
28
29
# File 'lib/rufio/ui_renderer.rb', line 27

def completion_lamp_message
  @completion_lamp_message
end

#completion_lamp_timeObject

Returns the value of attribute completion_lamp_time.



27
28
29
# File 'lib/rufio/ui_renderer.rb', line 27

def completion_lamp_time
  @completion_lamp_time
end

#directory_listingObject

Returns the value of attribute directory_listing.



25
26
27
# File 'lib/rufio/ui_renderer.rb', line 25

def directory_listing
  @directory_listing
end

#file_previewObject

Returns the value of attribute file_preview.



25
26
27
# File 'lib/rufio/ui_renderer.rb', line 25

def file_preview
  @file_preview
end

#highlight_updatedObject (readonly)

Returns the value of attribute highlight_updated.



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

def highlight_updated
  @highlight_updated
end

#keybind_handlerObject

Returns the value of attribute keybind_handler.



25
26
27
# File 'lib/rufio/ui_renderer.rb', line 25

def keybind_handler
  @keybind_handler
end

#left_panel_ratioObject (readonly)

Returns the value of attribute left_panel_ratio.



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

def left_panel_ratio
  @left_panel_ratio
end

#tab_mode_managerObject (readonly)

Returns the value of attribute tab_mode_manager.



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

def tab_mode_manager
  @tab_mode_manager
end

#test_modeObject

Returns the value of attribute test_mode.



26
27
28
# File 'lib/rufio/ui_renderer.rb', line 26

def test_mode
  @test_mode
end

Instance Method Details

#bookmark_highlight_expired?Boolean

ブックマークハイライトが期限切れかどうか

Returns:

  • (Boolean)


108
109
110
111
112
# File 'lib/rufio/ui_renderer.rb', line 108

def bookmark_highlight_expired?
  return false unless @highlighted_bookmark_index && @highlighted_bookmark_time

  (Time.now - @highlighted_bookmark_time) >= BOOKMARK_HIGHLIGHT_DURATION
end

#clear_bookmark_cacheObject



84
85
86
87
# File 'lib/rufio/ui_renderer.rb', line 84

def clear_bookmark_cache
  @cached_bookmarks = nil
  @cached_bookmark_time = nil
end

#clear_highlighted_bookmarkObject



102
103
104
105
# File 'lib/rufio/ui_renderer.rb', line 102

def clear_highlighted_bookmark
  @highlighted_bookmark_index = nil
  @highlighted_bookmark_time = nil
end

#clear_preview_cacheObject

Cache management



79
80
81
82
# File 'lib/rufio/ui_renderer.rb', line 79

def clear_preview_cache
  @preview_cache.clear
  @last_preview_path = nil
end

#draw_directory_list_to_buffer(screen, entries, width, height) ⇒ Object

ディレクトリリスト描画



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/rufio/ui_renderer.rb', line 243

def draw_directory_list_to_buffer(screen, entries, width, height)
  start_index = [@keybind_handler.current_index - height / 2, 0].max

  (0...height).each do |i|
    entry_index = start_index + i
    line_num = i + CONTENT_START_LINE

    if entry_index < entries.length
      entry = entries[entry_index]
      is_selected = entry_index == @keybind_handler.current_index

      draw_entry_line_to_buffer(screen, entry, width, is_selected, 0, line_num)
    else
      # 空行
      safe_width = [width - CURSOR_OFFSET, (@screen_width * @left_panel_ratio).to_i - CURSOR_OFFSET].min
      screen.put_string(0, line_num, ' ' * safe_width)
    end
  end
end

#draw_entry_line_to_buffer(screen, entry, width, is_selected, x, y) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/rufio/ui_renderer.rb', line 263

def draw_entry_line_to_buffer(screen, entry, width, is_selected, x, y)
  # アイコンと色の設定
  icon, color = get_entry_display_info(entry)

  # 左ペイン専用の安全な幅を計算
  safe_width = [width - CURSOR_OFFSET, (@screen_width * @left_panel_ratio).to_i - CURSOR_OFFSET].min

  # 選択マークの追加
  selection_mark = @keybind_handler.is_selected?(entry[:name]) ? "" : "  "

  # ファイル名(必要に応じて切り詰め)
  name = entry[:name]
  max_name_length = safe_width - ICON_SIZE_PADDING
  name = name[0...max_name_length - 3] + '...' if max_name_length > 0 && name.length > max_name_length

  # サイズ情報
  size_info = format_size(entry[:size])

  # 行の内容を構築
  content_without_size = "#{selection_mark}#{icon} #{name}"
  available_for_content = safe_width - size_info.length

  line_content = if available_for_content > 0
                   content_without_size.ljust(available_for_content) + size_info
                 else
                   content_without_size
                 end

  # 確実に safe_width を超えないよう切り詰め
  line_content = line_content[0...safe_width]

  # 色を決定
  if is_selected
    fg_color = ColorHelper.color_to_selected_ansi(ConfigLoader.colors[:selected])
    screen.put_string(x, y, line_content, fg: fg_color)
  elsif @keybind_handler.is_selected?(entry[:name])
    # 選択されたアイテムは緑背景、黒文字
    screen.put_string(x, y, line_content, fg: "\e[42m\e[30m")
  else
    screen.put_string(x, y, line_content, fg: color)
  end
end

#draw_file_preview_to_buffer(screen, selected_entry, width, height, left_offset) ⇒ Object

ファイルプレビュー描画



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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/rufio/ui_renderer.rb', line 310

def draw_file_preview_to_buffer(screen, selected_entry, width, height, left_offset)
  # 事前計算
  cursor_position = left_offset + CURSOR_OFFSET
  max_chars_from_cursor = @screen_width - cursor_position
  safe_width = [max_chars_from_cursor - 2, width - 2, 0].max

  # プレビューコンテンツをキャッシュから取得
  preview_content = nil
  wrapped_lines = nil
  highlighted_wrapped_lines = nil

  if selected_entry && selected_entry[:type] == 'file'
    if @last_preview_path != selected_entry[:path]
      full_preview = @file_preview.preview_file(selected_entry[:path])
      preview_content = extract_preview_lines(full_preview)
      @preview_cache[selected_entry[:path]] = {
        content: preview_content,
        preview_data: full_preview,
        highlighted: nil,
        wrapped: {},
        highlighted_wrapped: {}
      }
      @last_preview_path = selected_entry[:path]
    else
      cache_entry = @preview_cache[selected_entry[:path]]
      preview_content = cache_entry[:content] if cache_entry
    end

    # bat が利用可能な場合はシンタックスハイライトを取得(非同期)
    if @syntax_highlighter&.available? && preview_content
      cache_entry = @preview_cache[selected_entry[:path]]
      if cache_entry
        preview_data = cache_entry[:preview_data]
        if preview_data && preview_data[:type] == 'code' && preview_data[:encoding] == 'UTF-8'
          if cache_entry[:highlighted].nil?
            cache_entry[:highlighted] = false
            file_path = selected_entry[:path]
            @syntax_highlighter.highlight_async(file_path) do |lines|
              if (ce = @preview_cache[file_path])
                ce[:highlighted] = lines
                ce[:highlighted_wrapped] = {}
              end
              @highlight_updated = true
            end
          end

          highlighted = cache_entry[:highlighted]
          if highlighted.is_a?(Array) && !highlighted.empty? && safe_width > 0
            if cache_entry[:highlighted_wrapped][safe_width]
              highlighted_wrapped_lines = cache_entry[:highlighted_wrapped][safe_width]
            else
              hl_wrapped = highlighted.flat_map do |hl_line|
                tokens = AnsiLineParser.parse(hl_line)
                tokens.empty? ? [[]] : AnsiLineParser.wrap(tokens, safe_width - 1)
              end
              cache_entry[:highlighted_wrapped][safe_width] = hl_wrapped
              highlighted_wrapped_lines = hl_wrapped
            end
          end
        end
      end
    end

    # プレーンテキストの折り返し(ハイライトなしのフォールバック)
    if preview_content && safe_width > 0 && highlighted_wrapped_lines.nil?
      cache_entry = @preview_cache[selected_entry[:path]]
      if cache_entry && cache_entry[:wrapped][safe_width]
        wrapped_lines = cache_entry[:wrapped][safe_width]
      else
        wrapped_lines = TextUtils.wrap_preview_lines(preview_content, safe_width - 1)
        cache_entry[:wrapped][safe_width] = wrapped_lines if cache_entry
      end
    end
  end

  content_x = cursor_position + 1

  (0...height).each do |i|
    line_num = i + CONTENT_START_LINE

    # 区切り線
    screen.put(cursor_position, line_num, '')

    next if safe_width <= 0

    if selected_entry && i == 0
      # プレビューヘッダー
      header = " #{selected_entry[:name]} "
      header += "[PREVIEW MODE]" if @keybind_handler&.preview_focused?
      header = TextUtils.truncate_to_width(header, safe_width) if TextUtils.display_width(header) > safe_width
      remaining_space = safe_width - TextUtils.display_width(header)
      header += ' ' * remaining_space if remaining_space > 0
      screen.put_string(content_x, line_num, header)

    elsif i >= 2 && highlighted_wrapped_lines
      # シンタックスハイライト付きコンテンツ
      scroll_offset = @keybind_handler&.preview_scroll_offset || 0
      display_line_index = i - 2 + scroll_offset

      if display_line_index < highlighted_wrapped_lines.length
        draw_highlighted_line_to_buffer(screen, content_x, line_num,
                                        highlighted_wrapped_lines[display_line_index], safe_width)
      else
        screen.put_string(content_x, line_num, ' ' * safe_width)
      end

    elsif i >= 2 && wrapped_lines
      # プレーンテキストコンテンツ
      scroll_offset = @keybind_handler&.preview_scroll_offset || 0
      display_line_index = i - 2 + scroll_offset

      content_to_print = if display_line_index < wrapped_lines.length
                           " #{wrapped_lines[display_line_index] || ''}"
                         else
                           ' '
                         end
      content_to_print = TextUtils.truncate_to_width(content_to_print, safe_width) if TextUtils.display_width(content_to_print) > safe_width
      remaining_space = safe_width - TextUtils.display_width(content_to_print)
      content_to_print += ' ' * remaining_space if remaining_space > 0
      screen.put_string(content_x, line_num, content_to_print)

    else
      screen.put_string(content_x, line_num, ' ' * safe_width)
    end
  end
end

フッター描画



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/rufio/ui_renderer.rb', line 470

def draw_footer_to_buffer(screen, y, fps = nil)
  if @keybind_handler.filter_active?
    if @keybind_handler.instance_variable_get(:@filter_mode)
      help_text = "Filter mode: Type to filter, ESC to clear, Enter to apply, Backspace to delete"
    else
      help_text = "Filtered view active - Space to edit filter, ESC to clear filter"
    end
    footer_content = help_text.ljust(@screen_width)[0...@screen_width]
    screen.put_string(0, y, footer_content, fg: "\e[7m")
  else
    # ブックマークをキャッシュ(Tab移動と同じソース:keybind_handler経由で取得)
    current_time = Time.now
    if @cached_bookmarks.nil? || @cached_bookmark_time.nil? || (current_time - @cached_bookmark_time) > @bookmark_cache_ttl
      @cached_bookmarks = @keybind_handler.bookmark_list
      @cached_bookmark_time = current_time
    end
    bookmarks = @cached_bookmarks

    # 起動ディレクトリを取得
    start_dir = @directory_listing&.start_directory
    start_dir_name = if start_dir
                       File.basename(start_dir)
                     else
                       "start"
                     end

    # ブックマーク一覧を作成(0.起動dir を先頭に追加)
    bookmark_parts = ["0.#{start_dir_name}"]
    unless bookmarks.empty?
      bookmark_parts.concat(bookmarks.take(9).map.with_index(1) { |bm, idx| "#{idx}.#{bm[:name]}" })
    end
    bookmark_text = bookmark_parts.join("")

    # 右側の情報
    right_parts = []

    # ジョブ数を表示(ジョブがある場合のみ)
    if @keybind_handler.has_jobs?
      job_text = @keybind_handler.job_status_bar_text
      right_parts << "[#{job_text}]" if job_text
    end

    # バックグラウンドコマンドの実行状態をランプで表示
    if @background_executor
      if @background_executor.running?
        command_name = @background_executor.current_command || "処理中"
        right_parts << "\e[32m🔄\e[0m #{command_name}"
      elsif @completion_lamp_message && @completion_lamp_time
        if (Time.now - @completion_lamp_time) < 3.0
          right_parts << @completion_lamp_message
        else
          @completion_lamp_message = nil
          @completion_lamp_time = nil
        end
      end
    end

    # FPS表示(test modeの時のみ)
    right_parts << "#{fps.round(1)} FPS" if @test_mode && fps

    right_info = right_parts.join(" | ")

    # ブックマーク一覧を利用可能な幅に収める
    if right_info.empty?
      available_width = @screen_width
    else
      available_width = @screen_width - right_info.length - 3
    end
    if bookmark_text.length > available_width && available_width > 3
      bookmark_text = bookmark_text[0...available_width - 3] + "..."
    elsif available_width <= 3
      bookmark_text = ""
    end

    # フッタ全体を構築
    if right_info.empty?
      footer_content = bookmark_text.ljust(@screen_width)[0...@screen_width]
    else
      padding = @screen_width - bookmark_text.length - right_info.length
      footer_content = "#{bookmark_text}#{' ' * padding}#{right_info}"
      footer_content = footer_content.ljust(@screen_width)[0...@screen_width]
    end
    screen.put_string(0, y, footer_content, fg: "\e[90m")

    # Tab ジャンプ時:対象ブックマークを 500ms ハイライト(セカンドパス)
    if @highlighted_bookmark_index && !bookmark_highlight_expired? && available_width > 3
      highlight_idx = @highlighted_bookmark_index
      if highlight_idx < bookmark_parts.length
        separator_len = 3  # " │ "
        x_pos = bookmark_parts[0...highlight_idx].sum { |p| p.length + separator_len }
        part_text = bookmark_parts[highlight_idx]
        if x_pos < available_width
          visible_len = [part_text.length, available_width - x_pos].min
          screen.put_string(x_pos, y, part_text[0...visible_len], fg: "\e[1;36m")
        end
      end
    end
  end
end

#draw_highlighted_line_to_buffer(screen, x, y, tokens, max_width) ⇒ Object

ハイライト済みトークン列を1行分 Screen バッファに描画する



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/rufio/ui_renderer.rb', line 438

def draw_highlighted_line_to_buffer(screen, x, y, tokens, max_width)
  current_x = x
  max_x = x + max_width

  # 先頭スペース
  if current_x < max_x
    screen.put(current_x, y, ' ')
    current_x += 1
  end

  # トークンを描画
  tokens&.each do |token|
    break if current_x >= max_x
    token[:text].each_char do |char|
      char_w = TextUtils.char_width(char)
      break if current_x + char_w > max_x
      screen.put(current_x, y, char, fg: token[:fg])
      current_x += char_w
    end
  end

  # 残りをスペースで埋める
  while current_x < max_x
    screen.put(current_x, y, ' ')
    current_x += 1
  end
end


740
741
742
743
744
745
746
747
748
749
750
751
752
# File 'lib/rufio/ui_renderer.rb', line 740

def draw_job_footer_to_buffer(screen, y, job_manager, log_mode: false)
  job_count = job_manager&.job_count || 0
  help_text = if log_mode
                "[ESC] Close Log | Jobs: #{job_count}"
              else
                "[Space] View Log | [x] Cancel | Jobs: #{job_count}"
              end
  footer_content = help_text.center(@screen_width)[0...@screen_width]

  footer_content.each_char.with_index do |char, x|
    screen.put(x, y, char, fg: "\e[30m", bg: "\e[47m")
  end
end

#draw_job_line_to_buffer(screen, job, is_selected, y) ⇒ Object



702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
# File 'lib/rufio/ui_renderer.rb', line 702

def draw_job_line_to_buffer(screen, job, is_selected, y)
  icon = job.status_icon
  name = job.name
  path = "(#{job.path})"
  duration = job.formatted_duration
  duration_text = duration.empty? ? "" : "[#{duration}]"

  status_text = case job.status
                when :running then "Running"
                when :completed then "Done"
                when :failed then "Failed"
                when :waiting then "Waiting"
                when :cancelled then "Cancelled"
                else ""
                end

  status_color = case job.status
                 when :running then "\e[33m"
                 when :completed then "\e[32m"
                 when :failed then "\e[31m"
                 else "\e[37m"
                 end

  line_content = "#{icon} #{name} #{path}".ljust(40)
  line_content += "#{duration_text.ljust(12)} #{status_text}"
  line_content = line_content[0...@screen_width].ljust(@screen_width)

  if is_selected
    line_content.each_char.with_index do |char, x|
      screen.put(x, y, char, fg: "\e[30m", bg: "\e[47m")
    end
  else
    line_content.each_char.with_index do |char, x|
      screen.put(x, y, char, fg: status_color)
    end
  end
end

#draw_job_list_to_buffer(screen, height, job_manager, job_mode_instance) ⇒ Object

ジョブモード描画



660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
# File 'lib/rufio/ui_renderer.rb', line 660

def draw_job_list_to_buffer(screen, height, job_manager, job_mode_instance)
  return unless job_manager

  # ログモード中は選択ジョブのログを表示
  if job_mode_instance&.log_mode?
    draw_job_log_to_buffer(screen, height, job_mode_instance.selected_job)
    return
  end

  jobs = job_manager.jobs
  selected_index = job_mode_instance&.selected_index || 0

  (0...height).each do |i|
    line_num = i + CONTENT_START_LINE

    if i < jobs.length
      job = jobs[i]
      draw_job_line_to_buffer(screen, job, i == selected_index, line_num)
    else
      screen.put_string(0, line_num, ' ' * @screen_width)
    end
  end
end

#draw_job_log_to_buffer(screen, height, job) ⇒ Object



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'lib/rufio/ui_renderer.rb', line 684

def draw_job_log_to_buffer(screen, height, job)
  unless job
    screen.put_string(0, CONTENT_START_LINE, 'No job selected'.ljust(@screen_width), fg: "\e[90m")
    return
  end

  log_lines = job.logs || []
  title = "=== Log: #{job.name} ==="
  screen.put_string(0, CONTENT_START_LINE, title.ljust(@screen_width), fg: "\e[1;36m")

  (0...height - 1).each do |i|
    line_num = i + CONTENT_START_LINE + 1
    line = log_lines[i] || ''
    line = line[0...@screen_width].ljust(@screen_width)
    screen.put_string(0, line_num, line, fg: "\e[37m")
  end
end

#draw_mode_tabs_to_buffer(screen, y) ⇒ Object

モードタブ描画



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# File 'lib/rufio/ui_renderer.rb', line 574

def draw_mode_tabs_to_buffer(screen, y)
  # タブモードマネージャの状態を同期
  sync_tab_mode_with_keybind_handler

  current_x = 0
  modes = @tab_mode_manager.available_modes
  labels = @tab_mode_manager.mode_labels
  keys = @tab_mode_manager.mode_keys
  current_mode = @tab_mode_manager.current_mode

  modes.each_with_index do |mode, index|
    key = keys[mode]
    label = key ? " #{key}:#{labels[mode]} " : " #{labels[mode]} "

    if mode == current_mode
      label.each_char do |char|
        screen.put(current_x, y, char, fg: "\e[30m\e[1m", bg: "\e[46m")
        current_x += 1
      end
    else
      label.each_char do |char|
        screen.put(current_x, y, char, fg: "\e[90m")
        current_x += 1
      end
    end

    # セパレータ
    if index < modes.length - 1
      if mode == current_mode
        screen.put(current_x, y, "\uE0B0", fg: "\e[36m")
      else
        screen.put(current_x, y, TAB_SEPARATOR, fg: "\e[90m")
      end
      current_x += 1
    end
  end

  # パスとバージョン情報を行末に追加
  current_path = @directory_listing.current_path
  version_str = " rufio v#{VERSION}"
  version_w = version_str.length

  remaining_w = @screen_width - current_x
  path_display_w = remaining_w - 2 - version_w

  if path_display_w >= 3
    arrow_fg = modes.last == current_mode ? "\e[36m" : "\e[90m"
    screen.put(current_x, y, TAB_SEPARATOR, fg: arrow_fg)
    current_x += 1

    path_end = @screen_width - 1 - version_w
    path_str = " #{shorten_path(current_path)} "
    path_str.each_char do |char|
      break if current_x >= path_end
      char_w = TextUtils.display_width(char)
      break if current_x + char_w > path_end
      screen.put(current_x, y, char, fg: "\e[90m")
      current_x += char_w
    end

    while current_x < path_end
      screen.put(current_x, y, ' ')
      current_x += 1
    end

    screen.put(current_x, y, "\uE0B2", fg: "\e[36m")
    current_x += 1

    version_str.each_char do |char|
      break if current_x >= @screen_width
      screen.put(current_x, y, char, fg: "\e[30m\e[1m", bg: "\e[46m")
      current_x += 1
    end
  end

  # 残りをスペースで埋める
  while current_x < @screen_width
    screen.put(current_x, y, ' ')
    current_x += 1
  end
end

#draw_screen(screen, notification_message: nil, fps: nil, in_job_mode: false, job_manager: nil, job_mode_instance: nil) ⇒ Object

Screen バッファに全体を描画する

Parameters:

  • screen (Screen)

    描画対象のスクリーンバッファ

  • notification_message (String, nil) (defaults to: nil)

    通知メッセージ

  • fps (Float, nil) (defaults to: nil)

    FPS(テストモード時のみ表示)

  • in_job_mode (Boolean) (defaults to: false)

    ジョブモード中かどうか

  • job_manager (JobManager, nil) (defaults to: nil)

    ジョブマネージャー

  • job_mode_instance (JobMode, nil) (defaults to: nil)

    ジョブモードインスタンス



125
126
127
128
129
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
# File 'lib/rufio/ui_renderer.rb', line 125

def draw_screen(screen, notification_message: nil, fps: nil,
                in_job_mode: false, job_manager: nil, job_mode_instance: nil)
  content_height = @screen_height - HEADER_FOOTER_MARGIN

  if in_job_mode
    # ジョブモード: フッタ y=0(上部)、コンテンツ y=1〜h-2、統合行 y=h-1(下部)
    log_mode = job_mode_instance&.log_mode? || false
    draw_job_footer_to_buffer(screen, 0, job_manager, log_mode: log_mode)
    draw_job_list_to_buffer(screen, content_height, job_manager, job_mode_instance)
    draw_mode_tabs_to_buffer(screen, @screen_height - 1)
  else
    # 通常モード: フッタ y=0(上部)、コンテンツ y=1〜h-2、統合行 y=h-1(下部)
    draw_footer_to_buffer(screen, 0, fps)

    entries = get_display_entries
    selected_entry = entries[@keybind_handler.current_index]

    left_width = (@screen_width * @left_panel_ratio).to_i
    right_width = @screen_width - left_width

    draw_directory_list_to_buffer(screen, entries, left_width, content_height)
    draw_file_preview_to_buffer(screen, selected_entry, right_width, content_height, left_width)

    draw_mode_tabs_to_buffer(screen, @screen_height - 1)
  end

  # 通知メッセージがある場合は表示
  if notification_message
    notification_line = @screen_height - 1
    message_display = " #{notification_message} "
    message_display = message_display[0...(@screen_width - 3)] + "..." if message_display.length > @screen_width
    screen.put_string(0, notification_line, message_display.ljust(@screen_width), fg: "\e[7m")
  end
end

#draw_screen_to_buffer(screen, notification_message = nil, fps = nil, in_job_mode: false, job_manager: nil, job_mode_instance: nil) ⇒ Object

後方互換性のためのエイリアス(TerminalUI のシグネチャに合わせる)



161
162
163
164
165
166
167
168
169
# File 'lib/rufio/ui_renderer.rb', line 161

def draw_screen_to_buffer(screen, notification_message = nil, fps = nil,
                          in_job_mode: false, job_manager: nil, job_mode_instance: nil)
  draw_screen(screen,
              notification_message: notification_message,
              fps: fps,
              in_job_mode: in_job_mode,
              job_manager: job_manager,
              job_mode_instance: job_mode_instance)
end

#extract_preview_lines(preview) ⇒ Object

FilePreview の結果ハッシュからプレーンテキスト行を抽出する



224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/rufio/ui_renderer.rb', line 224

def extract_preview_lines(preview)
  case preview[:type]
  when 'text', 'code'
    preview[:lines]
  when 'binary'
    ["(#{ConfigLoader.message('file.binary_file')})", ConfigLoader.message('file.cannot_preview')]
  when 'error'
    ["#{ConfigLoader.message('file.error_prefix')}:", preview[:message]]
  else
    ["(#{ConfigLoader.message('file.cannot_preview')})"]
  end
rescue StandardError
  ["(#{ConfigLoader.message('file.preview_error')})"]
end

#format_size(size) ⇒ Object

ファイルサイズ表示



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rufio/ui_renderer.rb', line 175

def format_size(size)
  return '      ' if size == 0

  if size < KILOBYTE
    "#{size}B".rjust(6)
  elsif size < MEGABYTE
    "#{(size / KILOBYTE.to_f).round(1)}K".rjust(6)
  elsif size < GIGABYTE
    "#{(size / MEGABYTE.to_f).round(1)}M".rjust(6)
  else
    "#{(size / GIGABYTE.to_f).round(1)}G".rjust(6)
  end
end

#get_entry_display_info(entry) ⇒ Object

エントリ表示情報



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rufio/ui_renderer.rb', line 193

def get_entry_display_info(entry)
  colors = ConfigLoader.colors

  case entry[:type]
  when 'directory'
    color_code = ColorHelper.color_to_ansi(colors[:directory])
    ['📁', color_code]
  when 'executable'
    color_code = ColorHelper.color_to_ansi(colors[:executable])
    ['', color_code]
  else
    case File.extname(entry[:name]).downcase
    when '.rb'
      ['💎', "\e[31m"]  # 赤
    when '.js', '.ts'
      ['📜', "\e[33m"]  # 黄
    when '.txt', '.md'
      color_code = ColorHelper.color_to_ansi(colors[:file])
      ['📄', color_code]
    else
      color_code = ColorHelper.color_to_ansi(colors[:file])
      ['📄', color_code]
    end
  end
end

#highlight_updated?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/rufio/ui_renderer.rb', line 89

def highlight_updated?
  @highlight_updated
end

#preview_enabled?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/rufio/ui_renderer.rb', line 30

def preview_enabled?
  @preview_enabled
end

#reset_highlight_updatedObject



93
94
95
# File 'lib/rufio/ui_renderer.rb', line 93

def reset_highlight_updated
  @highlight_updated = false
end

#set_highlighted_bookmark(index) ⇒ Object



97
98
99
100
# File 'lib/rufio/ui_renderer.rb', line 97

def set_highlighted_bookmark(index)
  @highlighted_bookmark_index = index
  @highlighted_bookmark_time = Time.now
end