Skip to content

Commit e205ab9

Browse files
authored
Merge pull request #103 from Shopify/at-remove-colorize
Use our own shell coloration helper
2 parents 2125f60 + 5c09912 commit e205ab9

File tree

11 files changed

+77
-80
lines changed

11 files changed

+77
-80
lines changed

Gemfile.lock

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ PATH
22
remote: .
33
specs:
44
spoom (1.1.4)
5-
colorize
65
sorbet (>= 0.5.9204)
76
sorbet-runtime (>= 0.5.9204)
87
thor (>= 0.19.2)
@@ -13,7 +12,6 @@ GEM
1312
ast (2.4.1)
1413
byebug (11.1.3)
1514
coderay (1.1.3)
16-
colorize (0.8.1)
1715
method_source (1.0.0)
1816
minitest (5.14.3)
1917
parallel (1.19.2)

lib/spoom.rb

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

4-
require "colorize"
54
require "sorbet-runtime"
65

76
module Spoom
@@ -27,6 +26,7 @@ def self.exec(cmd, *arg, path: '.', capture_err: false)
2726
end
2827
end
2928

29+
require "spoom/colors"
3030
require "spoom/sorbet"
3131
require "spoom/cli"
3232
require "spoom/version"

lib/spoom/cli/helper.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module Helper
1111
extend T::Sig
1212
extend T::Helpers
1313

14+
include Colorize
15+
1416
requires_ancestor { Thor }
1517

1618
# Print `message` on `$stdout`
@@ -84,7 +86,7 @@ def sorbet_config
8486
# Colors
8587

8688
# Color used to highlight expressions in backticks
87-
HIGHLIGHT_COLOR = :blue
89+
HIGHLIGHT_COLOR = T.let(Spoom::Color::BLUE, Spoom::Color)
8890

8991
# Is the `--color` option true?
9092
sig { returns(T::Boolean) }
@@ -116,35 +118,35 @@ def highlight(string)
116118
end
117119

118120
# Colorize a string if `color?`
119-
sig { params(string: String, color: Symbol).returns(String) }
120-
def colorize(string, color)
121+
sig { params(string: String, color: Color).returns(String) }
122+
def colorize(string, *color)
121123
return string unless color?
122-
string.colorize(color)
124+
T.unsafe(self).set_color(string, *color)
123125
end
124126

125127
sig { params(string: String).returns(String) }
126128
def blue(string)
127-
colorize(string, :blue)
129+
colorize(string, Color::BLUE)
128130
end
129131

130132
sig { params(string: String).returns(String) }
131133
def gray(string)
132-
colorize(string, :light_black)
134+
colorize(string, Color::LIGHT_BLACK)
133135
end
134136

135137
sig { params(string: String).returns(String) }
136138
def green(string)
137-
colorize(string, :green)
139+
colorize(string, Color::GREEN)
138140
end
139141

140142
sig { params(string: String).returns(String) }
141143
def red(string)
142-
colorize(string, :red)
144+
colorize(string, Color::RED)
143145
end
144146

145147
sig { params(string: String).returns(String) }
146148
def yellow(string)
147-
colorize(string, :yellow)
149+
colorize(string, Color::YELLOW)
148150
end
149151
end
150152
end

lib/spoom/colors.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
module Spoom
5+
class Color < T::Enum
6+
extend T::Sig
7+
8+
enums do
9+
CLEAR = new("\e[0m")
10+
BOLD = new("\e[1m")
11+
12+
BLACK = new("\e[30m")
13+
RED = new("\e[31m")
14+
GREEN = new("\e[32m")
15+
YELLOW = new("\e[33m")
16+
BLUE = new("\e[34m")
17+
MAGENTA = new("\e[35m")
18+
CYAN = new("\e[36m")
19+
WHITE = new("\e[37m")
20+
21+
LIGHT_BLACK = new("\e[90m")
22+
LIGHT_RED = new("\e[91m")
23+
LIGHT_GREEN = new("\e[92m")
24+
LIGHT_YELLOW = new("\e[93m")
25+
LIGHT_BLUE = new("\e[94m")
26+
LIGHT_MAGENTA = new("\e[95m")
27+
LIGHT_CYAN = new("\e[96m")
28+
LIGHT_WHITE = new("\e[97m")
29+
end
30+
31+
sig { returns(String) }
32+
def ansi_code
33+
serialize
34+
end
35+
end
36+
37+
module Colorize
38+
extend T::Sig
39+
40+
sig { params(string: String, color: Color).returns(String) }
41+
def set_color(string, *color)
42+
"#{color.map(&:ansi_code).join}#{string}#{Color::CLEAR.ansi_code}"
43+
end
44+
end
45+
end

