Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8686c19

Browse files
committed
Better output formatting for the CLI.
1 parent 2febac5 commit 8686c19

File tree

2 files changed

+76
-20
lines changed

2 files changed

+76
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
2323
- Support singleton single-line method definitions.
2424
- Support `stree-ignore` comments to ignore formatting nodes.
2525
- Add special formatting for arrays of `VarRef` nodes whose sum width is greater than 2 * the maximum width.
26+
- Better output formatting for the CLI.
2627

2728
### Changed
2829

lib/syntax_tree/cli.rb

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,63 @@
22

33
class SyntaxTree
44
module CLI
5+
# A utility wrapper around colored strings in the output.
6+
class ColoredString
7+
COLORS = { default: "0", gray: "38;5;102", yellow: "33" }
8+
9+
attr_reader :code, :string
10+
11+
def initialize(color, string)
12+
@code = COLORS[color]
13+
@string = string
14+
end
15+
16+
def to_s
17+
"\033[#{code}m#{string}\033[0m"
18+
end
19+
end
20+
21+
# The parent action class for the CLI that implements the basics.
22+
class Action
23+
def run(filepath, source)
24+
end
25+
26+
def success
27+
end
28+
29+
def failure
30+
end
31+
end
32+
533
# An action of the CLI that prints out the AST for the given source.
6-
class AST
34+
class AST < Action
735
def run(filepath, source)
836
pp SyntaxTree.parse(source)
937
end
1038
end
1139

1240
# An action of the CLI that formats the source twice to check if the first
1341
# format is not idempotent.
14-
class Check
42+
class Check < Action
1543
def run(filepath, source)
1644
formatted = SyntaxTree.format(source)
17-
raise if formatted != SyntaxTree.format(formatted)
45+
return true if formatted == SyntaxTree.format(formatted)
46+
47+
puts "[#{ColoredString.new(:yellow, "warn")}] #{filepath}"
48+
false
49+
end
50+
51+
def success
52+
puts "All files matched expected format."
53+
end
54+
55+
def failure
56+
warn("The listed files did not match the expected format.")
1857
end
1958
end
2059

2160
# An action of the CLI that prints out the doc tree IR for the given source.
22-
class Doc
61+
class Doc < Action
2362
def run(filepath, source)
2463
formatter = Formatter.new([])
2564
SyntaxTree.parse(source).format(formatter)
@@ -28,17 +67,26 @@ def run(filepath, source)
2867
end
2968

3069
# An action of the CLI that formats the input source and prints it out.
31-
class Format
70+
class Format < Action
3271
def run(filepath, source)
3372
puts SyntaxTree.format(source)
3473
end
3574
end
3675

3776
# An action of the CLI that formats the input source and writes the
3877
# formatted output back to the file.
39-
class Write
78+
class Write < Action
4079
def run(filepath, source)
41-
File.write(filepath, SyntaxTree.format(source))
80+
print filepath
81+
start = Time.now
82+
83+
formatted = SyntaxTree.format(source)
84+
File.write(filepath, formatted)
85+
86+
delta = ((Time.now - start) * 1000).round
87+
color = source == formatted ? :gray : :default
88+
89+
puts "\r#{ColoredString.new(color, filepath)} #{delta}ms"
4290
end
4391
end
4492

@@ -81,15 +129,32 @@ def run(argv)
81129
errored = false
82130
patterns.each do |pattern|
83131
Dir.glob(pattern).each do |filepath|
84-
errored |= run_for(action, filepath) if File.file?(filepath)
132+
next unless File.file?(filepath)
133+
134+
begin
135+
action.run(filepath, source_for(filepath))
136+
rescue => error
137+
warn("!!! Failed on #{filepath}")
138+
warn(error.message)
139+
warn(error.backtrace)
140+
errored = true
141+
end
85142
end
86143
end
87-
88-
errored ? 1 : 0
144+
145+
if errored
146+
action.failure
147+
1
148+
else
149+
action.success
150+
0
151+
end
89152
end
90153

91154
private
92155

156+
# Returns the source from the given filepath taking into account any
157+
# potential magic encoding comments.
93158
def source_for(filepath)
94159
encoding =
95160
File.open(filepath, "r") do |file|
@@ -100,16 +165,6 @@ def source_for(filepath)
100165

101166
File.read(filepath, encoding: encoding)
102167
end
103-
104-
def run_for(action, filepath)
105-
action.run(filepath, source_for(filepath))
106-
false
107-
rescue => error
108-
warn("!!! Failed on #{filepath}")
109-
warn(error.message)
110-
warn(error.backtrace)
111-
true
112-
end
113168
end
114169
end
115170
end

0 commit comments

Comments
 (0)