Module: Inkjet::Indent
- Defined in:
- lib/inkjet/indent.rb
Defined Under Namespace
Modules: String
Classes: Formatter
Constant Summary
collapse
- TABSTOP =
2
Class Method Summary
collapse
Class Method Details
.add_bindings(block) ⇒ Object
18
19
20
21
22
23
24
|
# File 'lib/inkjet/indent.rb', line 18
def self.add_bindings(block)
block.binding.eval("def puts(output=''); Inkjet::Indent.puts(output); end")
block.binding.eval("def print(output=''); Inkjet::Indent.print(output); end")
block.binding.eval("def format_with(meth, *args); Inkjet::Indent.format_with(meth, *args); end")
block.binding.eval("def undent(&block); Inkjet::Indent.undent(&block); end")
block
end
|
64
65
66
67
|
# File 'lib/inkjet/indent.rb', line 64
def self.apply_formatters(output)
formatters.each { |f| output = f.call_on(output) }
output
end
|
60
61
62
|
# File 'lib/inkjet/indent.rb', line 60
def self.format_with(fmeth, *fargs)
formatters.push(Formatter.new(fmeth, *fargs))
end
|
56
57
58
|
# File 'lib/inkjet/indent.rb', line 56
def self.formatters
@@formatters ||= []
end
|
.indent(*args, &block) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/inkjet/indent.rb', line 26
def self.indent(*args, &block)
@@spaces ||= 0
if block_given?
@@spaces += args[0] || TABSTOP
scoped_formatters = formatters.clone
add_bindings(block).call
@@formatters = scoped_formatters
@@spaces -= args[0] || TABSTOP
else
spaces = args[1] || TABSTOP
"#{padding(spaces.to_i)}#{args[0].to_s.split("\n").join("\n#{padding(spaces.to_i)}")}"
end
end
|
.padding(spaces) ⇒ Object
69
70
71
|
# File 'lib/inkjet/indent.rb', line 69
def self.padding(spaces)
spaces.times.map {" "}.join
end
|
.print(output = '') ⇒ Object
77
78
79
|
# File 'lib/inkjet/indent.rb', line 77
def self.print(output='')
STDOUT.print apply_formatters(indent(output, @@spaces))
end
|
.puts(output = '') ⇒ Object
73
74
75
|
# File 'lib/inkjet/indent.rb', line 73
def self.puts(output='')
STDOUT.puts apply_formatters(indent(output, @@spaces))
end
|
.undent(&block) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/inkjet/indent.rb', line 42
def self.undent(&block)
@@spaces ||= 0
if block_given?
scoped_spaces = @@spaces
@@spaces = 0
scoped_formatters = formatters.clone
add_bindings(block).call
@@formatters = scoped_formatters
@@spaces = scoped_spaces
end
end
|