Class: Rufio::ZoxideIntegration

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

Overview

Integrates with zoxide for directory history navigation

Constant Summary collapse

DIALOG_WIDTH =

Dialog size constants

45
DIALOG_BORDER_HEIGHT =
4

Instance Method Summary collapse

Constructor Details

#initialize(dialog_renderer = nil) ⇒ ZoxideIntegration

Returns a new instance of ZoxideIntegration.



12
13
14
15
# File 'lib/rufio/zoxide_integration.rb', line 12

def initialize(dialog_renderer = nil)
  @dialog_renderer = dialog_renderer
  @terminal_ui = nil
end

Instance Method Details

#add_to_history(path) ⇒ Boolean

Add directory to zoxide history

Parameters:

  • path (String)

    Directory path

Returns:

  • (Boolean)

    Success status



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rufio/zoxide_integration.rb', line 77

def add_to_history(path)
  return false unless available?
  return false unless Dir.exist?(path)

  begin
    system("zoxide add #{Shellwords.escape(path)} > /dev/null 2>&1")
    true
  rescue StandardError
    false
  end
end

#available?Boolean

Check if zoxide is available

Returns:

  • (Boolean)


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

def available?
  system('which zoxide > /dev/null 2>&1')
end

#get_historyArray<Hash>

Get zoxide history

Returns:

  • (Array<Hash>)

    Array of { path: String, score: Float }



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
# File 'lib/rufio/zoxide_integration.rb', line 30

def get_history
  return [] unless available?

  begin
    # Get zoxide history with scores
    output = `zoxide query --list --score 2>/dev/null`.strip
    return [] if output.empty?

    # Parse each line into path and score
    lines = output.split("\n")
    history = lines.map do |line|
      # zoxide output format: "score path"
      if line.match(/^\s*(\d+(?:\.\d+)?)\s+(.+)$/)
        score = ::Regexp.last_match(1).to_f
        path = ::Regexp.last_match(2).strip
        { path: path, score: score }
      else
        # No score (backward compatibility)
        { path: line.strip, score: 0.0 }
      end
    end

    # Filter to only existing directories
    history.select { |entry| Dir.exist?(entry[:path]) }
  rescue StandardError
    []
  end
end

#set_terminal_ui(terminal_ui) ⇒ Object

terminal_ui を設定



18
19
20
# File 'lib/rufio/zoxide_integration.rb', line 18

def set_terminal_ui(terminal_ui)
  @terminal_ui = terminal_ui
end

#show_menuString?

Show zoxide history menu and let user select

Returns:

  • (String, nil)

    Selected path or nil if cancelled



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rufio/zoxide_integration.rb', line 61

def show_menu
  return nil unless @dialog_renderer

  history = get_history

  if history.empty?
    show_no_history_message
    return nil
  end

  select_from_history(history)
end