Skip to content

Commit e753dc4

Browse files
committed
feat: xbreaks and ybreaks arguments
* `cat_piece()` and `str_piece()` gain arguments `xbreaks` and `ybreaks` to provide a subset (of integers) to provide axis labels for if `annotate` is `TRUE` (#17). closes #17
1 parent c66646c commit e753dc4

File tree

8 files changed

+70
-25
lines changed

8 files changed

+70
-25
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Encoding: UTF-8
22
Package: ppcli
33
Type: Package
44
Title: Plaintext Board Game Visualizations
5-
Version: 0.2.0-1
5+
Version: 0.2.0-2
66
Authors@R: c(person("Trevor L.", "Davis", role=c("aut", "cre"),
77
email="trevor.l.davis@gmail.com",
88
comment = c(ORCID = "0000-0001-6341-4639")))

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ ppcli 0.2.0 (development)
1111
* "white" `go` and `checkers` bits should now render the same
1212
whether `piece_side` is `"bit_back"` or `"bit_face"`.
1313

14+
* `cat_piece()` and `str_piece()` gain arguments `xbreaks` and `ybreaks`
15+
to provide a subset (of integers) to provide axis labels for if `annotate` is `TRUE` (#17).
16+
1417
ppcli 0.1.1
1518
===========
1619

R/cat_piece.r

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
#' @seealso [str_piece()] for just the character vector. See <https://github.com/trevorld/game-bit-font> for more information about the \dQuote{Game Bit} family of fonts.
1515
#' @export
1616
cat_piece <- function(df, color = NULL, reorient = "none", annotate = FALSE, ...,
17-
file = "", annotation_scale = NULL, style = c("Unicode", "Game Bit Mono", "Game Bit Duo")) {
17+
file = "", annotation_scale = NULL,
18+
style = c("Unicode", "Game Bit Mono", "Game Bit Duo"),
19+
xbreaks = NULL, ybreaks = NULL) {
1820
color <- color %||% (is.null(file) || file == "")
1921
s <- str_piece(df, color = color, reorient = reorient, annotate = annotate,
20-
annotation_scale = annotation_scale, style = style)
22+
annotation_scale = annotation_scale, style = style,
23+
xbreaks = xbreaks, ybreaks = ybreaks)
2124
if (!is.null(file)) cat(s, ..., file = file)
2225
invisible(s)
2326
}

R/str_piece.r

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,24 @@
2424
#' @param style If "Unicode" (default) only use glyphs in Unicode proper.
2525
#' If "Game Bit Duo" use glyphs in Private Use Area of "Game Bit Duo" font.
2626
#' If "Game Bit Mono" use glyphs in Private Use Area of "Game Bit Mono" font.
27+
#' @param xbreaks,ybreaks Subset (of integers) to provide axis labels for if `annotate` is `TRUE`.
28+
#' If `NULL` infer a reasonable choice.
2729
#' @return Character vector for text diagram.
2830
#' @seealso [cat_piece()] for printing to the terminal.
2931
#' See <https://github.com/trevorld/game-bit-font> for more information about the \dQuote{Game Bit} family of fonts.
3032
#' @export
3133
str_piece <- function(df, color = NULL, reorient = "none", annotate = FALSE, ...,
32-
annotation_scale = NULL, style = c("Unicode", "Game Bit Mono", "Game Bit Duo")) {
34+
annotation_scale = NULL,
35+
style = c("Unicode", "Game Bit Mono", "Game Bit Duo"),
36+
xbreaks = NULL, ybreaks = NULL) {
3337
str_piece_helper(df, ..., color = color, reorient = reorient, annotate = annotate, ...,
34-
annotation_scale = annotation_scale, style = style)
38+
annotation_scale = annotation_scale, style = style,
39+
xbreaks = xbreaks, ybreaks = ybreaks)
3540
}
3641
str_piece_helper <- function(df, color = NULL, reorient = "none", annotate = FALSE, ...,
3742
xoffset = NULL, yoffset = NULL,
38-
annotation_scale = NULL, style = "Unicode") {
43+
annotation_scale = NULL,
44+
style = "Unicode", xbreaks = NULL, ybreaks = NULL) {
3945
annotation_scale <- annotation_scale %||% attr(df, "scale_factor") %||% 1
4046
if (nrow(df) == 0) {
4147
return(character(0))
@@ -63,7 +69,7 @@ str_piece_helper <- function(df, color = NULL, reorient = "none", annotate = FAL
6369
cfg <- as.character(df[rr, "cfg"])
6470
cm <- add_piece(cm, ps, suit, rank, x, y, angle, cfg, reorient, style)
6571
}
66-
cm <- annotate_text(cm, nc, nr, offset$x, offset$y, annotate, annotation_scale)
72+
cm <- annotate_text(cm, nc, nr, offset$x, offset$y, annotate, annotation_scale, xbreaks, ybreaks)
6773
cm <- color_text(cm, color)
6874
text <- rev(apply(cm$char, 1, function(x) paste(x, collapse = "")))
6975
text <- paste(text, collapse = "\n")
@@ -280,24 +286,45 @@ col_cli <- function(col = c("black", "blue", "cyan", "green", "magenta", "red",
280286
get(paste0("col_", col), envir = getNamespace("cli"))
281287
}
282288

283-
annotate_text <- function(cm, nc, nr, xoffset, yoffset, annotate, annotation_scale) {
289+
annotate_text <- function(cm, nc, nr, xoffset, yoffset, annotate, annotation_scale,
290+
xbreaks, ybreaks) {
284291
if (isFALSE(annotate) || annotate == "none") return(cm)
285292
step <- 2 * annotation_scale
286-
x <- seq(1 + step + 2 * xoffset, nc, by = step)
293+
294+
if (is.null(xbreaks)) {
295+
x <- seq(1 + step + 2 * xoffset, nc, by = step)
296+
} else {
297+
xbreaks <- as.integer(xbreaks)
298+
x <- seq(1 + step + 2 * xoffset, by = step, length.out = max(xbreaks))
299+
}
287300
if (annotate == "cartesian") {
288301
x <- utils::head(x, 9)
289302
xt <- as.character(seq_along(x))
290-
cm$char[1, x] <- xt
291303
} else {
292304
if (length(x) > 26) x <- x[1:26]
293-
cm$char[1, x] <- letters[seq_along(x)]
305+
xt <- letters[seq_along(x)]
306+
}
307+
if (!is.null(xbreaks)) {
308+
x <- x[xbreaks]
309+
xt <- xt[xbreaks]
310+
}
311+
cm$char[1, x] <- xt
312+
313+
if (is.null(ybreaks)) {
314+
y <- seq(1 + step + 2 * yoffset, nr, by= step)
315+
} else {
316+
ybreaks <- as.integer(ybreaks)
317+
y <- seq(1 + step + 2 * yoffset, by= step, length.out = max(ybreaks))
294318
}
295-
y <- seq(1 + step + 2 * yoffset, nr, by= step)
296319
yt <- as.character(seq_along(y))
297320
if (length(yt) > 9) {
298321
yt <- stringr::str_pad(yt, 2, "right")
299322
cm$char[y[-seq(9)], 2L] <- substr(yt[-seq(9)], 2, 2)
300323
}
324+
if (!is.null(ybreaks)) {
325+
y <- y[ybreaks]
326+
yt <- yt[ybreaks]
327+
}
301328
cm$char[y, 1L] <- substr(yt, 1L, 1L)
302329
cm
303330
}

man/cat_piece.Rd

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/str_piece.Rd

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/cat_piece.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -555,17 +555,18 @@
555555
│◌ ◌ ◌ ◌│
556556
└───────┘
557557
Code
558-
cat_piece(df)
558+
cat_piece(df, xbreaks = 1:7, ybreaks = 1:7, annotate = TRUE, annotation_scale = 0.5)
559559
Output
560-
┌───────┐
561-
│○ ● ● ●│
562-
│ ● ● ● │
563-
│● ● ● ●│
564-
│ ● ● ● │
565-
│● ● ● ●│
566-
│ ● ● ● │
567-
│● ● ● ●│
568-
└───────┘
560+
┌───────┐
561+
7│○ ● ● ●│
562+
6│ ● ● ● │
563+
5│● ● ● ●│
564+
4│ ● ● ● │
565+
3│● ● ● ●│
566+
2│ ● ● ● │
567+
1│● ● ● ●│
568+
└───────┘
569+
abcdefg
569570

570571
---
571572

tests/testthat/test_cat_piece.r

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ test_that("text diagrams", {
182182
)
183183
df <- rbind(dfb, dfm)
184184
cat_piece(dfb)
185-
cat_piece(df)
185+
cat_piece(df, xbreaks = 1:7, ybreaks = 1:7,
186+
annotate = TRUE, annotation_scale = 0.5)
186187
})
187188

188189
# alquerque

0 commit comments

Comments
 (0)