Class: Fatty::SearchSession

Inherits:
Session
  • Object
show all
Defined in:
lib/fatty/session/search_session.rb

Constant Summary collapse

DEFAULT_SEARCH_HISTORY_FILE =
File.expand_path("~/.fatty_search_history")
DEFAULT_SEARCH_HISTORY_MAX =
200

Instance Attribute Summary collapse

Attributes inherited from Session

#counter, #keymap, #terminal, #views

Instance Method Summary collapse

Methods inherited from Session

#add_view, #close, #handle_resize, #init, #inspect, #persist!, #resolve_action, #tick, #update

Methods included from Actionable

included

Constructor Details

#initialize(direction: :backward, regex: false, history: nil, prefix: nil) ⇒ SearchSession

Returns a new instance of SearchSession.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fatty/session/search_session.rb', line 12

def initialize(direction: :backward, regex: false, history: nil, prefix: nil)
  super(keymap: Keymaps.emacs, views: [])

  @direction = direction.to_sym
  @regex = !!regex

  prompt = search_prompt(direction: @direction, regex: @regex)

  @field = Fatty::InputField.new(
    prompt: prompt,
    history: history,
    history_kind: -> { @regex ? :search_regex : :search_string },
  )
  text = prefix.to_s
  @field.buffer.replace(text) unless text.empty?
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



7
8
9
# File 'lib/fatty/session/search_session.rb', line 7

def direction
  @direction
end

#fieldObject (readonly)

Returns the value of attribute field.



7
8
9
# File 'lib/fatty/session/search_session.rb', line 7

def field
  @field
end

#regexObject (readonly)

Returns the value of attribute regex.



7
8
9
# File 'lib/fatty/session/search_session.rb', line 7

def regex
  @regex
end

Instance Method Details

#cancel!Object



102
103
104
# File 'lib/fatty/session/search_session.rb', line 102

def cancel!
  [[:terminal, :pop_modal]]
end

#handle_action(action, args, event:) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fatty/session/search_session.rb', line 78

def handle_action(action, args, event:)
  env = ActionEnvironment.new(
    session: self,
    counter: counter,
    event: event,
    buffer: @field.buffer,
    field: @field,
  )

  if Fatty::Actions.lookup(action)&.fetch(:on) == :session
    Fatty::Actions.call(action, env, *args)
  else
    @field.act_on(action, *args, env: env)
  end
rescue Fatty::ActionError
  []
end

#keymap_contextsObject

Framework and Session Hooks



33
34
35
# File 'lib/fatty/session/search_session.rb', line 33

def keymap_contexts
  [:search, :text, :terminal]
end

#search_acceptObject



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fatty/session/search_session.rb', line 119

def search_accept
  pattern = @field.accept_line.to_s

  cmds = []
  cmds << [
    :terminal,
    :send_modal_owner,
    [:cmd, :pager_search_set, { pattern: pattern, direction: direction, regex: regex }]
  ]
  cmds << [:terminal, :pop_modal]
  cmds
end

#search_prompt(direction:, regex:) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fatty/session/search_session.rb', line 106

def search_prompt(direction:, regex:)
  label =
    if regex
      "Regex: "
    else
      "Search string: "
    end

  # If you want a subtle direction hint right at the prompt:
  dir = (direction == :backward ? "" : "")
  dir + label
end

#step_search(dir) ⇒ Object



132
133
134
# File 'lib/fatty/session/search_session.rb', line 132

def step_search(dir)
  [[:terminal, :send_modal_owner, [:cmd, :pager_search_step, { direction: dir }]]]
end

#toggle_regexObject



96
97
98
99
100
# File 'lib/fatty/session/search_session.rb', line 96

def toggle_regex
  @regex = !@regex
  @field.prompt = search_prompt(direction: @direction, regex: @regex)
  []
end

#view(screen:, renderer:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fatty/session/search_session.rb', line 37

def view(screen:, renderer:)
  row = screen.output_rect.rows - 1

  ::Curses.curs_set(1)
  renderer.render_pager_field(
    @field,
    row: row,
    role: :search_input,
  )
  renderer.restore_output_cursor(@field, row: row)
end