Class: Hokusai::Blocks::Input

Inherits:
Hokusai::Block show all
Defined in:
ui/src/hokusai/blocks/input.rb

Instance Attribute Summary collapse

Attributes inherited from Hokusai::Block

#node, #provides, #publisher

Instance Method Summary collapse

Methods inherited from Hokusai::Block

#children, #children?, compile, computed, computed!, #draw, #draw_with, #dump, #emit, inject, inject!, #method_missing, mount, #on_resize, provide, provides, style, styles_get, template, template_from_file, template_get, #update, use, uses

Constructor Details

#initialize(**args) ⇒ Input

Returns a new instance of Input.



39
40
41
42
43
44
45
46
47
48
49
# File 'ui/src/hokusai/blocks/input.rb', line 39

def initialize(**args)
  super

  @table = ::Hokusai::Util::PieceTable.new(initial)
  @buffer = ""
  @buffer_count = 0
  @start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  @buffer_offset = 0
  @offset = 0
  @changed = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hokusai::Block

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



36
37
38
# File 'ui/src/hokusai/blocks/input.rb', line 36

def buffer
  @buffer
end

#buffer_countObject

Returns the value of attribute buffer_count.



36
37
38
# File 'ui/src/hokusai/blocks/input.rb', line 36

def buffer_count
  @buffer_count
end

#buffer_offsetObject

Returns the value of attribute buffer_offset.



36
37
38
# File 'ui/src/hokusai/blocks/input.rb', line 36

def buffer_offset
  @buffer_offset
end

#offsetObject

Returns the value of attribute offset.



36
37
38
# File 'ui/src/hokusai/blocks/input.rb', line 36

def offset
  @offset
end

#start_timeObject

Returns the value of attribute start_time.



36
37
38
# File 'ui/src/hokusai/blocks/input.rb', line 36

def start_time
  @start_time
end

#tableObject

Returns the value of attribute table.



36
37
38
# File 'ui/src/hokusai/blocks/input.rb', line 36

def table
  @table
end

Instance Method Details

#after_updatedObject



51
52
53
54
55
56
57
58
59
60
61
# File 'ui/src/hokusai/blocks/input.rb', line 51

def after_updated
  if @changed && !growable
    @table = ::Hokusai::Util::PieceTable.new(initial)
    @buffer = ""
    @buffer_count = 0
    @start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    @buffer_offset = 0
    @offset = 0
    @changed = false
  end
end

#cursor_offsetObject



174
175
176
# File 'ui/src/hokusai/blocks/input.rb', line 174

def cursor_offset
  offset + buffer.size
end

#dynamic_keypress_handle(event) ⇒ Object



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
161
162
# File 'ui/src/hokusai/blocks/input.rb', line 89

def dynamic_keypress_handle(event)
  return unless node.meta.focused
  case event
  when proc(&:ctrl), proc(&:super)
    if event.char == "z"
      table.undo
    end

    return
  else
    case event.key
    when :right
      flush

      self.offset += 1 unless offset + 1 > table.to_s.size
    when :left
      flush
      
      self.offset = offset - 1 <= 0 ? 0 : offset - 1
    when :down, :up
      # no op
    when :delete, :backspace
      flush

      if buffer_count > 0
        table.delete(offset, buffer_count)
      elsif offset > 0
        table.delete(offset - 1, 1)

        self.offset -= 1
      end

      self.buffer_count = 0

      return
    when :enter
      if buffer.size.zero?
        self.buffer_offset = offset
      end

      unless growable
        flush
        
        emit("change", table.to_s)
        @changed = true

        self.table = ::Hokusai::Util::PieceTable.new("")
        self.buffer_offset = 0
        self.offset = 0

        return
      end

      self.buffer += "\n"
    else
      if buffer_count > 0
        flush
        
        table.delete(offset, buffer_count)

        self.buffer_offset = offset
        self.buffer_count = 0
      end

      if buffer.size.zero?
        self.buffer_offset = offset
      end

      self.buffer += event.char
    end
  end

  emit("modified")
end

#flushObject



164
165
166
167
168
169
170
171
172
# File 'ui/src/hokusai/blocks/input.rb', line 164

def flush
  if buffer.size > 0
    table.insert(buffer, buffer_offset)

    self.offset = cursor_offset
    self.buffer_offset = 0
    self.buffer = ""
  end
end

#handle_select_offset(value) ⇒ Object



82
83
84
85
86
87
# File 'ui/src/hokusai/blocks/input.rb', line 82

def handle_select_offset(value)
  flush
  
  self.offset = value
  self.buffer_count = 0
end

#handle_selected(start, stop) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'ui/src/hokusai/blocks/input.rb', line 69

def handle_selected(start, stop)
  flush

  return if start.nil?

  self.offset = start
  self.buffer_count = 0

  return if stop.nil?

  self.buffer_count = (stop + 1) - start
end

#render(canvas) {|canvas| ... } ⇒ Object

Yields:

  • (canvas)


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'ui/src/hokusai/blocks/input.rb', line 193

def render(canvas)
  if !buffer.empty? && (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) > 10
    flush

    if table.pieces.size > 10
      self.table = Util::PieceTable.new(table.to_s)
    end

    self.start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  end

  draw do
    rect(canvas.x, canvas.y, canvas.width, canvas.height) do |command|
      command.color = background if background
      command.outline = outline if outline
      command.outline_color = outline_color if outline_color
      command.round = rounding if rounding

      canvas = command.trim_canvas(canvas)
    end
  end

  yield canvas
end

#text_contentObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'ui/src/hokusai/blocks/input.rb', line 178

def text_content
  StringIO.open do |io|
    saved = table.to_s

    io.write saved[0...buffer_offset]
    io.write buffer

    if last = saved[buffer_offset..-1]
      io.write last
    end

    io.string
  end
end

#update_height(value) ⇒ Object



63
64
65
66
67
# File 'ui/src/hokusai/blocks/input.rb', line 63

def update_height(value)
  node.meta.props[:height] = value

  emit("height_updated", value)
end