Skip to content

Commit

Permalink
Use cli for printing
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Jan 31, 2025
1 parent 49647ed commit 94a75ac
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
10 changes: 6 additions & 4 deletions R/route.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Route <- R6Class('Route',
#'
print = function(...) {
n_handlers <- length(ls(private$handlerStore))
cat('A route with ', n_handlers, ' handlers\n', sep = '')
cli::cli_text('A route with {n_handlers} handler{?s}')
if (n_handlers != 0) {
method_order <- c('get', 'head', 'post', 'put', 'delete', 'connect', 'options', 'trace', 'patch', 'all')
reg_methods <- names(private$handlerMap)
Expand All @@ -116,10 +116,12 @@ Route <- R6Class('Route',
method_length <- max(nchar(reg_methods))
for (i in order(map_order)) {
paths <- names(private$handlerMap[[reg_methods[i]]])
cat(format(reg_methods[i], width = method_length), ': ', paths[1], '\n', sep = '')
for(j in 1 + seq_len(length(paths) - 1)) {
cat(format(' ', width = method_length), ': ', paths[j], '\n', sep = '')
cli::cli_text('{.emph {reg_methods[i]}:}')
id <- cli::cli_ul()
for(j in seq_along(paths)) {
cli::cli_li(paths[j])
}
cli::cli_end(id)
}
}
return(invisible(self))
Expand Down
7 changes: 4 additions & 3 deletions R/routestack.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,13 @@ RouteStack <- R6Class('RouteStack',
#'
print = function(...) {
n_routes <- length(private$stack) + length(private$assets)
cat('A RouteStack containing ', n_routes, ' routes\n', sep = '')
cli::cli_text('A RouteStack containing {n_routes} route{?s}')
cli::cli_ol()
for (i in seq_along(private$assets)) {
cat(format(i, width = nchar(n_routes)), ': ', private$routeNames[i], ' (assset route)\n', sep = '')
cli::cli_li('{private$assetNames[i]} (asset route)')
}
for (i in seq_along(private$stack)) {
cat(format(i, width = nchar(n_routes)), ': ', private$routeNames[i], '\n', sep = '')
cli::cli_li('{private$routeNames[i]}')
}
invisible(self)
},
Expand Down
18 changes: 10 additions & 8 deletions tests/testthat/_snaps/route.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@

Code
route$print()
Output
Message
A route with 0 handlers

---

Code
route$print()
Output
A route with 1 handlers
get: /test
Message
A route with 1 handler
get:
* /test

# handlers can get added and removed

Code
route$print()
Output
A route with 1 handlers
get: /test
Message
A route with 1 handler
get:
* /test

---

Code
route$print()
Output
Message
A route with 0 handlers

---
Expand Down
26 changes: 13 additions & 13 deletions tests/testthat/_snaps/routestack.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@

Code
router$print()
Output
Message
A RouteStack containing 0 routes

---

Code
router$print()
Output
A RouteStack containing 1 routes
1: test
Message
A RouteStack containing 1 route
1. test

# routes can be added, queried, and removed

Code
router$print()
Output
A RouteStack containing 1 routes
1: test
Message
A RouteStack containing 1 route
1. test

---

Code
router$print()
Output
Message
A RouteStack containing 0 routes

---
Expand All @@ -48,12 +48,12 @@

Code
router$print()
Output
Message
A RouteStack containing 4 routes
1: test3
2: test
3: test4
4: test2
1. test3
2. test
3. test4
4. test2

# attach_to and name works as expected

Expand Down

0 comments on commit 94a75ac

Please sign in to comment.