Skip to content

Instantly share code, notes, and snippets.

@brendon9x
Created February 24, 2015 15:17
Show Gist options
  • Save brendon9x/f42a9aebfc5541570dfb to your computer and use it in GitHub Desktop.
Save brendon9x/f42a9aebfc5541570dfb to your computer and use it in GitHub Desktop.
Pretty route printer for Rails
desc 'Pretty version on rails rake routes.'
black = "\033[1;30m"
red = "\033[1;31m"
yellow = "\033[1;33m"
blue = "\033[1;34m"
magenta = "\033[1;35m"
cyan = "\033[1;36m"
white = "\033[1;37m"
no_color = "\033[0m"
task :pretty_routes => :environment do
rows, cols = []
begin
rows, cols = `stty size`.scan(/\d+/).map { |s| s.to_i }
rescue
# ignored
end
Rails.application.reload_routes!
all_routes = Rails.application.routes.routes.to_a
all_routes.reject! { |route| route.verb.nil? || route.path.spec.to_s == '/assets' }
all_routes.select! { |route| ENV['CONTROLLER'].nil? || route.defaults[:controller].to_s == ENV['CONTROLLER'] }
max_widths = {
names: (all_routes.map { |route| route.name.to_s.length }.max),
verbs: (6),
paths: (all_routes.map { |route| route.path.spec.to_s.length }.max),
controllers: (all_routes.map { |route| route.defaults[:controller].to_s.length }.max),
actions: (all_routes.map { |route| route.defaults[:action].to_s.length }.max)
}
cols_spread = {
names: 0.40,
paths: 0.60
}
wrap_lines = ->(opts) do
term_width = cols.to_i
fixed_width = (opts[:cols].keys - opts[:cols_to_wrap]).map { |c| max_widths[c] }.sum
remaining_width = term_width - fixed_width - (opts[:cols_to_wrap].size - 1) * 1 - 10
wrapped_cols = opts[:cols_to_wrap].each_with_object({}) do |c, hash|
max_widths[c] = (cols_spread[c] * remaining_width).round
hash[c] = opts[:cols][c].scan(/.{1,#{max_widths[c]}}/)
end
max_lines = wrapped_cols.values.map { |array| array.size }.max
max_lines.times.map do |i|
opts[:cols].map do |c, content|
if wrapped_cols[c] && i < wrapped_cols[c].length
wrapped_cols[c][i]
elsif i == 0
content
else
""
end
end
end
end
full_line_printer = ->(name, verb, path, action) do
name = cyan + name.rjust(max_widths[:names]) + no_color
verb = yellow + verb.center(max_widths[:verbs]) + no_color
path = red + path.ljust(max_widths[:paths]).gsub(/\.?:\w+/) { |s| blue + s + red } + no_color
action = white + action.ljust(max_widths[:actions]) + no_color
puts "#{name} | #{verb} | #{path} | #{action}"
end
wrapping_line_printer = ->(name, verb, path, action) do
wrapped_lines = wrap_lines[cols: {names: name, verbs: verb, paths: path, actions: action}, cols_to_wrap: [:names, :paths]]
wrapped_lines.each do |line|
full_line_printer[*line]
end
end
printer = cols ? wrapping_line_printer : full_line_printer
all_routes.group_by { |route| route.defaults[:controller] }.each_value do |group|
puts magenta + "\nCONTROLLER: " + white + group.first.defaults[:controller].to_s + no_color
group.each do |route|
name = route.name.to_s
verb = route.verb.inspect.gsub(/^.{2}|.{2}$/, "")
path = route.path.spec.to_s
action = route.defaults[:action].to_s
printer[name, verb, path, action]
end
end
end
@brendon9x
Copy link
Author

Credit to https://github.com/nicooga/color_routes, but added wrapping and made friendly for dark background terminals

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment