Module: PipeText
- Defined in:
- lib/pipetext/version.rb,
lib/pipetext/pipetext.rb
Constant Summary collapse
- VERSION =
"0.2.5"
Instance Method Summary collapse
-
#abbreviated_match(input, match, case_match = false, ignored_characters = ",.':-") ⇒ Object
Match abbreviated text descriptions for emojis by default ignore case and punctuation Allows for ‘space anchoring’ so you can use |[smi f w he e] as an abbreviation for |[smiling face with heart-eyes].
-
#fastpipe(text, attributes) ⇒ Object
Immediate output to screen, no string buffer.
- #fastpipetext(text, box_mode = true, ampersand_mode = false) ⇒ Object
- #ignored_character(character, ignored_characters) ⇒ Object
-
#paint(text, box_mode = true, ampersand_mode = true) ⇒ Object
Defaults to using & for background colors.
- #pipe(text, attributes) ⇒ Object
- #pipetext(text, box_mode = true, ampersand_mode = false) ⇒ Object
- #pipetext_init(box_mode = true, ampersand_mode = false) ⇒ Object
-
#printable_length(string) ⇒ Object
This is not entirely accurate because of emojis, which we assume are 2 characters.
- #write(text, box_mode = true, ampersand_mode = false) ⇒ Object
Instance Method Details
#abbreviated_match(input, match, case_match = false, ignored_characters = ",.':-") ⇒ Object
Match abbreviated text descriptions for emojis by default ignore case and punctuation Allows for ‘space anchoring’ so you can use |[smi f w he e] as an abbreviation for |[smiling face with heart-eyes]
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/pipetext/pipetext.rb', line 141 def abbreviated_match(input, match, case_match=false, ignored_characters=",.':-") if(!input || !match) return 0 end count = 0 offset = 0 input.chars.each_with_index do |character, index| while(ignored_character(match[index + offset], ignored_characters)) offset += 1 end if(character == match[index + offset].chr) count += 1 elsif(case_match == false && character.downcase == match[index + offset].chr.downcase) count += 1 elsif(character == ' ' && match[index + offset..-1] =~ / /) count += 1 while(match[index + offset].chr != character) offset += 1 end else count = 0 offset = 0 break end end return count + offset end |
#fastpipe(text, attributes) ⇒ Object
Immediate output to screen, no string buffer
85 86 87 88 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 |
# File 'lib/pipetext/pipetext.rb', line 85 def fastpipe(text, attributes) new_text = String.new text.chars.each do |character| process_character(character, new_text, attributes) if(new_text != "") print(new_text) if(new_text =~ /\n(.*)$/) attributes['position'] = printable_length($1) else attributes['position'] += printable_length(new_text) end new_text = String.new end end # Clean up in case we've captured something we didn't process yet if(attributes['color_capture'] > 0) emit_color(new_text, attributes) elsif(attributes['palette_capture'] > 0) emit_palette_color(new_text, attributes) elsif(attributes['unicode_capture'] > 0) emit_unicode(new_text, attributes) elsif(attributes['emoji_capture'] == true) new_text << "|[" + attributes['emoji'] elsif(attributes['center_capture'] == true) new_text << "|{" + attributes['center'] elsif(attributes['variable_capture'] == true) new_text << "|(" + attributes['variable'] end print(new_text) end |
#fastpipetext(text, box_mode = true, ampersand_mode = false) ⇒ Object
116 117 118 |
# File 'lib/pipetext/pipetext.rb', line 116 def fastpipetext(text, box_mode=true, ampersand_mode=false) fastpipe(text, pipetext_init(box_mode, ampersand_mode)) end |
#ignored_character(character, ignored_characters) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/pipetext/pipetext.rb', line 129 def ignored_character(character, ignored_characters) ignored_characters.chars.each do |ignored| if(character == ignored) return true end end return false end |
#paint(text, box_mode = true, ampersand_mode = true) ⇒ Object
Defaults to using & for background colors
125 126 127 |
# File 'lib/pipetext/pipetext.rb', line 125 def paint(text, box_mode=true, ampersand_mode=true) fastpipetext(text, box_mode, ampersand_mode) end |
#pipe(text, attributes) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/pipetext/pipetext.rb', line 53 def pipe(text, attributes) new_text = String.new text.chars.each do |character| process_character(character, new_text, attributes) if(new_text =~ /.*\n(.*)$/m) attributes['position'] = printable_length($1) else attributes['position'] += printable_length(new_text) end end # Clean up in case we've captured something we didn't process yet if(attributes['color_capture'] > 0) emit_color(new_text, attributes) elsif(attributes['palette_capture'] > 0) emit_palette_color(new_text, attributes) elsif(attributes['unicode_capture'] > 0) emit_unicode(new_text, attributes) elsif(attributes['emoji_capture'] == true) new_text << "|[" + attributes['emoji'] elsif(attributes['center_capture'] == true) new_text << "|{" + attributes['center'] elsif(attributes['variable_capture'] == true) new_text << "|(" + attributes['variable'] end return new_text end |
#pipetext(text, box_mode = true, ampersand_mode = false) ⇒ Object
80 81 82 |
# File 'lib/pipetext/pipetext.rb', line 80 def pipetext(text, box_mode=true, ampersand_mode=false) pipe(text, pipetext_init(box_mode, ampersand_mode)) end |
#pipetext_init(box_mode = true, ampersand_mode = false) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/pipetext/pipetext.rb', line 7 def pipetext_init(box_mode=true, ampersand_mode=false) attributes = { 'pipe' => false, # Pipe already been found? 'repeat_pattern' => false, # Used by |<#>~repeat pattern~ 'pattern' => String.new, # Used by |<#>~repeat pattern~ to capture 'escape' => false, # Has an escape \ already been found in front of this character? 'ampersand' => false, # Has an ampersand already been found in front of this character? 'ampersand_mode' => ampersand_mode, # Do we even process ampersands for background colors? 'blink' => false, # Is blink turned on? 'bold' => false, 'crossed_out' => false, 'faint' => false, 'found' => false, # At the end -- did we find a match? 'italic' => false, 'inverse' => false, 'underline' => false, 'box' => -1, # Default to |O (no boxes) 'box_mode' => box_mode, 'num' => 0, # Number of times to repeat pattern 'end_capture' => false, # Used to capture the end column number 'end' => 0, # Number which current denotes the end of the column 'position' => 0, # Number which current position in the column 'center_capture' => false, # Used to capture text for centering 'center' => String.new, # Text to be centered 'variable_capture' => false, # Used to capture a variable for update or to display 'variable' => String.new, # Variable captured 'variables' => Hash.new, # Stores all captured variables 'emoji_capture' => false, # Used to capture emoji description or bell/move to position 'emoji' => String.new, # Used to capture emoji description or bell/move to position 'unicode_capture' => 0, # Used to capture Unicode using 6 character UTF-16 hex format 'unicode' => String.new, 'palette_capture' => 0, # Used to capture 8-bit color using 2 character hex format 'p' => String.new, # |p00 to |pFF 'color_capture' => 0, # Used to capture RGB color using #RRGGBB format 'r' => String.new, 'g' => String.new, 'b' => String.new, 'fg' => String.new, # Needed to restore after background change 'bg' => String.new, # Needed to restore after foreground change 'shell_prompt' => false # Used to add \[ and \] around non-printables for prompts } attributes['variables']['height'] = `tput lines`.chomp attributes['variables']['width'] = `tput cols`.chomp return attributes end |
#printable_length(string) ⇒ Object
This is not entirely accurate because of emojis, which we assume are 2 characters
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/pipetext/pipetext.rb', line 170 def printable_length(string) length = 0 escape = false string.chars.each do |character| if(character.ord == 27) escape = true elsif(character.ord >= 32) if(escape == true && character.ord == 109) escape = false elsif(character.ord > 9600) # ~ Emoji / Unicode - double wide characters start length += 2 elsif(escape == false) length += 1 end end end return length end |
#write(text, box_mode = true, ampersand_mode = false) ⇒ Object
120 121 122 |
# File 'lib/pipetext/pipetext.rb', line 120 def write(text, box_mode=true, ampersand_mode=false) fastpipetext(text, box_mode, ampersand_mode) end |