lib/spoom/file_tree.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def print_node(node)
156156
end
157157
print("\n")
158158
else
159-
print_colored(node.name, :blue)
159+
print_colored(node.name, Color::BLUE)
160160
print("/")
161161
printn
162162
indent
@@ -180,15 +180,15 @@ def node_strictness(node)
180180
Spoom::Sorbet::Sigils.file_strictness(path)
181181
end
182182

183-
sig { params(strictness: T.nilable(String)).returns(Symbol) }
183+
sig { params(strictness: T.nilable(String)).returns(Color) }
184184
def strictness_color(strictness)
185185
case strictness
186186
when "false"
187-
:red
187+
Color::RED
188188
when "true", "strict", "strong"
189-
:green
189+
Color::GREEN
190190
else
191-
:uncolored
191+
Color::CLEAR
192192
end
193193
end
194194
end

lib/spoom/printer.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Printer
88
extend T::Sig
99
extend T::Helpers
1010

11+
include Colorize
12+
1113
abstract!
1214

1315
sig { returns(T.any(IO, StringIO)) }
@@ -42,11 +44,10 @@ def print(string)
4244
# Print `string` colored with `color` into `out`
4345
#
4446
# Does not use colors unless `@colors`.
45-
sig { params(string: T.nilable(String), color: Symbol, colors: Symbol).void }
46-
def print_colored(string, color, *colors)
47+
sig { params(string: T.nilable(String), color: Color).void }
48+
def print_colored(string, *color)
4749
return unless string
48-
string = colorize(string, color)
49-
colors.each { |c| string = colorize(string, c) }
50+
string = T.unsafe(self).colorize(string, *color)
5051
@out.print(string)
5152
end
5253

@@ -72,9 +73,10 @@ def printt
7273
end
7374

7475
# Colorize `string` with color if `@colors`
75-
sig { params(string: String, color: Symbol).returns(String) }
76-
def colorize(string, color)
77-
@colors ? string.colorize(color) : string
76+
sig { params(string: String, color: Spoom::Color).returns(String) }
77+
def colorize(string, *color)
78+
return string unless @colors
79+
T.unsafe(self).set_color(string, *color)
7880
end
7981
end
8082
end

lib/spoom/sorbet/lsp/structures.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def self.from_json(json)
5656

5757
sig { override.params(printer: SymbolPrinter).void }
5858
def accept_printer(printer)
59-
printer.print_colored("#{line}:#{char}", :light_black)
59+
printer.print_colored("#{line}:#{char}", Color::LIGHT_BLACK)
6060
end
6161

6262
def to_s
@@ -81,7 +81,7 @@ def self.from_json(json)
8181
sig { override.params(printer: SymbolPrinter).void }
8282
def accept_printer(printer)
8383
printer.print_object(start)
84-
printer.print_colored("-", :light_black)
84+
printer.print_colored("-", Color::LIGHT_BLACK)
8585
printer.print_object(self.end)
8686
end
8787

@@ -106,7 +106,7 @@ def self.from_json(json)
106106

107107
sig { override.params(printer: SymbolPrinter).void }
108108
def accept_printer(printer)
109-
printer.print_colored("#{printer.clean_uri(uri)}:", :light_black)
109+
printer.print_colored("#{printer.clean_uri(uri)}:", Color::LIGHT_BLACK)
110110
printer.print_object(range)
111111
end
112112

@@ -203,14 +203,14 @@ def accept_printer(printer)
203203
printer.printt
204204
printer.print(kind_string)
205205
printer.print(' ')
206-
printer.print_colored(name, :blue, :bold)
207-
printer.print_colored(' (', :light_black)
206+
printer.print_colored(name, Color::BLUE, Color::BOLD)
207+
printer.print_colored(' (', Color::LIGHT_BLACK)
208208
if range
209209
printer.print_object(range)
210210
elsif location
211211
printer.print_object(location)
212212
end
213-
printer.print_colored(')', :light_black)
213+
printer.print_colored(')', Color::LIGHT_BLACK)
214214
printer.printn
215215
unless children.empty?
216216
printer.indent

sorbet/rbi/gems/colorize@0.8.1.rbi

Lines changed: 0 additions & 39 deletions
This file was deleted.

sorbet/rbi/gems/rake@10.5.0.rbi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,9 +789,7 @@ RakeFileUtils = Rake::FileUtilsExt
789789
class String
790790
include(::Comparable)
791791
include(::JSON::Ext::Generator::GeneratorMethods::String)
792-
include(::Colorize::InstanceMethods)
793792
extend(::JSON::Ext::Generator::GeneratorMethods::String::Extend)
794-
extend(::Colorize::ClassMethods)
795793

796794
def ext(newext = _); end
797795
def pathmap(spec = _, &block); end

sorbet/rbi/shims/colorize.rbi

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)