Class: Yatte::Editor

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

Constant Summary collapse

QUIT_CONFIRM_COUNT =
3
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

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
# 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
  @quit_count = QUIT_CONFIRM_COUNT
  @directory = directory || "."
  @file_finder = nil
  @edits_since_swap = 0
  @last_swap_time = monotonic_time
  @open_finder_on_start = false

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

Instance Method Details

#process_keypress(key) ⇒ Object



66
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
108
109
110
111
112
# File 'lib/yatte/editor.rb', line 66

def process_keypress(key)
  @quit_count = QUIT_CONFIRM_COUNT 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

  case key
  when String
    handle_insert_char(key)
    @edits_since_swap += 1
  when :enter
    handle_insert_newline
    @edits_since_swap += 1
  when :backspace
    handle_delete_backward
    @edits_since_swap += 1
  when :delete
    handle_delete_forward
    @edits_since_swap += 1
  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



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

def run
  @terminal.raw_mode do
    check_recovery
    if @open_finder_on_start
      refresh_screen(force: true)
      open_file_finder
    end
    loop do
      maybe_swap_save
      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
  cleanup_swap_files
  @terminal.write("\x1b[2J#{Yatte::HOME}#{Yatte::SHOW_CURSOR}")
end