Module: Teems::Formatters::FormatUtils
- Defined in:
- lib/teems/formatters/format_utils.rb
Overview
Shared stateless formatting utilities used across commands and formatters
Class Method Summary collapse
- .attachment_name(att) ⇒ Object
- .build_time_range(start_str, end_match) ⇒ Object
- .format_bytes(bytes) ⇒ Object
- .format_end_time(start_time, end_time) ⇒ Object
- .format_single_reaction(reaction, emoji_map) ⇒ Object
- .format_time(time_string) ⇒ Object
- .safe_filename(name) ⇒ Object
- .truncate(text, max = 120) ⇒ Object
Class Method Details
.attachment_name(att) ⇒ Object
25 26 27 |
# File 'lib/teems/formatters/format_utils.rb', line 25 def (att) att.is_a?(Hash) ? (att['fileName'] || att['name'] || 'file') : att.to_s end |
.build_time_range(start_str, end_match) ⇒ Object
49 50 51 52 53 |
# File 'lib/teems/formatters/format_utils.rb', line 49 def build_time_range(start_str, end_match) start_time = Time.parse(start_str).getlocal end_time = end_match ? Time.parse(end_match[1]).getlocal : nil [start_time, end_time] end |
.format_bytes(bytes) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/teems/formatters/format_utils.rb', line 13 def format_bytes(bytes) if bytes >= 1_048_576 then "#{(bytes / 1_048_576.0).round(1)} MB" elsif bytes >= 1024 then "#{(bytes / 1024.0).round(1)} KB" else "#{bytes} B" end end |
.format_end_time(start_time, end_time) ⇒ Object
44 45 46 47 |
# File 'lib/teems/formatters/format_utils.rb', line 44 def format_end_time(start_time, end_time) fmt = (end_time - start_time) >= 86_400 ? '%b %-d, %-I:%M %p' : '%-I:%M %p' end_time.strftime(fmt) end |
.format_single_reaction(reaction, emoji_map) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/teems/formatters/format_utils.rb', line 29 def format_single_reaction(reaction, emoji_map) type = reaction[:type] emoji = emoji_map[type] || type count = reaction[:count] || 1 count > 1 ? "#{emoji}(#{count})" : emoji.to_s end |
.format_time(time_string) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/teems/formatters/format_utils.rb', line 36 def format_time(time_string) return '' unless time_string Time.parse(time_string).getlocal.strftime('[%Y-%m-%d %H:%M]') rescue ArgumentError '' end |
.safe_filename(name) ⇒ Object
20 21 22 23 |
# File 'lib/teems/formatters/format_utils.rb', line 20 def safe_filename(name) base = File.basename(name) base.empty? ? 'file' : base end |
.truncate(text, max = 120) ⇒ Object
9 10 11 |
# File 'lib/teems/formatters/format_utils.rb', line 9 def truncate(text, max = 120) text.length > max ? "#{text[0...max]}..." : text end |