plotcli-py 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. CLAUDE.md +51 -0
  2. LICENSE +21 -0
  3. PKG-INFO +358 -0
  4. README.md +340 -0
  5. main.py +6 -0
  6. plotcli-original/.Rbuildignore +18 -0
  7. plotcli-original/.github/workflows/deploy_docs.yml +43 -0
  8. plotcli-original/.gitignore +46 -0
  9. plotcli-original/DESCRIPTION +25 -0
  10. plotcli-original/NAMESPACE +60 -0
  11. plotcli-original/NEWS.md +112 -0
  12. plotcli-original/R/ascii_escape.r +13 -0
  13. plotcli-original/R/canvas.r +586 -0
  14. plotcli-original/R/class_functions.r +114 -0
  15. plotcli-original/R/geom_registry.r +1376 -0
  16. plotcli-original/R/ggplotcli.r +234 -0
  17. plotcli-original/R/ggplotcli_helpers.r +1099 -0
  18. plotcli-original/R/helper_functions.r +351 -0
  19. plotcli-original/R/plotcli.r +963 -0
  20. plotcli-original/R/plotcli_grid.r +1 -0
  21. plotcli-original/R/plotcli_wrappers.r +416 -0
  22. plotcli-original/R/zzz.r +15 -0
  23. plotcli-original/README.md +192 -0
  24. plotcli-original/docs/ascii.png +0 -0
  25. plotcli-original/docs/bar.png +0 -0
  26. plotcli-original/docs/block.png +0 -0
  27. plotcli-original/docs/boxplot.png +0 -0
  28. plotcli-original/docs/density.png +0 -0
  29. plotcli-original/docs/facet.png +0 -0
  30. plotcli-original/docs/facet_grid.png +0 -0
  31. plotcli-original/docs/generate_png.sh +137 -0
  32. plotcli-original/docs/heatmap.png +0 -0
  33. plotcli-original/docs/histogram.png +0 -0
  34. plotcli-original/docs/line.png +0 -0
  35. plotcli-original/docs/noborder.png +0 -0
  36. plotcli-original/docs/scatter.png +0 -0
  37. plotcli-original/docs/showcase.R +182 -0
  38. plotcli-original/inst/doc/ggplotcli.R +231 -0
  39. plotcli-original/inst/doc/ggplotcli.Rmd +329 -0
  40. plotcli-original/inst/doc/ggplotcli.html +1078 -0
  41. plotcli-original/inst/doc/plotcli_class.R +98 -0
  42. plotcli-original/inst/doc/plotcli_class.Rmd +121 -0
  43. plotcli-original/inst/doc/plotcli_class.html +564 -0
  44. plotcli-original/inst/doc/plotcli_wrappers.R +35 -0
  45. plotcli-original/inst/doc/plotcli_wrappers.Rmd +62 -0
  46. plotcli-original/inst/doc/plotcli_wrappers.html +546 -0
  47. plotcli-original/man/AsciiCanvas.Rd +116 -0
  48. plotcli-original/man/BlockCanvas.Rd +132 -0
  49. plotcli-original/man/BrailleCanvas.Rd +146 -0
  50. plotcli-original/man/Canvas.Rd +492 -0
  51. plotcli-original/man/GeomRegistry.Rd +9 -0
  52. plotcli-original/man/add_legend_to_output.Rd +12 -0
  53. plotcli-original/man/braille_dot_bit.Rd +29 -0
  54. plotcli-original/man/braille_set_dot.Rd +21 -0
  55. plotcli-original/man/bresenham.Rd +27 -0
  56. plotcli-original/man/build_plot_output.Rd +28 -0
  57. plotcli-original/man/build_plot_output_v2.Rd +41 -0
  58. plotcli-original/man/cat_plot_matrix.Rd +17 -0
  59. plotcli-original/man/cbind.plotcli.Rd +19 -0
  60. plotcli-original/man/cbind_plots.Rd +17 -0
  61. plotcli-original/man/color_to_term.Rd +18 -0
  62. plotcli-original/man/create_canvas.Rd +21 -0
  63. plotcli-original/man/create_panel_scales.Rd +29 -0
  64. plotcli-original/man/create_scales.Rd +27 -0
  65. plotcli-original/man/dot-geom_registry.Rd +16 -0
  66. plotcli-original/man/draw_border.Rd +12 -0
  67. plotcli-original/man/draw_grid.Rd +12 -0
  68. plotcli-original/man/extract_legend_info.Rd +12 -0
  69. plotcli-original/man/extract_plot_labels.Rd +12 -0
  70. plotcli-original/man/extract_plot_style.Rd +12 -0
  71. plotcli-original/man/format_axis_label.Rd +18 -0
  72. plotcli-original/man/format_four_chars.Rd +21 -0
  73. plotcli-original/man/geom_area_handler.Rd +12 -0
  74. plotcli-original/man/geom_bar_handler.Rd +12 -0
  75. plotcli-original/man/geom_boxplot_handler.Rd +13 -0
  76. plotcli-original/man/geom_density_handler.Rd +12 -0
  77. plotcli-original/man/geom_histogram_handler.Rd +12 -0
  78. plotcli-original/man/geom_hline_handler.Rd +12 -0
  79. plotcli-original/man/geom_line_handler.Rd +12 -0
  80. plotcli-original/man/geom_path_handler.Rd +12 -0
  81. plotcli-original/man/geom_point_handler.Rd +12 -0
  82. plotcli-original/man/geom_rect_handler.Rd +12 -0
  83. plotcli-original/man/geom_segment_handler.Rd +12 -0
  84. plotcli-original/man/geom_smooth_handler.Rd +12 -0
  85. plotcli-original/man/geom_text_handler.Rd +12 -0
  86. plotcli-original/man/geom_vline_handler.Rd +12 -0
  87. plotcli-original/man/get_color_hue.Rd +18 -0
  88. plotcli-original/man/get_data_subset.Rd +23 -0
  89. plotcli-original/man/get_facet_info.Rd +18 -0
  90. plotcli-original/man/get_geom_handler.Rd +17 -0
  91. plotcli-original/man/get_term_colors.Rd +21 -0
  92. plotcli-original/man/ggplotcli.Rd +83 -0
  93. plotcli-original/man/init_color_mapping.Rd +15 -0
  94. plotcli-original/man/is_braille.Rd +20 -0
  95. plotcli-original/man/is_geom_registered.Rd +17 -0
  96. plotcli-original/man/list_registered_geoms.Rd +14 -0
  97. plotcli-original/man/make_colored.Rd +23 -0
  98. plotcli-original/man/make_unique_names.Rd +20 -0
  99. plotcli-original/man/normalize_data.Rd +27 -0
  100. plotcli-original/man/pclib.Rd +48 -0
  101. plotcli-original/man/pclibx.Rd +46 -0
  102. plotcli-original/man/pclid.Rd +44 -0
  103. plotcli-original/man/pclih.Rd +50 -0
  104. plotcli-original/man/pclil.Rd +48 -0
  105. plotcli-original/man/pclis.Rd +48 -0
  106. plotcli-original/man/pixel_to_braille.Rd +23 -0
  107. plotcli-original/man/plotcli.Rd +598 -0
  108. plotcli-original/man/plotcli_bar.Rd +48 -0
  109. plotcli-original/man/plotcli_box.Rd +46 -0
  110. plotcli-original/man/plotcli_density.Rd +44 -0
  111. plotcli-original/man/plotcli_histogram.Rd +50 -0
  112. plotcli-original/man/plotcli_line.Rd +48 -0
  113. plotcli-original/man/plotcli_options.Rd +18 -0
  114. plotcli-original/man/plotcli_scatter.Rd +48 -0
  115. plotcli-original/man/plus-.plotcli.Rd +19 -0
  116. plotcli-original/man/rbind.plotcli.Rd +19 -0
  117. plotcli-original/man/rbind_plots.Rd +17 -0
  118. plotcli-original/man/register_geom.Rd +21 -0
  119. plotcli-original/man/remove_color_codes.Rd +21 -0
  120. plotcli-original/man/render_faceted_plot.Rd +25 -0
  121. plotcli-original/man/render_single_panel.Rd +12 -0
  122. plotcli-original/man/safe_aes_name.Rd +18 -0
  123. plotcli-original/tests/testthat/test-new-geoms.R +136 -0
  124. plotcli-original/tests/testthat/test-plotcli.R +69 -0
  125. plotcli-original/tests/testthat.R +4 -0
  126. plotcli-original/vignettes/ggplotcli.Rmd +329 -0
  127. plotcli-original/vignettes/plotcli_class.R +98 -0
  128. plotcli-original/vignettes/plotcli_class.Rmd +121 -0
  129. plotcli-original/vignettes/plotcli_wrappers.R +35 -0
  130. plotcli-original/vignettes/plotcli_wrappers.Rmd +62 -0
  131. plotcli.egg-info/PKG-INFO +11 -0
  132. plotcli.egg-info/SOURCES.txt +7 -0
  133. plotcli.egg-info/dependency_links.txt +1 -0
  134. plotcli.egg-info/entry_points.txt +3 -0
  135. plotcli.egg-info/top_level.txt +1 -0
  136. plotcli.py +978 -0
  137. plotcli_py-0.1.0.dist-info/METADATA +358 -0
  138. plotcli_py-0.1.0.dist-info/RECORD +143 -0
  139. plotcli_py-0.1.0.dist-info/WHEEL +4 -0
  140. plotcli_py-0.1.0.dist-info/entry_points.txt +2 -0
  141. plotcli_py-0.1.0.dist-info/licenses/LICENSE +21 -0
  142. pyproject.toml +31 -0
  143. uv.lock +8 -0
