Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/journal-cli/color.rb,
lib/journal-cli/color.rb,
lib/journal-cli/string.rb
Overview
String helpers
Instance Method Summary collapse
-
#last_color_code ⇒ Object
Get the calculated ANSI color at the end of the string.
-
#normalize_color ⇒ String
Normalize a color name, removing underscores, replacing “bright” with “bold”, and converting bgbold to boldbg.
-
#parse_condition ⇒ Boolean
Parse and test a condition.
-
#validate_color ⇒ String
Extract the longest valid %color name from a string.
- #x ⇒ Object
Instance Method Details
#last_color_code ⇒ Object
Get the calculated ANSI color at the end of the string
137 138 139 140 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/journal-cli/color.rb', line 137 def last_color_code m = scan(ESCAPE_REGEX) em = ["0"] fg = nil bg = nil rgbf = nil rgbb = nil m.each do |c| case c when "0" em = ["0"] fg, bg, rgbf, rgbb = nil when /^[34]8/ case c when /^3/ fg = nil rgbf = c when /^4/ bg = nil rgbb = c end else c.split(";").each do |i| x = i.to_i if x <= 9 em << x elsif x >= 30 && x <= 39 rgbf = nil fg = x elsif x >= 40 && x <= 49 rgbb = nil bg = x elsif x >= 90 && x <= 97 rgbf = nil fg = x elsif x >= 100 && x <= 107 rgbb = nil bg = x end end end end escape = "\e[#{em.join(";")}m" escape += "\e[#{rgbb}m" if rgbb escape += "\e[#{rgbf}m" if rgbf escape + "\e[#{[fg, bg].delete_if(&:nil?).join(";")}m" end |
#normalize_color ⇒ String
Normalize a color name, removing underscores, replacing “bright” with “bold”, and converting bgbold to boldbg
128 129 130 |
# File 'lib/journal-cli/color.rb', line 128 def normalize_color delete("_").sub(/bright/i, "bold").sub(/bgbold/, "boldbg") end |
#parse_condition ⇒ Boolean
Parse and test a condition
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 |
# File 'lib/journal-cli/string.rb', line 11 def parse_condition condition = dup time_rx = /(?<comp>[<>=]{1,2}|before|after) +(?<time>(?:noon|midnight|[0-9]+) *(?:am|pm)?)$/i return true unless condition&.match?(time_rx) now = Journal.date m = condition.match(time_rx) time = Chronic.parse(m["time"]) now.localtime time.localtime time_of_day = Time.parse("#{now.strftime("%Y-%m-%d")} #{time.strftime("%H:%M")}") Journal.notify("{br}Invalid time string in question (#{m["time"]})", exit_code: 4) unless time case m["comp"] when /^<=$/ now <= time_of_day when /^(<|bef)/i now < time_of_day when /^>=/ now >= time_of_day when /^(>|aft)/i now > time_of_day end # TODO: Other condition types end |
#validate_color ⇒ String
Extract the longest valid %color name from a string.
Allows %colors to bleed into other text and still be recognized, e.g. %greensomething still finds %green.
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/journal-cli/color.rb', line 110 def validate_color valid_color = nil compiled = "" normalize_color.chars.each do |char| compiled += char valid_color = compiled if Color.attributes.include?(compiled.to_sym) || compiled =~ /^([fb]g?)?#([a-f0-9]{6})$/i end valid_color end |