Class: Pppt::Helpers
- Inherits:
-
Object
- Object
- Pppt::Helpers
- Defined in:
- lib/pppt/helpers.rb
Class Method Summary collapse
- .format_page_number(page) ⇒ Object
- .screen_width(str) ⇒ Object
- .split_screen_width(str, width = 40) ⇒ Object
- .truncate_screen_width(str, width, suffix = '...') ⇒ Object
Class Method Details
.format_page_number(page) ⇒ Object
31 32 33 34 35 |
# File 'lib/pppt/helpers.rb', line 31 def self.format_page_number(page) index = page['page'] || '-' title = page['action'] || (page['title'] && " - #{page['title']}") || page['h1'] || page['h2'] || page['h3'] || (page['body'] && " #{Helpers.truncate_screen_width(page['body'].join(''), 15)}") format('%02s %s', index, title) end |
.screen_width(str) ⇒ Object
26 27 28 29 |
# File 'lib/pppt/helpers.rb', line 26 def self.screen_width(str) hankaku_len = str.each_char.count(&:ascii_only?) hankaku_len + (str.size - hankaku_len) * 2 end |
.split_screen_width(str, width = 40) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pppt/helpers.rb', line 13 def self.split_screen_width(str, width = 40) s = i = r = 0 str.each_char.each_with_object([]) do |c, res| i += c.ascii_only? ? 1 : 2 r += 1 next res if i < width res << str[s, r] s += r i = r = 0 res end << str[s, r] end |
.truncate_screen_width(str, width, suffix = '...') ⇒ Object
2 3 4 5 6 7 8 9 10 11 |
# File 'lib/pppt/helpers.rb', line 2 def self.truncate_screen_width(str, width, suffix = '...') i = 0 str.each_char.inject(0) do |c, x| c += x.ascii_only? ? 1 : 2 i += 1 next c if c < width return str[0, i] + suffix end str end |