Class: Glimmer::LibUI::CustomControl::CodeEntry

Inherits:
Object
  • Object
show all
Includes:
Glimmer::LibUI::CustomControl
Defined in:
lib/glimmer/libui/custom_control/code_entry.rb

Constant Summary collapse

REGEX_COLOR_HEX6 =
/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/

Instance Attribute Summary collapse

Attributes included from Glimmer::LibUI::CustomControl

#args, #body_root, #content, #keyword, #libui, #options, #parent, #parent_proxy, #slot_controls

Instance Method Summary collapse

Methods included from Glimmer::LibUI::CustomControl

add_custom_control_namespaces_for, after_body, before_body, body, #can_handle_listener?, custom_control_namespaces, custom_controls_being_interpreted, def_option_attr_accessors, flyweight_custom_control_classes, for, #handle_listener, #has_instance_method?, #initialize, keyword, namespaces_for_class, #observer_registrations, option, options, #post_add_content, #post_initialize_child, reset_custom_control_namespaces, shortcut_keyword

Instance Attribute Details

#lineObject (readonly)

TODO consider offering the option to autosave to a file upon changes



45
46
47
# File 'lib/glimmer/libui/custom_control/code_entry.rb', line 45

def line
  @line
end

#positionObject (readonly)

TODO consider offering the option to autosave to a file upon changes



45
46
47
# File 'lib/glimmer/libui/custom_control/code_entry.rb', line 45

def position
  @position
end

#syntax_highlighterObject (readonly)

TODO consider offering the option to autosave to a file upon changes



45
46
47
# File 'lib/glimmer/libui/custom_control/code_entry.rb', line 45

def syntax_highlighter
  @syntax_highlighter
end

Instance Method Details

#caret_indexObject



278
279
280
# File 'lib/glimmer/libui/custom_control/code_entry.rb', line 278

def caret_index
  code.lines[0..line].join.length - (current_code_line.length - @position)
end

#caret_layerObject



260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/glimmer/libui/custom_control/code_entry.rb', line 260

def caret_layer
  # TODO adjust padding offset based on text extent
  text(padding - 4, padding) {
    default_font @font_default
      
    # TODO make caret blink
    string(caret_text) {
      color :black
      background [0, 0, 0, 0]
    }
  }
end

#caret_textObject



273
274
275
276
# File 'lib/glimmer/libui/custom_control/code_entry.rb', line 273

def caret_text
  # TODO replace | with a real caret (see if there is an emoji or special character for it)
  ("\n" * @line) + (' ' * @position) + '|'
end

#code_layerObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/glimmer/libui/custom_control/code_entry.rb', line 244

def code_layer
  text(padding, padding) {
    default_font @font_default
    
    syntax_highlighter.syntax_highlighting(code).each do |token|
      token_text = token[:token_text].start_with?("\n") ? " #{token[:token_text]}" : token[:token_text]
      
      string(token_text) {
        font @font_italic if token[:token_style][:italic]
        color token[:token_style][:fg] || :black
        background token[:token_style][:bg] || :white
      }
    end
  }
end

#current_code_lineObject



282
283
284
# File 'lib/glimmer/libui/custom_control/code_entry.rb', line 282

def current_code_line
  code.lines[@line]
end

#current_code_line_max_positionObject



286
287
288
# File 'lib/glimmer/libui/custom_control/code_entry.rb', line 286

def current_code_line_max_position
  (current_code_line && current_code_line.length > 0) ? (current_code_line.length - 1) : 0
end

#line_countObject



294
295
296
# File 'lib/glimmer/libui/custom_control/code_entry.rb', line 294

def line_count
  code&.lines&.size || 1
end

#longest_line_sizeObject



290
291
292
# File 'lib/glimmer/libui/custom_control/code_entry.rb', line 290

def longest_line_size
  code&.lines&.map(&:size)&.max || 1
end