Class: Yatte::Editor

Inherits:
Object
  • Object
show all
Defined in:
lib/yatte/editor.rb

Constant Summary collapse

SHIFT_MOVEMENT_KEYS =
%i[
  shift_arrow_up shift_arrow_down shift_arrow_left shift_arrow_right
].freeze
MOVEMENT_KEYS =
%i[
  arrow_up arrow_down arrow_left arrow_right
  home end_key ctrl_arrow_left ctrl_arrow_right
  ctrl_home ctrl_end page_up page_down
].freeze
EDIT_KEYS =
%i[enter backspace delete tab].freeze

Instance Method Summary collapse

Constructor Details

#initialize(terminal: Terminal.new, filename: nil, filenames: nil, directory: nil) ⇒ Editor

Returns a new instance of Editor.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/yatte/editor.rb', line 17

def initialize(terminal: Terminal.new, filename: nil, filenames: nil,
  directory: nil)
  @terminal = terminal
  @screen = Screen.new(@terminal)
  @input = Input.new(@terminal)
  @tab_manager = TabManager.new
  @message = nil
  @message_time = nil
  @directory = directory || "."
  @file_finder = nil
  @recovery = RecoveryController.new(
    input: @input, status: method(:status_update)
  )
  @quit_controller = QuitController.new(status: method(:set_message))
  @open_finder_on_start = false

  files = filenames || (filename ? [filename] : [])
  if files.empty?
    @tab_manager.open_empty
    @open_finder_on_start = !!directory
  else
    files.each { |name| @tab_manager.open(name) }
    @tab_manager.activate(0)
  end
end

Instance Method Details

#process_keypress(key) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/yatte/editor.rb', line 67

def process_keypress(key)
  @quit_controller.reset unless key == :ctrl_q || key == :ctrl_w

  if MOVEMENT_KEYS.include?(key)
    selection.clear if selection.active?
    return dispatch_movement(key)
  end

  if SHIFT_MOVEMENT_KEYS.include?(key)
    start_or_extend_selection
    dispatch_shift_movement(key)
    return nil
  end

  if edit_key?(key)
    dispatch_edit(key)
    @recovery.record_edit
    return nil
  end

  case key
  when :ctrl_q then return handle_quit
  when :ctrl_s then save_file
  when :ctrl_g then jump_to_line
  when :ctrl_f then incremental_search
  when :ctrl_a then select_all
  when :ctrl_c then copy_selection
  when :ctrl_x then cut_selection
  when :ctrl_v then paste
  when :ctrl_z then undo
  when :ctrl_y then redo_action
  when :ctrl_h then find_and_replace
  when :ctrl_t then open_file_finder
  when :ctrl_p then open_project_search
  when :ctrl_w then close_tab
  when :alt_arrow_right then @tab_manager.next_tab
  when :alt_arrow_left then @tab_manager.prev_tab
  end

  nil
end

#runObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/yatte/editor.rb', line 43

def run
  @terminal.raw_mode do
    @recovery.prompt_recovery(@tab_manager.tabs)
    if @open_finder_on_start
      refresh_screen(force: true)
      open_file_finder
    end
    loop do
      @recovery.maybe_save(filename, buffer)
      refresh_screen
      key = @input.read_keypress
      break if process_keypress(key) == :quit

      # Batch any queued input before next render
      while (queued = @input.read_keypress_nonblocking)
        break if process_keypress(queued) == :quit
      end
    end
  end
ensure
  @recovery.cleanup(@tab_manager.tabs)
  @terminal.write("\x1b[2J#{Yatte::HOME}#{Yatte::SHOW_CURSOR}")
end