Class: Ncurses::CharCode

Inherits:
String show all
Defined in:
lib/sup/util/ncurses.rb

Overview

Helper class for storing keycodes and multibyte characters.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from String

#ascii, #camel_to_hyphy, #check, #display_length, #each, #find_all_positions, #fix_encoding!, #normalize_whitespace, #ord, #slice_by_display_length, #split_on_commas, #split_on_commas_with_remainder, #to_set_of_symbols, #transcode, #wrap

Constructor Details

#initialize(c = "", status = Ncurses::OK) ⇒ CharCode

Returns a new instance of CharCode.



75
76
77
78
79
80
81
# File 'lib/sup/util/ncurses.rb', line 75

def initialize(c = "", status = Ncurses::OK)
  @status = status
  c = "" if c.nil?
  return super("") if status == Ncurses::ERR
  c = enc_char(c) if c.is_a?(Integer)
  super c.length > 1 ? c[0,1] : c
end

Instance Attribute Details

#statusObject (readonly)

Status code allows us to detect printable characters and control codes.



12
13
14
# File 'lib/sup/util/ncurses.rb', line 12

def status
  @status
end

Class Method Details

.character(c) ⇒ Object

Creates new instance of CharCode that keeps a printable character.



38
39
40
# File 'lib/sup/util/ncurses.rb', line 38

def self.character(c)
  generate c, Ncurses::OK
end

.dumb!Object

Enables dumb mode for any new instance.



66
67
68
# File 'lib/sup/util/ncurses.rb', line 66

def self.dumb!
  @dumb = true
end

.dumb?Boolean

Asks if dumb mode was set

Returns:

  • (Boolean)


71
72
73
# File 'lib/sup/util/ncurses.rb', line 71

def self.dumb?
  defined?(@dumb) && @dumb
end

.emptyObject

Returns empty singleton.



26
27
28
# File 'lib/sup/util/ncurses.rb', line 26

def self.empty
  Empty.instance
end

.generate(c = nil, status = Ncurses::OK) ⇒ Object

Generates new object like new but for empty or erroneous objects it returns empty singleton.



45
46
47
48
49
50
51
# File 'lib/sup/util/ncurses.rb', line 45

def self.generate(c = nil, status = Ncurses::OK)
  if status == Ncurses::ERR || c.nil? || c === Ncurses::ERR
    empty
  else
    new(c, status)
  end
end

.get(handle_interrupt = true) ⇒ Object

Gets character from input. Pretends ctrl-c’s are ctrl-g’s.



55
56
57
58
59
60
61
62
63
# File 'lib/sup/util/ncurses.rb', line 55

def self.get handle_interrupt=true
  begin
    status, code = nonblocking_getwch
    generate code, status
  rescue Interrupt => e
    raise e unless handle_interrupt
    keycode Ncurses::KEY_CANCEL
  end
end

.keycode(c) ⇒ Object

Creates new instance of CharCode that keeps a given keycode.



32
33
34
# File 'lib/sup/util/ncurses.rb', line 32

def self.keycode(c)
  generate c, Ncurses::KEY_CODE_YES
end

.nonblocking_getwchObject

Reads character from user input.



15
16
17
18
19
20
21
22
23
# File 'lib/sup/util/ncurses.rb', line 15

def self.nonblocking_getwch
  # If we get input while we're shelled, we'll ignore it for the
  # moment and use Ncurses.sync to wait until the shell_out is done.
  begin
    s, c = Redwood::BufferManager.shelled? ? Ncurses.sync { nil } : Ncurses.get_wch
    break if s != Ncurses::ERR
  end until IO.select([$stdin], nil, nil, 2)
  [s, c]
end

Instance Method Details

#characterObject

Alias for try_character



106
# File 'lib/sup/util/ncurses.rb', line 106

def character       ; try_character                             end

#character!Object

Sets character flag



108
# File 'lib/sup/util/ncurses.rb', line 108

def character!      ; @status  = Ncurses::OK ; self             end

#character?Boolean

Returns true if character

Returns:

  • (Boolean)


107
# File 'lib/sup/util/ncurses.rb', line 107

def character?      ; dumb? || @status == Ncurses::OK           end

#codeObject

Returns decimal representation of a character



100
# File 'lib/sup/util/ncurses.rb', line 100

def code            ; ord                                       end

#dumb?Boolean

True if we cannot distinguish keycodes from characters

Returns:

  • (Boolean)


114
# File 'lib/sup/util/ncurses.rb', line 114

def dumb?           ; self.class.dumb?                          end

#is_character?(c) ⇒ Boolean

Tests if character matches

Returns:

  • (Boolean)


102
# File 'lib/sup/util/ncurses.rb', line 102

def is_character?(c); character? &&  self == c                  end

#is_keycode?(c) ⇒ Boolean

Tests if keycode matches

Returns:

  • (Boolean)


101
# File 'lib/sup/util/ncurses.rb', line 101

def is_keycode?(c)  ; keycode?   &&  code == c                  end

#keycodeObject

Alias for try_keycode



105
# File 'lib/sup/util/ncurses.rb', line 105

def keycode         ; try_keycode                               end

#keycode!Object

Sets keycode flag



110
# File 'lib/sup/util/ncurses.rb', line 110

def keycode!        ; @status  = Ncurses::KEY_CODE_YES ; self   end

#keycode=(c) ⇒ Object

Sets keycode



111
# File 'lib/sup/util/ncurses.rb', line 111

def keycode=(c)     ; replace(c); keycode! ; self               end

#keycode?Boolean

Returns true if keycode

Returns:

  • (Boolean)


109
# File 'lib/sup/util/ncurses.rb', line 109

def keycode?        ; dumb? || @status == Ncurses::KEY_CODE_YES end

#present?Boolean

Proxy method

Returns:

  • (Boolean)


112
# File 'lib/sup/util/ncurses.rb', line 112

def present?        ; not empty?                                end

#printable?Boolean

Alias for character?

Returns:

  • (Boolean)


113
# File 'lib/sup/util/ncurses.rb', line 113

def printable?      ; character?                                end

#replace(c) ⇒ Object

Proxy method for String’s replace



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sup/util/ncurses.rb', line 84

def replace(c)
  return self if c.object_id == object_id
  if c.is_a?(self.class)
    @status = c.status
    super(c)
  else
    @status = Ncurses::OK
    c = "" if c.nil?
    c = enc_char(c) if c.is_a?(Integer)
    super c.length > 1 ? c[0,1] : c
  end
end

#to_characterObject

Returns character or code as a string



97
# File 'lib/sup/util/ncurses.rb', line 97

def to_character    ; character? ? self : "<#{code}>"           end

#to_keycodeObject

Returns keycode or ERR if it’s not a keycode



98
# File 'lib/sup/util/ncurses.rb', line 98

def to_keycode      ; keycode?   ? code : Ncurses::ERR          end

#to_sequenceObject

Returns unpacked sequence of bytes for a character



99
# File 'lib/sup/util/ncurses.rb', line 99

def to_sequence     ; bytes.to_a                                end

#try_characterObject

Returns character if character, nil otherwise



104
# File 'lib/sup/util/ncurses.rb', line 104

def try_character   ; character? ? self : nil                   end

#try_keycodeObject

Returns dec. code if keycode, nil otherwise



103
# File 'lib/sup/util/ncurses.rb', line 103

def try_keycode     ; keycode?   ? code : nil                   end