@@ -5,21 +5,14 @@ def initialize(out: STDOUT)
55
66 def print_traffic_lights ( lights :)
77 lights . each do |light |
8- # Quick inline way to colour states, done in a messy way rather than a dedicated method.
9- state_colour = case light [ :state ]
10- when "red" then "\e [31m#{ light [ :state ] } \e [0m"
11- when "green" then "\e [32m#{ light [ :state ] } \e [0m"
12- when "amber" then "\e [33m#{ light [ :state ] } \e [0m"
13- else light [ :state ]
14- end
15- puts ( "Direction: #{ light [ :direction ] } , State: #{ state_colour } , Time left: #{ light [ :timer ] } s" )
8+ puts ( "Direction: #{ light [ :direction ] } , State: #{ state_colour ( state : light [ :state ] ) } , Time left: #{ light [ :timer ] } s" )
169 end
1710 end
1811
1912 def print_pedestrian_signals ( signals :)
2013 puts "\n Pedestrian signals:"
2114 signals . each do |direction , can_walk |
22- signal_text = can_walk ? " \e [32mWALK \e [0m" : " \e [31mDON 'T WALK\e [0m"
15+ signal_text = can_walk ? colourizer ( :green , "WALK" ) : colourizer ( :red , "DON 'T WALK" )
2316 puts " #{ direction } : #{ signal_text } "
2417 end
2518
@@ -28,5 +21,17 @@ def print_pedestrian_signals(signals:)
2821
2922 private
3023
24+ def state_colour ( state :) = colourizer ( state . to_sym , state )
25+
26+ def colourizer ( color , input )
27+ case color
28+ when :red then "\e [31m#{ input } \e [0m"
29+ when :green then "\e [32m#{ input } \e [0m"
30+ when :amber then "\e [33m#{ input } \e [0m"
31+ else
32+ input
33+ end
34+ end
35+
3136 attr_reader :out
3237end
0 commit comments