Class: Ektoplayer::Views::TrackRenderer

Inherits:
UI::ListItemRenderer show all
Defined in:
lib/ektoplayer/views/trackrenderer.rb

Instance Method Summary collapse

Methods inherited from UI::ListItemRenderer

#width=

Constructor Details

#initialize(width: nil, format: nil) ⇒ TrackRenderer

Returns a new instance of TrackRenderer.



55
56
57
58
# File 'lib/ektoplayer/views/trackrenderer.rb', line 55

def initialize(width: nil, format: nil)
   super(width: width)
   self.column_format=(format)
end

Instance Method Details

#column_format=(format) ⇒ Object



60
61
62
63
64
# File 'lib/ektoplayer/views/trackrenderer.rb', line 60

def column_format=(format)
   return unless format
   @column_format = format
   layout
end

#layoutObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ektoplayer/views/trackrenderer.rb', line 66

def layout
   return unless @column_format
   fail if @column_format.empty?
   fail unless @width

   pd = PercentualDistributor.new(
      total: (@width - (@column_format.size - 1))
   )

   @column_format.each do |c|
      if c[:rel]
         pd.add_part(c[:rel])
      else
         c[:render_size] = c[:size]
         pd.increment_total(-1 * c[:size])
      end

      c[:curses_codes] = UI::Colors.set(nil, *c[:curses_attrs])
   end

   @column_format.each do |c|
      if c[:rel]
         c[:render_size] = (pd.get_for(c[:rel]) or fail)
      end
   end
end

#render(scr, item, index, selected: false, marked: false, selection: false) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ektoplayer/views/trackrenderer.rb', line 93

def render(scr, item, index, selected: false, marked: false, selection: false)
   fail ArgumentError, 'item is nil' unless item
   return unless @column_format

   additional_attributes = 0
   additional_attributes |= ICurses::A_BOLD     if marked
   additional_attributes |= ICurses::A_STANDOUT if selected

   if item.is_a? String or item.is_a? Symbol or item.is_a? Integer
      if selection
         color = Theme[:'list.item_selection']
      elsif index % 2 == 0
         color = Theme[:'list.item_even']
      else
         color = Theme[:'list.item_odd']
      end

      scr.attrset(color | additional_attributes)
      scr.addstr("[#{item}]".ljust(@width))
      return
   end

   left_pad = 0
   y = scr.cury

   @column_format.each_with_index do |c,i|
      if selection
         scr.attrset(Theme[:'list.item_selection'] | additional_attributes)
      else
         scr.attrset(c[:curses_codes] | additional_attributes)
      end

      if value = item[c[:tag]]
         if value.is_a? Integer
            value = '%.2d' % value
         else
            value = value.to_s[0..(c[:render_size] - 1)]
         end

         scr.mvaddstr(y, left_pad, ' ' * c[:render_size])
         scr.addch(32) if i < @column_format.size - 1

         if c[:justify] == :right
            scr.mvaddstr(y, left_pad, value.rjust(c[:render_size]))
         else
            scr.mvaddstr(y, left_pad, value)
         end
      end

      left_pad += c[:render_size] + 1
   end
end