Class: Chamomile::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/chamomile/components/cursor.rb

Overview

Virtual text cursor with blink modes (blink, static, hide).

Constant Summary collapse

:blink
MODE_STATIC =
:static
MODE_HIDE =
:hide
0.53

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode: MODE_BLINK, blink_speed: BLINK_SPEED) ⇒ Cursor

Returns a new instance of Cursor.



33
34
35
36
37
38
39
40
41
# File 'lib/chamomile/components/cursor.rb', line 33

def initialize(mode: MODE_BLINK, blink_speed: BLINK_SPEED)
  @id = self.class.next_id
  @mode = mode
  @blink_speed = blink_speed
  @char = " "
  @blinked = false
  @focused = false
  @tag = 0
end

Instance Attribute Details

Returns the value of attribute blink_speed.



30
31
32
# File 'lib/chamomile/components/cursor.rb', line 30

def blink_speed
  @blink_speed
end

#blinkedObject

Returns the value of attribute blinked.



31
32
33
# File 'lib/chamomile/components/cursor.rb', line 31

def blinked
  @blinked
end

#charObject

Returns the value of attribute char.



31
32
33
# File 'lib/chamomile/components/cursor.rb', line 31

def char
  @char
end

#idObject (readonly)

Returns the value of attribute id.



30
31
32
# File 'lib/chamomile/components/cursor.rb', line 30

def id
  @id
end

#modeObject

Returns the value of attribute mode.



30
31
32
# File 'lib/chamomile/components/cursor.rb', line 30

def mode
  @mode
end

Class Method Details

.next_idObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/chamomile/components/cursor.rb', line 18

def self.next_id
  @id_mutex.synchronize do
    if Process.pid != @id_pid
      @id_pid = Process.pid
      @next_id = 0
      @id_mutex = Mutex.new
    end
    @next_id += 1
    "#{@id_pid}-cur-#{@next_id}"
  end
end

Instance Method Details



67
68
69
70
71
72
73
74
75
# File 'lib/chamomile/components/cursor.rb', line 67

def blink_cmd
  captured_id = @id
  captured_tag = @tag
  speed = @blink_speed
  -> {
    sleep(speed)
    CursorBlinkMsg.new(id: captured_id, tag: captured_tag)
  }
end

#blurObject



49
50
51
52
53
54
# File 'lib/chamomile/components/cursor.rb', line 49

def blur
  @focused = false
  @blinked = true
  @tag += 1
  nil
end

#focusObject



43
44
45
46
47
# File 'lib/chamomile/components/cursor.rb', line 43

def focus
  @focused = true
  @blinked = false
  @mode == MODE_BLINK ? blink_cmd : nil
end

#focused?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/chamomile/components/cursor.rb', line 56

def focused?
  @focused
end

#handle(msg) ⇒ Object Also known as: update



77
78
79
80
81
82
83
84
85
# File 'lib/chamomile/components/cursor.rb', line 77

def handle(msg)
  return unless msg.is_a?(CursorBlinkMsg)
  return unless @mode == MODE_BLINK && @focused
  return unless msg.id == @id && msg.tag == @tag

  @blinked = !@blinked
  @tag += 1
  blink_cmd
end

#viewObject



89
90
91
92
93
94
# File 'lib/chamomile/components/cursor.rb', line 89

def view
  return @char if @mode == MODE_HIDE
  return @char if @blinked

  "\e[7m#{@char}\e[0m"
end