Skip to content

Commit 71fbe82

Browse files
authored
Ruby: Add colored CLI output for the parse and lex commnads (#776)
<img width="1995" height="1496" alt="CleanShot 2025-11-04 at 00 28 16@2x" src="https://github.com/user-attachments/assets/a085f899-7e06-446f-b718-795001998df4" /> <img width="2890" height="1752" alt="CleanShot 2025-11-04 at 00 26 50@2x" src="https://github.com/user-attachments/assets/a30a9c51-781e-4c23-a2bc-e469f7f4d291" />
1 parent 1c90bed commit 71fbe82

File tree

12 files changed

+328
-24
lines changed

12 files changed

+328
-24
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Metrics/AbcSize:
8080
- lib/herb/project.rb
8181
- lib/herb/engine.rb
8282
- lib/herb/engine/**/*.rb
83+
- lib/herb/token.rb
8384
- templates/template.rb
8485
- test/fork_helper.rb
8586
- test/snapshot_utils.rb

lib/herb.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22
# typed: false
33

4+
require_relative "herb/colors"
45
require_relative "herb/range"
56
require_relative "herb/position"
67
require_relative "herb/location"

lib/herb/colors.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# frozen_string_literal: true
2+
# typed: true
3+
4+
# rbs_inline: enabled
5+
6+
module Herb
7+
module Colors
8+
module_function
9+
10+
#: () -> bool
11+
def enabled?
12+
return false if ENV["NO_COLOR"]
13+
return false if defined?(IRB)
14+
return false if defined?(Minitest)
15+
16+
$stdout.tty?
17+
end
18+
19+
#: (String) -> String
20+
def white(string)
21+
return string unless enabled?
22+
23+
"\e[37m#{string}\e[0m"
24+
end
25+
26+
#: (String) -> String
27+
def yellow(string)
28+
return string unless enabled?
29+
30+
"\e[33m#{string}\e[0m"
31+
end
32+
33+
#: (String) -> String
34+
def green(string)
35+
return string unless enabled?
36+
37+
"\e[32m#{string}\e[0m"
38+
end
39+
40+
#: (String) -> String
41+
def red(string)
42+
return string unless enabled?
43+
44+
"\e[31m#{string}\e[0m"
45+
end
46+
47+
#: (String) -> String
48+
def magenta(string)
49+
return string unless enabled?
50+
51+
"\e[35m#{string}\e[0m"
52+
end
53+
54+
#: (String) -> String
55+
def cyan(string)
56+
return string unless enabled?
57+
58+
"\e[36m#{string}\e[0m"
59+
end
60+
61+
#: (String) -> String
62+
def bright_magenta(string)
63+
return string unless enabled?
64+
65+
"\e[95m#{string}\e[0m"
66+
end
67+
68+
#: (String) -> String
69+
def dimmed(string)
70+
return string unless enabled?
71+
72+
"\e[2m#{string}\e[0m"
73+
end
74+
75+
#: (String) -> String
76+
def bold(string)
77+
return string unless enabled?
78+
79+
"\e[1m#{string}\e[0m"
80+
end
81+
end
82+
end

lib/herb/location.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ def to_json(state = nil)
4040

4141
#: () -> String
4242
def tree_inspect
43-
%((location: #{start.tree_inspect}-#{self.end.tree_inspect}))
43+
"#{start.tree_inspect}-#{self.end.tree_inspect}"
4444
end
4545

4646
#: () -> String
4747
def inspect
48-
%(#<Herb::Location #{tree_inspect}>)
48+
%(#<Herb::Location (location: #{tree_inspect})>)
4949
end
5050
end
5151
end

lib/herb/token.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
module Herb
55
class Token
6+
include Colors
7+
68
attr_reader :value #: String
79
attr_reader :range #: Range
810
attr_reader :location #: Location
@@ -33,7 +35,7 @@ def to_json(state = nil)
3335

3436
#: () -> String
3537
def tree_inspect
36-
%("#{value.force_encoding("utf-8")}" #{location.tree_inspect})
38+
"#{green("\"#{value.force_encoding("utf-8")}\"")} #{dimmed("(location: #{location.tree_inspect})")}"
3739
end
3840

3941
#: () -> String
@@ -45,9 +47,19 @@ def value_inspect
4547
end
4648
end
4749

50+
#: () -> String
51+
def colorize_range
52+
white("[") + cyan(range.from.to_s) + white(", ") + cyan(range.to.to_s) + white("]")
53+
end
54+
55+
#: (Position) -> String
56+
def colorize_position(position)
57+
white("(") + cyan(position.line.to_s) + white(":") + cyan(position.column.to_s) + white(")")
58+
end
59+
4860
#: () -> String
4961
def inspect
50-
%(#<Herb::Token type="#{type}" value=#{value_inspect} range=#{range.tree_inspect} start=#{location.start.tree_inspect} end=#{location.end.tree_inspect}>)
62+
"#{white("#<")}#{bold(yellow("Herb::Token"))} #{white("type=")}#{bright_magenta("\"#{type}\"")} #{white("value=")}#{green(value_inspect)} #{white("range=")}#{colorize_range} #{white("start=")}#{colorize_position(location.start)} #{white("end=")}#{colorize_position(location.end)}#{white(">")}"
5163
end
5264
end
5365
end

0 commit comments

Comments
 (0)