Class: Echoes::CopyMode

Inherits:
Object
  • Object
show all
Defined in:
lib/echoes/copy_mode.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(screen) ⇒ CopyMode

Returns a new instance of CopyMode.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/echoes/copy_mode.rb', line 8

def initialize(screen)
  @screen = screen
  @active = false
  @cursor_row = 0
  @cursor_col = 0
  @selection_start = nil
  @selection_end = nil
  @search_query = nil
  @search_direction = :forward
  @pending_find = nil   # :f, :F, :t, :T
  @last_find = nil      # [direction, char] for ; and ,
  @line_selection = false
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



5
6
7
# File 'lib/echoes/copy_mode.rb', line 5

def active
  @active
end

#cursor_colObject (readonly)

Returns the value of attribute cursor_col.



5
6
7
# File 'lib/echoes/copy_mode.rb', line 5

def cursor_col
  @cursor_col
end

#cursor_rowObject (readonly)

Returns the value of attribute cursor_row.



5
6
7
# File 'lib/echoes/copy_mode.rb', line 5

def cursor_row
  @cursor_row
end

#selection_endObject (readonly)

Returns the value of attribute selection_end.



6
7
8
# File 'lib/echoes/copy_mode.rb', line 6

def selection_end
  @selection_end
end

#selection_startObject (readonly)

Returns the value of attribute selection_start.



6
7
8
# File 'lib/echoes/copy_mode.rb', line 6

def selection_start
  @selection_start
end

Instance Method Details

#absolute_cursor_rowObject

Absolute row in the combined scrollback+grid buffer



196
197
198
# File 'lib/echoes/copy_mode.rb', line 196

def absolute_cursor_row
  @screen.scrollback.size + @cursor_row
end

#enterObject



22
23
24
25
26
27
28
29
# File 'lib/echoes/copy_mode.rb', line 22

def enter
  @active = true
  @cursor_row = @screen.cursor.row
  @cursor_col = @screen.cursor.col
  @selection_start = nil
  @selection_end = nil
  @search_query = nil
end

#exitObject



31
32
33
34
35
36
37
# File 'lib/echoes/copy_mode.rb', line 31

def exit
  @active = false
  @selection_start = nil
  @selection_end = nil
  @line_selection = false
  @search_query = nil
end

#handle_key(key) ⇒ Object

Handle a key in copy mode. Returns :exit if copy mode should end, :yank if text was yanked, nil otherwise.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/echoes/copy_mode.rb', line 45

def handle_key(key)
  # Pending f/F/t/T — next key is the target character
  if @pending_find
    execute_find(@pending_find, key)
    @pending_find = nil
    update_selection_end if selecting?
    return nil
  end

  case key
  # Basic movement
  when 'h'
    move_cursor(0, -1)
  when 'j'
    move_cursor(1, 0)
  when 'k'
    move_cursor(-1, 0)
  when 'l'
    move_cursor(0, 1)

  # Line positions
  when '0'
    @cursor_col = 0
  when '^'
    move_first_non_blank
  when '$'
    move_end_of_line

  # Word motions (vim-style: word = keyword chars)
  when 'w'
    move_word_forward
  when 'e'
    move_word_end_forward
  when 'b'
    move_word_backward

  # WORD motions (whitespace-delimited)
  when 'W'
    move_bigword_forward
  when 'E'
    move_bigword_end_forward
  when 'B'
    move_bigword_backward

  # Find in line
  when 'f'
    @pending_find = :f
  when 'F'
    @pending_find = :F
  when 't'
    @pending_find = :t
  when 'T'
    @pending_find = :T
  when ';'
    repeat_find(:same)
  when ','
    repeat_find(:reverse)

  # Screen-relative jumps
  when 'H'
    @cursor_row = visible_top_row
  when 'M'
    @cursor_row = visible_top_row + @screen.rows / 2
  when 'L'
    @cursor_row = visible_top_row + @screen.rows - 1

  # Document jumps
  when 'g'
    @cursor_row = -@screen.scrollback.size
    @cursor_col = 0
  when 'G'
    @cursor_row = @screen.rows - 1
    @cursor_col = 0

  # Paragraph motions
  when '{'
    move_paragraph_backward
  when '}'
    move_paragraph_forward

  # Scrolling
  when "\x15" # Ctrl-U
    move_cursor(-(@screen.rows / 2), 0)
  when "\x04" # Ctrl-D
    move_cursor(@screen.rows / 2, 0)
  when "\x02" # Ctrl-B
    move_cursor(-@screen.rows, 0)
  when "\x06" # Ctrl-F
    move_cursor(@screen.rows, 0)

  # Search
  when '/'
    @search_query = +""
    @search_direction = :forward
  when '?'
    @search_query = +""
    @search_direction = :backward
  when 'n'
    search_next
  when 'N'
    search_prev

  # Selection & yank
  when 'v'
    start_selection
  when 'V'
    start_line_selection
  when 'y'
    return :yank if selecting?
  when 'q', "\e"
    self.exit
    return :exit
  end
  update_selection_end if selecting?
  nil
end

#scroll_offset_for_cursorObject

Scroll offset needed to keep cursor visible



187
188
189
190
191
192
193
# File 'lib/echoes/copy_mode.rb', line 187

def scroll_offset_for_cursor
  if @cursor_row < 0
    @cursor_row.abs
  else
    0
  end
end

#selected_textObject

Extract text from selection range



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/echoes/copy_mode.rb', line 163

def selected_text
  return "" unless @selection_start && @selection_end

  start_pos, end_pos = [@selection_start, @selection_end].sort_by { |p| [p[0], p[1]] }
  sr, sc = start_pos
  er, ec = end_pos

  lines = []
  (sr..er).each do |row|
    line = row_text(row)
    if row == sr && row == er
      lines << line[sc..ec]
    elsif row == sr
      lines << line[sc..]
    elsif row == er
      lines << line[0..ec]
    else
      lines << line
    end
  end
  lines.join("\n")
end

#selecting?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/echoes/copy_mode.rb', line 39

def selecting?
  @selection_start != nil
end