Class: Mui::Fzf::Plugin

Inherits:
Plugin
  • Object
show all
Defined in:
lib/mui/fzf/plugin.rb

Overview

fzf integration plugin for Mui editor Provides :Fzf, :Rg, and :Ag commands for fuzzy file searching

Constant Summary collapse

FZF_CANCEL_EXIT_CODE =
130
FZF_PREVIEW_COMMAND =
"head -100 {}"
GREP_PREVIEW_COMMAND =

Preview command for grep results (file:line:content format) Extract filename with cut -d: -f1, line number with cut -d: -f2

"head -100 $(echo {} | cut -d: -f1)"

Instance Method Summary collapse

Instance Method Details

#build_ag_command(query) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/mui/fzf/plugin.rb', line 104

def build_ag_command(query)
  initial_query = escape_query(query)
  ag_cmd = "ag --color --nogroup"
  if initial_query.empty?
    # No initial query: start with empty list, search on input
    "echo '' | fzf --ansi --disabled " \
      "--bind 'change:reload:#{ag_cmd} {q} || true' " \
      "--preview '#{GREP_PREVIEW_COMMAND}'"
  else
    # With initial query: run ag first, then allow filtering
    "#{ag_cmd} '#{initial_query}' | " \
      "fzf --ansi --disabled --query '#{initial_query}' " \
      "--bind 'change:reload:#{ag_cmd} {q} || true' " \
      "--preview '#{GREP_PREVIEW_COMMAND}'"
  end
end

#build_file_list_command(list_cmd) ⇒ Object



83
84
85
# File 'lib/mui/fzf/plugin.rb', line 83

def build_file_list_command(list_cmd)
  "#{list_cmd} | fzf --preview '#{FZF_PREVIEW_COMMAND}'"
end

#build_fzf_commandObject



79
80
81
# File 'lib/mui/fzf/plugin.rb', line 79

def build_fzf_command
  "fzf --preview '#{FZF_PREVIEW_COMMAND}'"
end

#build_rg_command(query) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mui/fzf/plugin.rb', line 87

def build_rg_command(query)
  initial_query = escape_query(query)
  rg_cmd = "rg --color=always --line-number --no-heading"
  if initial_query.empty?
    # No initial query: start with empty list, search on input
    "echo '' | fzf --ansi --disabled " \
      "--bind 'change:reload:#{rg_cmd} {q} || true' " \
      "--preview '#{GREP_PREVIEW_COMMAND}'"
  else
    # With initial query: run rg first, then allow filtering
    "#{rg_cmd} '#{initial_query}' | " \
      "fzf --ansi --disabled --query '#{initial_query}' " \
      "--bind 'change:reload:#{rg_cmd} {q} || true' " \
      "--preview '#{GREP_PREVIEW_COMMAND}'"
  end
end

#execute_ag(context, args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/mui/fzf/plugin.rb', line 68

def execute_ag(context, args)
  unless context.command_exists?("ag")
    context.set_message("Error: ag (the_silver_searcher) is not installed")
    return
  end

  query = args&.strip || ""
  # Always use interactive grep mode (start empty or with initial query)
  execute_fzf(context, build_ag_command(query), grep_mode: true)
end

#execute_fzf(context, cmd, grep_mode: false) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/mui/fzf/plugin.rb', line 47

def execute_fzf(context, cmd, grep_mode: false)
  unless context.command_exists?("fzf")
    context.set_message("Error: fzf is not installed")
    return
  end

  result = context.run_interactive_command(cmd)
  handle_result(context, result, grep_mode:)
end

#execute_rg(context, args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/mui/fzf/plugin.rb', line 57

def execute_rg(context, args)
  unless context.command_exists?("rg")
    context.set_message("Error: rg (ripgrep) is not installed")
    return
  end

  query = args&.strip || ""
  # Always use interactive grep mode (start empty or with initial query)
  execute_fzf(context, build_rg_command(query), grep_mode: true)
end

#setupObject



16
17
18
19
20
# File 'lib/mui/fzf/plugin.rb', line 16

def setup
  register_fzf_command
  register_rg_command
  register_ag_command
end