Class: Dispersion::Formatters::ANSI

Inherits:
Object
  • Object
show all
Defined in:
lib/dispersion/formatters/ansi.rb

Constant Summary collapse

Regular =
"\e[22m"
Bold =
"\e[1m"
Faint =
"\e[2m"
Reset =
"\e[0m"
Italic =
"\e[3m"
Underline =
"\e[4m"
Black =

Basic Colors

"\e[30m"
Red =
"\e[31m"
Green =
"\e[32m"
Yellow =
"\e[33m"
Blue =
"\e[34m"
Magenta =
"\e[35m"
Cyan =
"\e[36m"
White =
"\e[37m"
BrightBlack =

Bright Colors

"\e[90m"
BrightRed =
"\e[91m"
BrightGreen =
"\e[92m"
BrightYellow =
"\e[93m"
BrightBlue =
"\e[94m"
BrightMagenta =
"\e[95m"
BrightCyan =
"\e[96m"
BrightWhite =
"\e[97m"
DEFAULT_ANSI =
{
	call: Red,
	string: Yellow,
	keyword: Cyan,
	comment: Italic + Magenta,
	var: "",
	constant: Blue,
	ivar: Blue,
	symbol: Green,
	numeric: Green,
	bracket: "",
	operator: "\e[36m",
}

Instance Method Summary collapse

Constructor Details

#initialize(code, annotations:, theme:) ⇒ ANSI

Returns a new instance of ANSI.



47
48
49
50
51
# File 'lib/dispersion/formatters/ansi.rb', line 47

def initialize(code, annotations:, theme:)
	@code = code
	@annotations = annotations
	@marks = DEFAULT_ANSI
end

Instance Method Details

#callObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dispersion/formatters/ansi.rb', line 53

def call
	lines = @code.lines
	current_symbol = nil
	buffer = +""

	lines.each_with_index do |line, line_number|
		cursor = 0

		@annotations[line_number].each do |(symbol, column)|
			buffer << line[cursor...column]
			cursor = column

			# Always reset the current symbol
			if current_symbol
				buffer << Reset
				current_symbol = nil
			end

			unless symbol == :reset
				buffer << @marks.fetch(symbol)
				current_symbol = symbol
			end
		end

		buffer << line[cursor..]
	end

	if current_symbol
		buffer << Reset
		current_symbol = nil
	end

	buffer.gsub(/\t/, "  ")
end