Class: TermUtils::Tab::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/term_utils/tab.rb

Overview

Represents a table column.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Column

Returns a new instance of Column

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (Symbol)
  • :index (Integer)
  • :width (Integer)
  • :align (Symbol)
  • :fixed (Boolean)
  • :ellipsis (String)
  • :format (Proc, String, nil)


173
174
175
176
177
178
179
180
181
# File 'lib/term_utils/tab.rb', line 173

def initialize(opts = {})
  @id = opts.fetch(:id)
  @index = opts.fetch(:index)
  @width = opts.fetch(:width, 8)
  @align = opts.fetch(:align, :left)
  @fixed = opts.fetch(:fixed, false)
  @ellipsis = opts.fetch(:ellipsis, "?")
  @format = opts.fetch(:format, nil)
end

Instance Attribute Details

#alignSymbol

Returns `:left`, `:right`.

Returns:

  • (Symbol)

    `:left`, `:right`.



158
159
160
# File 'lib/term_utils/tab.rb', line 158

def align
  @align
end

#ellipsisString

Returns:

  • (String)


162
163
164
# File 'lib/term_utils/tab.rb', line 162

def ellipsis
  @ellipsis
end

#fixedBoolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/term_utils/tab.rb', line 160

def fixed
  @fixed
end

#formatProc, ...

Returns:

  • (Proc, String, nil)


164
165
166
# File 'lib/term_utils/tab.rb', line 164

def format
  @format
end

#idSymbol

Returns:

  • (Symbol)


152
153
154
# File 'lib/term_utils/tab.rb', line 152

def id
  @id
end

#indexInteger

Returns:

  • (Integer)


154
155
156
# File 'lib/term_utils/tab.rb', line 154

def index
  @index
end

#widthInteger

Returns:

  • (Integer)


156
157
158
# File 'lib/term_utils/tab.rb', line 156

def width
  @width
end

Instance Method Details

#align_cut(str) ⇒ String

Aligns and cuts a given string.

Parameters:

  • str (String)

Returns:

  • (String)


197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/term_utils/tab.rb', line 197

def align_cut(str)
  if @align == :left
    # Align left
    if @fixed and (str.length > @width)
      str = "#{str[0..(@width - (@ellipsis.length + 1))]}#{@ellipsis}"
    else
      str = "%-*s" % [@width, str]
    end
  else
    # Align right
    if @fixed and (str.length > @width)
      str = "#{@ellipsis}#{str[(str.length - @width + @ellipsis.length)..(str.length - 1)]}"
    else
      str = "%*s" % [@width, str]
    end
  end
  str
end

#render_data(v) ⇒ Object

Renders a given value. return [String]

Parameters:

  • v (Object)


226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/term_utils/tab.rb', line 226

def render_data(v)
  str = v
  if v
    if @format.is_a? Proc
      str = @format.call(v)
    elsif @format.is_a? String
      str = @format % v
    end
  end
  str = str.to_s unless str.is_a? String
  align_cut str
end

#render_header(v) ⇒ Object

Renders a given header. return [String]

Parameters:

  • v (Object)


218
219
220
221
222
# File 'lib/term_utils/tab.rb', line 218

def render_header(v)
  str = v
  str = str.to_s unless str.is_a? String
  align_cut str
end

#validatenil

Validates the column represented by this one.

Returns:

  • (nil)


184
185
186
187
188
189
190
191
192
193
# File 'lib/term_utils/tab.rb', line 184

def validate
  raise "missing column id (nil)" if @id.nil?
  raise "missing column index (nil)" if @index.nil?
  raise "wrong column index (not integer)" unless @index.is_a? Integer
  raise "wrong column index (not >= 0)" if @index < 0
  raise "missing column width (nil)" if @width.nil?
  raise "wrong column width (not integer)" unless @width.is_a? Integer
  raise "wrong column width (not > 0)" if @width <= 0
  raise "wrong column align (not :left or :right)" unless %i{left right}.index @align
end