pipetext 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ddf8b4e3efec82be555067563e9729ae54582acbe96a3936d136b481a7775bc3
4
- data.tar.gz: 7df01c3c6613a9f07675d1377630fdedfcb7b4447463377d4e927ad1000ceaa7
3
+ metadata.gz: 5883f76759f24059807937e5e3b245bb2994b69a196b1c9be7377500ec77f9b8
4
+ data.tar.gz: 2af581984d9692051ad962fc3e66328e97af653eec3479b0b33c4d84119cc795
5
5
  SHA512:
6
- metadata.gz: 25a8f7a14ee3a8e09effd2b42c643534eee9f91c3a91f7457288851a02623266fa51d9ae7cc8495892f2e4229b9ba164bde0a0f55e50bbcf0a4380d107d03cc0
7
- data.tar.gz: 72e8c74da68dfdf80a146d3ec81606487e6b2a410dc306ad1d4df80a0dce97f1b72adf9fe3499e73528622f0194dd93e61f46998474b6414c3000cf6237d9ad7
6
+ metadata.gz: 3b46368967758946d1b61e724dafa60e32272e04360fbe05fef35713708f6125175d45d77e3aca92bbb3cb4badc360f60614a21d4b50e1d2c9a4aefacd928bb3
7
+ data.tar.gz: 85214ee6cb603c8a42b2cb32c09ccdc65b8cf0908246cc7134ac3669a1d038e9517a771b46e8feab7bfa79e55be2eedf4886461de68d31c682e50b2c57094ee1
data/bin/pipetext CHANGED
@@ -27,9 +27,10 @@ pipetext_example = '|| pipe |||| && ampersand &&&& |&Toggle (&) backgr
27
27
  |G&rbright green with red background&n |n||G&&r |xCrossed out|x ||x
28
28
  |n&nnormal color and background ||n&&n Escape Sequence ||\\
29
29
 
30
- Example unicode sequences: https://unicode.org/emoji/charts/full-emoji-list.html
31
- ||[CLDR Short Name] |[gear] ||[gear |\b] |[smiling face with heart-eyes] ||[smiling face with heart-eyes |\b] |[zzz] ||[zzz |\b]
32
- |U2714 ||U2714 |U274c ||U274c |u262E ||u262E |u1f48e ||u1f48e |u1f49c ||u1f49c
30
+ Emojis: https://unicode.org/emoji/charts/full-emoji-list.html
31
+ ||[Abbreviated CLDR Short Name] |[smiling face with heart-eyes] ||[smiling face with heart-eyes] or
32
+ |[gear] ||[gear] |[zzz] ||[zzz] |[man] ||[man] |[sm f w he e] ||[sm f w he e]
33
+ |U2714 ||U2714 |U274c ||U274c |u262E ||u262E |u1f48e ||u1f48e |u1f49c ||u1f49c
33
34
 
34
35
  Single or double line box mode with ||- or ||=
35
36
 
@@ -52,7 +53,7 @@ Longer repetition patterns using the character ~
52
53
  ||3~ &&r||----||O> ALL YOUR ||kBASE||n ||3\~ARE||3\~ BELONG TO US. <||----||O&&n||\n~||n
53
54
  |3~ &r|----|O> ALL YOUR |kBASE|n |3\~ARE|3\~ BELONG TO US. <|----|O&n|\n~|n'
54
55
 
55
- if(ARGV[0] == nil)
56
+ if(ARGV[0] == nil && STDIN.tty? == true)
56
57
  puts "#{File.basename(__FILE__)} <filename> | <pipetext>"
57
58
  puts
58
59
  puts "Example usage:"
@@ -67,6 +68,9 @@ if(ARGV[0] == nil)
67
68
  puts
68
69
  pipe.paint(pipetext_example)
69
70
  else
71
+ if(STDIN.tty? == false)
72
+ pipe.paint(STDIN.read)
73
+ end
70
74
  begin
71
75
  f = File.open(ARGV[0].to_s)
72
76
  rescue
data/lib/pipetext.rb CHANGED
@@ -23,6 +23,8 @@ module PipeText
23
23
  'box' => -1, # Default to |O (no boxes)
24
24
  'box_mode' => box_mode,
25
25
  'num' => 0, # Number of times to repeat pattern
26
+ 'emoji_capture' => false, # Used to capture emoji description
27
+ 'emoji' => String.new, # Used to capture emoji description
26
28
  'unicode_capture' => 0, # Used to capture Unicode using 6 character UTF-16 hex format
27
29
  'unicode' => String.new,
28
30
  'palette_capture' => 0, # Used to capture 8-bit color using 2 character hex format
@@ -35,7 +37,6 @@ module PipeText
35
37
  'bg' => String.new # Needed to restore after foreground change
36
38
  }
37
39
  new_text = String.new
38
- text = substitute_emojis(text)
39
40
  text.chars.each do |character|
40
41
  process_character(character, new_text, attributes)
41
42
  end
@@ -46,6 +47,8 @@ module PipeText
46
47
  emit_palette_color(new_text, attributes)
