Class: Chamomile::Cursor
- Inherits:
-
Object
- Object
- Chamomile::Cursor
- Defined in:
- lib/chamomile/components/cursor.rb
Overview
Virtual text cursor with blink modes (blink, static, hide).
Constant Summary collapse
- MODE_BLINK =
:blink- MODE_STATIC =
:static- MODE_HIDE =
:hide- BLINK_SPEED =
0.53
Instance Attribute Summary collapse
-
#blink_speed ⇒ Object
readonly
Returns the value of attribute blink_speed.
-
#blinked ⇒ Object
Returns the value of attribute blinked.
-
#char ⇒ Object
Returns the value of attribute char.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#mode ⇒ Object
Returns the value of attribute mode.
Class Method Summary collapse
Instance Method Summary collapse
- #blink_cmd ⇒ Object
- #blur ⇒ Object
- #focus ⇒ Object
- #focused? ⇒ Boolean
- #handle(msg) ⇒ Object (also: #update)
-
#initialize(mode: MODE_BLINK, blink_speed: BLINK_SPEED) ⇒ Cursor
constructor
A new instance of Cursor.
- #view ⇒ Object
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
#blink_speed ⇒ Object (readonly)
Returns the value of attribute blink_speed.
30 31 32 |
# File 'lib/chamomile/components/cursor.rb', line 30 def blink_speed @blink_speed end |
#blinked ⇒ Object
Returns the value of attribute blinked.
31 32 33 |
# File 'lib/chamomile/components/cursor.rb', line 31 def blinked @blinked end |
#char ⇒ Object
Returns the value of attribute char.
31 32 33 |
# File 'lib/chamomile/components/cursor.rb', line 31 def char @char end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
30 31 32 |
# File 'lib/chamomile/components/cursor.rb', line 30 def id @id end |
#mode ⇒ Object
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_id ⇒ Object
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
#blink_cmd ⇒ Object
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 |
#blur ⇒ Object
49 50 51 52 53 54 |
# File 'lib/chamomile/components/cursor.rb', line 49 def blur @focused = false @blinked = true @tag += 1 nil end |
#focus ⇒ Object
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
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 |
#view ⇒ Object
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 |