@@ -0,0 +1,351 @@
1
+ #' @importFrom crayon blue red green yellow magenta cyan silver white black
2
+ NULL
3
+
4
+ #' Print plot matrix
5
+ #'
6
+ #' This function prints a plot matrix to the console.
7
+ #'
8
+ #' @param plot_matrix The plot matrix to be printed.
9
+ #' @export
10
+ #'
11
+ #' @examples
12
+ #' cat_plot_matrix(matrix(c("a", "b", "c", "d"), nrow = 2, ncol = 2))
13
+ cat_plot_matrix <- function(plot_matrix) {
14
+ cat("\n")
15
+ for (i in 1:nrow(plot_matrix)) {
16
+ cat(paste(plot_matrix[i, ], collapse = ""), "\n")
17
+ }
18
+ }
19
+
20
+ #' Make colored text
21
+ #'
22
+ #' This function applies a specified color to a given text string.
23
+ #'
24
+ #' @param x The text string to be colored.
25
+ #' @param color The color to be applied to the text. If NULL, the color codes will be removed.
26
+ #' @return A colored text string or a text string with color codes removed.
27
+ #' @export
28
+ #'
29
+ #' @examples
30
+ #' make_colored("Hello, world!", "blue")
31
+ #' make_colored("Hello, world!", NULL)
32
+ make_colored <- function(x,
33
+ color = NULL) {
34
+ if (is.null(color)) {
35
+ out <- remove_color_codes(x)
36
+ } else {
37
+ color_function <- get(color, mode = "function", envir = asNamespace("crayon"))
38
+ out <- color_function(remove_color_codes(x))
39
+ }
40
+ return(out)
41
+ }
42
+
43
+ #' Bresenham's line algorithm
44
+ #'
45
+ #' This function generates a list of points that form a line between two given points using Bresenham's line algorithm.
46
+ #'
47
+ #' @param x0 The x-coordinate of the starting point.
48
+ #' @param y0 The y-coordinate of the starting point.
49
+ #' @param x1 The x-coordinate of the ending point.
50
+ #' @param y1 The y-coordinate of the ending point.
51
+ #' @return A list of points that form a line between the two given points.
52
+ #' @export
53
+ #'
54
+ #' @examples
55
+ #' bresenham(0, 0, 5, 5)
56
+ #' bresenham(0, 0, -5, -5)
57
+ bresenham <- function(x0,
58
+ y0,
59
+ x1,
60
+ y1) {
61
+ points <- list()
62
+ dx <- abs(x1 - x0)
63
+ dy <- abs(y1 - y0)
64
+ sx <- ifelse(x0 < x1, 1, -1)
65
+ sy <- ifelse(y0 < y1, 1, -1)
66
+ err <- dx - dy
67
+
68
+ while (TRUE) {
69
+ points <- append(points, list(c(x0, y0)))
70
+
71
+ if (x0 == x1 && y0 == y1) {
72
+ break
73
+ }
74
+
75
+ e2 <- 2 * err
76
+ if (e2 > -dy) {
77
+ err <- err - dy
78
+ x0 <- x0 + sx
79
+ }
80
+ if (e2 < dx) {
81
+ err <- err + dx
82
+ y0 <- y0 + sy
83
+ }
84
+ }
85
+
86
+ return(points)
87
+ }
88
+
89
+ #' Get terminal colors
90
+ #'
91
+ #' This function returns a vector of terminal colors.
92
+ #'
93
+ #' @param n The number of colors to return.
94
+ #' @return A vector of terminal colors.
95
+ #' @export
96
+ #'
97
+ #' @examples
98
+ #' get_term_colors(5)
99
+ #' get_term_colors(10)
100
+ get_term_colors <- function(n = NULL) {
101
+ term_colors <- c(
102
+ "blue",
103
+ "red",
104
+ "green",
105
+ "yellow",
106
+ "magenta",
107
+ # "grey",
108
+ "cyan",
109
+ "silver"
110
+ )
111
+
112
+ if (is.null(n)) n <- length(term_colors)
113
+
114
+ if (n > length(term_colors)) term_colors <- rep(term_colors, times = ceiling(n / length(term_colors)))
115
+
116
+ return(term_colors[1:n])
117
+ }
118
+
119
+ #' Normalize data
120
+ #'
121
+ #' This function normalizes the given data to a specified plot range.
122
+ #'
123
+ #' @param data The data to be normalized.
124
+ #' @param data_min The minimum value of the data.
125
+ #' @param data_max The maximum value of the data.
126
+ #' @param plot_range The range to normalize the data to.
127
+ #' @return The normalized data.
128
+ #' @export
129
+ #'
130
+ #' @examples
131
+ #' normalize_data(c(1, 2, 3, 4, 5), 1, 5, 10)
132
+ #' normalize_data(c(10, 20, 30, 40, 50), 10, 50, 100)
133
+ normalize_data <- function(data,
134
+ data_min,
135
+ data_max,
136
+ plot_range) {
137
+ if (data_max == data_min) {
138
+ return(rep(round(plot_range / 2), length(data)))
139
+ }
140
+ val <- ((data - data_min) / (data_max - data_min) * (plot_range)) + 1
141
+ val[val < 1] <- 1
142
+ val[val > plot_range] <- plot_range
143
+ return(val)
144
+ }
145
+
146
+ #' Format number to four characters
147
+ #'
148
+ #' This function formats a number to a string of exactly four characters.
149
+ #'
150
+ #' @param num The number to be formatted.
151
+ #' @return A string representation of the number with exactly four characters.
152
+ #' @export
153
+ #'
154
+ #' @examples
155
+ #' format_four_chars(123)
156
+ #' format_four_chars(-12.34)
157
+ format_four_chars <- function(num) {
158
+ if (num < 0) {
159
+ # Negative numbers
160
+ if (num <= -100) {
161
+ formatted_num <- sprintf("%0.0e", num) # Use scientific notation
162
+ } else {
163
+ formatted_num <- sprintf("%.1f", num) # One decimal place
164
+ }
165
+ } else {
166
+ # Positive numbers
167
+ if (num < 100) {
168
+ formatted_num <- sprintf("%.1f", num) # One decimal place
169
+ } else {
170
+ formatted_num <- sprintf("%.0f", num) # No decimal places
171
+ }
172
+ }
173
+
174
+ # Ensure the string is exactly 4 characters long
175
+ if (nchar(formatted_num) < 4) {
176
+ formatted_num <- stringr::str_pad(formatted_num, width = 4, side = "left", pad = " ")
177
+ } else if (nchar(formatted_num) > 4) {
178
+ formatted_num <- substr(formatted_num, 1, 4)
179
+ }
180
+
181
+ return(formatted_num)
182
+ }
183
+
184
+ #' Remove color codes from a string
185
+ #'
186
+ #' This function removes ANSI color codes from a given text string.
187
+ #'
188
+ #' @param s The text string containing ANSI color codes.
189
+ #' @return A text string with ANSI color codes removed.
190
+ #' @export
191
+ #'
192
+ #' @examples
193
+ #' colored_text <- make_colored("Hello, world!", "blue")
194
+ #' remove_color_codes(colored_text)
195
+ remove_color_codes <- function(s) {
196
+ # ANSI color code pattern
197
+ ansi_pattern <- "\033\\[\\d+(;\\d+)?m"
198
+ # Remove the ANSI color codes using gsub
199
+ gsub(ansi_pattern, "", s, perl = TRUE)
200
+ }
201
+
202
+ #' Check if a character is a Braille character
203
+ #'
204
+ #' This function checks if a given character is a Braille character.
205
+ #'
206
+ #' @param char The character to be checked.
207
+ #' @return A boolean value indicating whether the character is a Braille character or not.
208
+ #' @export
209
+ #'
210
+ #' @examples
211
+ #' is_braille("A")
212
+ is_braille <- function(char) {
213
+ if (is.null(char) || length(char) == 0 || nchar(char) == 0) return(FALSE)
214
+ # Get the first character's code (ignore ANSI escape sequences)
215
+ code <- utf8ToInt(char)
216
+ if (length(code) == 0) return(FALSE)
217
+ # Check only the first code point
218
+ code <- code[1]
219
+ return(code >= 0x2800 && code <= 0x28FF)
220
+ }
221
+
222
+ #' Get Braille dot bit value
223
+ #'
224
+ #' Returns the bit value for a specific dot position in a Braille cell.
225
+ #' Braille cells have a 2x4 dot matrix with the following bit values:
226
+ #'
227
+ #' \preformatted{
228
+ #' Col 0 Col 1
229
+ #' Row 0: 0x01 0x08
230
+ #' Row 1: 0x02 0x10
231
+ #' Row 2: 0x04 0x20
232
+ #' Row 3: 0x40 0x80
233
+ #' }
234
+ #'
235
+ #' @param dot_row Row within the Braille cell (0-3, top to bottom)
236
+ #' @param dot_col Column within the Braille cell (0-1, left to right)
237
+ #' @return The bit value for that dot position
238
+ #' @export
239
+ braille_dot_bit <- function(dot_row, dot_col) {
240
+ # Braille dot bit mapping
241
+ # Left column (col 0): rows 0-2 are bits 0-2, row 3 is bit 6
242
+ # Right column (col 1): rows 0-2 are bits 3-5, row 3 is bit 7
243
+ if (dot_col == 0) {
244
+ if (dot_row < 3) {
245
+ return(bitwShiftL(1L, dot_row))
246
+ } else {
247
+ return(0x40L) # bit 6
248
+ }
249
+ } else {
250
+ if (dot_row < 3) {
251
+ return(bitwShiftL(1L, dot_row + 3))
252
+ } else {
253
+ return(0x80L) # bit 7
254
+ }
255
+ }
256
+ }
257
+
258
+ #' Set a dot in a Braille character
259
+ #'
260
+ #' @param current_char The current character (can be space or existing Braille)
261
+ #' @param dot_row Row within the Braille cell (0-3)
262
+ #' @param dot_col Column within the Braille cell (0-1)
263
+ #' @return The updated Braille character
264
+ #' @export
265
+ braille_set_dot <- function(current_char, dot_row, dot_col) {
266
+ # Get current code point
267
+ if (is.null(current_char) || current_char == " " || !is_braille(current_char)) {
268
+ current_code <- 0x2800L
269
+ } else {
270
+ current_code <- utf8ToInt(current_char)
271
+ }
272
+
273
+ # Get the bit for this dot position
274
+ dot_bit <- braille_dot_bit(dot_row, dot_col)
275
+
276
+ # Set the bit using OR
277
+ new_code <- bitwOr(current_code, dot_bit)
278
+
279
+ return(intToUtf8(new_code))
280
+ }
281
+
282
+ #' Convert pixel coordinates to Braille cell and dot position
283
+ #'
284
+ #' @param px Pixel x coordinate (1-based)
285
+ #' @param py Pixel y coordinate (1-based, from top)
286
+ #' @param canvas_rows Number of character rows in canvas
287
+ #' @param canvas_cols Number of character columns in canvas
288
+ #' @return List with cell_row, cell_col, dot_row, dot_col
289
+ #' @export
290
+ pixel_to_braille <- function(px, py, canvas_rows, canvas_cols) {
291
+ # Braille gives us 2x horizontal and 4x vertical resolution
292
+ # px ranges from 1 to canvas_cols * 2
293
+ # py ranges from 1 to canvas_rows * 4
294
+
295
+ # Cell position (1-based)
296
+ cell_col <- ((px - 1) %/% 2) + 1
297
+ cell_row <- ((py - 1) %/% 4) + 1
298
+
299
+ # Dot position within cell (0-based)
300
+ dot_col <- (px - 1) %% 2
301
+ dot_row <- (py - 1) %% 4
302
+
303
+ # Clamp to valid range
304
+ cell_col <- max(1, min(cell_col, canvas_cols))
305
+ cell_row <- max(1, min(cell_row, canvas_rows))
306
+
307
+ return(list(
308
+ cell_row = cell_row,
309
+ cell_col = cell_col,
310
+ dot_row = dot_row,
311
+ dot_col = dot_col
312
+ ))
313
+ }
314
+
315
+ #' Make unique names
316
+ #'
317
+ #' This function takes a vector of names and ensures that each name is unique by appending a number if necessary.
318
+ #'
319
+ #' @param names A character vector of names.
320
+ #' @return A character vector of unique names.
321
+ #' @export
322
+ #'
323
+ #' @examples
324
+ #' make_unique_names(c("apple", "apple", "banana", "apple"))
325
+ make_unique_names <- function(names) {
326
+ unique_names <- character(length(names))
327
+ for (i in seq_along(names)) {
328
+ name <- names[i]
329
+ count <- 1
330
+ while (name %in% unique_names) {
331
+ name <- paste(names[i], count, sep = "_")
332
+ count <- count + 1
333
+ }
334
+ unique_names[i] <- name
335
+ }
336
+ return(unique_names)
337
+ }
338
+
339
+ #' Set global options for plotcli
340
+ #'
341
+ #' @param plot_width Default plot width (default: 60)
342
+ #' @param plot_height Default plot height (default: 20)
343
+ #' @param braille Default braille setting (default: FALSE)
344
+ #' @export
345
+ plotcli_options <- function(plot_width = 60, plot_height = 20, braille = FALSE) {
346
+ options(
347
+ plotcli.plot_width = plot_width,
348
+ plotcli.plot_height = plot_height,
349
+ plotcli.braille = braille
350
+ )
351
+ }