47
48
  elsif(attributes['unicode_capture'] > 0)
48
49
  emit_unicode(new_text, attributes)
50
+ elsif(attributes['emoji_capture'] == true)
51
+ new_text << "|[" + attributes['emoji']
49
52
  end
50
53
  new_text
51
54
  end
@@ -59,6 +62,46 @@ module PipeText
59
62
  puts(pipetext(text, box_mode, ampersand_mode))
60
63
  end
61
64
 
65
+ def ignored_character(character, ignored_characters)
66
+ ignored_characters.chars.each do |ignored|
67
+ if(character == ignored)
68
+ return true
69
+ end
70
+ end
71
+ return false
72
+ end
73
+
74
+ # Match abbreviated text descriptions for emojis by default ignore case and punctuation
75
+ # Allows for 'space anchoring' so you can use |[smi f w he e] as an abbreviation for
76
+ # |[smiling face with heart-eyes]
77
+ def abbreviated_match(input, match, case_match=false, ignored_characters=",.':-")
78
+ if(!input || !match)
79
+ return 0
80
+ end
81
+ count = 0
82
+ offset = 0
83
+ input.chars.each_with_index do |character, index|
84
+ while(ignored_character(match[index + offset], ignored_characters))
85
+ offset += 1
86
+ end
87
+ if(character == match[index + offset])
88
+ count += 1
89
+ elsif(case_match == false && character.downcase == match[index + offset].downcase)
90
+ count += 1
91
+ elsif(character == ' ' && match[index + offset..-1] =~ / /)
92
+ count += 1
93
+ while(match[index + offset] != character)
94
+ offset += 1
95
+ end
96
+ else
97
+ count = 0
98
+ offset = 0
99
+ break
100
+ end
101
+ end
102
+ return count + offset
103
+ end
104
+
62
105
  private
63
106
 
64
107
  def process_character(character, new_text, attributes)
@@ -84,6 +127,12 @@ module PipeText
84
127
  capture_color(character, new_text, attributes)
85
128
  elsif(attributes['palette_capture'] > 0 && character =~ /[0-9,A-F,a-f]/)
86
129
  capture_palette_color(character, new_text, attributes)
130
+ elsif(attributes['emoji_capture'] == true)
131
+ if(character == ']')
132
+ emit_emoji(new_text, attributes)
133
+ else
134
+ attributes['emoji'] << character
135
+ end
87
136
  elsif(attributes['unicode_capture'] == 1 && character == '+') # Skip
88
137
  return
89
138
  elsif(attributes['unicode_capture'] > 0 && character =~ /[0-9,A-F,a-f]/)
@@ -144,6 +193,41 @@ module PipeText
144
193
  attributes['unicode'] = String.new
145
194
  end
146
195
 
196
+ def emit_emoji(new_text, attributes)
197
+ emoji = String.new
198
+ match_length = 0
199
+ value_length = 0
200
+ $substitute_emoji_names.each do |key, value|
201
+ if(attributes['emoji'] == key) # Use the most precise match first
202
+ emoji = value
203
+ break
204
+ elsif(attributes['emoji'].length <= key.length) # Otherwise use shortest match
205
+ match = abbreviated_match(attributes['emoji'], key)
206
+ if(match == 0 && key =~ /-/)
207
+ match = abbreviated_match(attributes['emoji'], key.sub(/-/, ' '))
208
+ end
209
+ if(match > 0 && (match <= match_length || match_length == 0) &&
210
+ (value.length <= value_length || value_length == 0))
211
+ emoji = value
212
+ match_length = match
213
+ value_length = value.length
214
+ end
215
+ end
216
+ end
217
+ if(emoji == String.new) # No match, put copy input
218
+ new_text << "|[" + attributes['emoji'] + "]"
219
+ else
220
+ emoji.split(/\|U/).each do |e|
221
+ if(e != "")
222
+ attributes['unicode'] = e.sub(/^\+?/, '')
223
+ emit_unicode(new_text, attributes)
224
+ end
225
+ end
226
+ attributes['emoji'] = String.new
227
+ end
228
+ attributes['emoji_capture'] = false
229
+ end
230
+
147
231
  def process_pipe(character, new_text, attributes)
148
232
  if(attributes['pipe'] == true && attributes['num'] == 0) # ||
149
233
  attributes['pipe'] = false
@@ -474,6 +558,8 @@ module PipeText
474
558
  else # We didn't find the next character
475
559
  attributes['found'] = false
476
560
  end
561
+ when '[' # |[emoji]
562
+ attributes['emoji_capture'] = true
477
563
  when '\\' # |\ - Escape mode
478
564
  attributes['pattern_escape'] = true
479
565
  else # We didn't find the next character
@@ -665,11 +751,4 @@ module PipeText
665
751
  character
666
752
  end
667
753
  end
668
-
669
- def substitute_emojis(text)
670
- $substitute_emoji_names.each do |key, value|
671
- text = text.gsub(/\|\[#{key}\]/i, value)
672
- end
673
- text
674
- end
675
754
  end