diff --git a/lib/hanami/router/formatter/human_friendly.rb b/lib/hanami/router/formatter/human_friendly.rb index cf96fa5a..19619ca5 100644 --- a/lib/hanami/router/formatter/human_friendly.rb +++ b/lib/hanami/router/formatter/human_friendly.rb @@ -33,23 +33,51 @@ class HumanFriendly EXTRA_LARGE_STRING_JUSTIFY_AMOUNT = 40 private_constant :EXTRA_LARGE_STRING_JUSTIFY_AMOUNT + # @api private + # @since 2.0.0 + COLUMN_SPACING = 5 + private_constant :COLUMN_SPACING + # @api private # @since 2.0.0 def call(routes) - routes.filter_map(&method(:format_route_unless_head)).join(NEW_LINE) + filtered_routes = routes.reject(&:head?) + return "" if filtered_routes.empty? + + column_widths = calculate_column_widths(filtered_routes) + filtered_routes.map { |route| format_route(route, column_widths) }.join(NEW_LINE) end private - def format_route_unless_head(route) - !route.head? && - [ - route.http_method.to_s.ljust(SMALL_STRING_JUSTIFY_AMOUNT), - route.path.ljust(LARGE_STRING_JUSTIFY_AMOUNT), - route.inspect_to.ljust(LARGE_STRING_JUSTIFY_AMOUNT), - route.as? ? "as #{route.inspect_as}".ljust(MEDIUM_STRING_JUSTIFY_AMOUNT) : "", - route.constraints? ? "(#{route.inspect_constraints})".ljust(EXTRA_LARGE_STRING_JUSTIFY_AMOUNT) : "" - ].join + def calculate_column_widths(routes) + path_width = LARGE_STRING_JUSTIFY_AMOUNT + inspect_to_width = LARGE_STRING_JUSTIFY_AMOUNT + as_width = MEDIUM_STRING_JUSTIFY_AMOUNT + constraints_width = EXTRA_LARGE_STRING_JUSTIFY_AMOUNT + + routes.each do |route| + path_width = [path_width, route.path.length].max + inspect_to_width = [inspect_to_width, route.inspect_to.length].max + as_width = [as_width, route.as? ? "as #{route.inspect_as}".length : 0].max + constraints_width = [constraints_width, + route.constraints? ? "(#{route.inspect_constraints})".length : 0].max + end + + # Include spacing in the widths + [path_width + COLUMN_SPACING, inspect_to_width + COLUMN_SPACING, as_width + COLUMN_SPACING, constraints_width] + end + + def format_route(route, column_widths) + path_width, inspect_to_width, as_width, constraints_width = column_widths + + [ + route.http_method.to_s.ljust(SMALL_STRING_JUSTIFY_AMOUNT), + route.path.ljust(path_width), + route.inspect_to.ljust(inspect_to_width), + route.as? ? "as #{route.inspect_as}".ljust(as_width) : "", + route.constraints? ? "(#{route.inspect_constraints})".ljust(constraints_width) : "" + ].join end end end diff --git a/spec/integration/hanami/router/inspection_spec.rb b/spec/integration/hanami/router/inspection_spec.rb index cc2b9c0f..02fb3484 100644 --- a/spec/integration/hanami/router/inspection_spec.rb +++ b/spec/integration/hanami/router/inspection_spec.rb @@ -17,8 +17,8 @@ it "inspects the routes" do expected = [ - "GET / (proc) as :root", - "GET /api (proc) as :api_root" + "GET / (proc) as :root ", + "GET /api (proc) as :api_root " ] actual = inspector.call diff --git a/spec/unit/hanami/router/formatter/human_friendly_spec.rb b/spec/unit/hanami/router/formatter/human_friendly_spec.rb index df262399..cd9ff06c 100644 --- a/spec/unit/hanami/router/formatter/human_friendly_spec.rb +++ b/spec/unit/hanami/router/formatter/human_friendly_spec.rb @@ -16,7 +16,7 @@ Hanami::Router::Route.new(http_method: "GET", path: "/resources/:id", to: "resource#show", as: :resource, constraints: {id: /\d+/}) ] - expected = "GET /resources/:id resource#show as :resource (id: /\\d+/) " + expected = "GET /resources/:id resource#show as :resource (id: /\\d+/) " expect(subject.call(routes)).to eq(expected) end