tmux-fingers/src/fingers/view.cr

121 lines
2.4 KiB
Crystal
Raw Normal View History

2020-05-02 11:45:56 +02:00
require "../tmux"
require "./hinter"
require "./state"
require "./action_runner"
module Fingers
class View
CLEAR_SEQ = "\e[H\e[J"
2023-09-27 14:01:48 +02:00
HIDE_CURSOR_SEQ = "\e[?25l"
2020-05-02 11:45:56 +02:00
@hinter : Hinter
@state : State
@output : Printer
@original_pane : Tmux::Pane
@tmux : Tmux
2023-11-10 10:13:49 +01:00
@mode : String
2020-05-02 11:45:56 +02:00
def initialize(
@hinter,
@output,
@original_pane,
@state,
2023-11-10 10:13:49 +01:00
@tmux,
@mode
2020-05-02 11:45:56 +02:00
)
end
def render
clear_screen
hide_cursor
hinter.run
end
def process_input(input : String)
command, *args = input.split(":")
case command
when "hint"
char, modifier = args
process_hint(char, modifier)
when "exit"
request_exit!
when "toggle-help"
when "toggle-multi-mode"
process_multimode
when "fzf"
# soon
end
end
def run_action
2023-11-10 10:13:49 +01:00
match = hinter.lookup(state.input)
2020-05-02 11:45:56 +02:00
ActionRunner.new(
hint: state.input,
modifier: state.modifier,
match: state.result,
2023-11-10 10:13:49 +01:00
original_pane: original_pane,
offset: match ? match.not_nil!.offset : nil,
mode: mode
2020-05-02 11:45:56 +02:00
).run
tmux.display_message("Copied: #{state.result}", 1000) if should_notify?
2020-05-02 11:45:56 +02:00
end
private def hide_cursor
output.print HIDE_CURSOR_SEQ
end
private def clear_screen
output.print CLEAR_SEQ
end
private def process_hint(char, modifier)
state.input += char
state.modifier = modifier
2023-11-10 10:13:49 +01:00
2020-05-02 11:45:56 +02:00
match = hinter.lookup(state.input)
2023-11-10 10:13:49 +01:00
if match.nil?
render
2023-11-10 10:13:49 +01:00
else
handle_match(match.not_nil!.text)
end
2020-05-02 11:45:56 +02:00
end
private def process_multimode
prev_state = state.multi_mode
state.multi_mode = !state.multi_mode
current_state = state.multi_mode
if prev_state == true && current_state == false
state.result = state.multi_matches.join(' ')
request_exit!
end
end
2023-11-10 10:13:49 +01:00
private getter :output, :hinter, :original_pane, :state, :tmux, :mode
2020-05-02 11:45:56 +02:00
private def handle_match(match)
if state.multi_mode
state.multi_matches << match
state.selected_hints << state.input
state.input = ""
render
else
state.result = match
request_exit!
end
end
private def request_exit!
state.exiting = true
end
private def should_notify?
!state.result.empty? && Fingers.config.show_copied_notification == "1"
end
2020-05-02 11:45:56 +02:00
end
end