Class: Lisp::Format::Directives::Tabulate

Inherits:
Directive show all
Defined in:
lib/carat/lisp-format.rb

Overview

Represents the ~T (Tabulate) directive. This tabulates to a given position in the output using white-space.

Instance Attribute Summary

Attributes inherited from Directive

#pos

Instance Method Summary collapse

Methods inherited from Directive

#initialize, #join

Constructor Details

This class inherits a constructor from Lisp::Format::Directives::Directive

Instance Method Details

#execute(state) ⇒ Object

The output is spaced over to a given position, depending on where it already is and parameters given to this directive.

~colnum,colinc:@T

with the following interpretations

colnum (1)

column to move to,

colinc (1)

number of columns to space over by if already at or beyond colnum,

@

performs relative tabulation. colnum is treated as the column to begin from (spacing over to it if necessary), and then moves over to a column that is the smallest multiple of colinc.

If output is already at or beyond colnum, then output is spaced over to column colnum + k * colinc, for the smallest k possible. An example of the @ modifiers effect is for the instance of the ~T directive ~3,8@T, which moves over three columns, and then to the first eight-sized tab-stop.



1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
# File 'lib/carat/lisp-format.rb', line 1368

def execute(state)
  colnum = param(0, state, 1)
  colinc = param(1, state, 1)
  padchar = ?\s.chr
  if at_mod?
    state.output(padchar * colnum)
    state.output(padchar * (colinc - state.col % colinc))
  else
    if state.col < colnum
      state.output(padchar * (colnum - state.col))
    elsif colinc > 0
      k = 1 + (state.col - colnum) / colinc
      state.output(padchar * ((colnum + k * colinc) - state.col))
    end
  end
end