Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added compression #3

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(alamak)
export(makePixPal)
importFrom(cli,console_width)
importFrom(crayon,combine_styles)
importFrom(crayon,make_style)
importFrom(crayon,red)
importFrom(grDevices,rgb)
Expand Down
140 changes: 106 additions & 34 deletions R/alamak.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
#' Generates a pixel pal from a PNG file
#'
#' @param filepath a character indicating a path to a .PNG file.
#' @param compress logical, should the Pixel Pal be compressed to take up fewer
#' spaces in the terminal? Default is TRUE.
#'
#' @return a character string converted to color ASCII through \code{crayon}
#'
#' @importFrom png readPNG
#' @importFrom crayon make_style
#' @importFrom crayon make_style combine_styles
#' @importFrom grDevices rgb
#'
#' @examples
Expand All @@ -34,21 +36,25 @@
#'
#' @export

makePixPal <- function(filepath) {
makePixPal <- function(filepath, compress = TRUE) {

pix = readPNG(filepath, native = FALSE)

pixcols = rgb(pix[,,1], pix[,,2], pix[,,3], pix[,,4])

pix = matrix(pixcols, nrow = dim(pix)[1], byrow = TRUE)

crayonstrings <- matrix(
unlist(sapply(pixcols, function(x) {
make_style(x, bg = TRUE)(" ")})),
nrow = nrow(pix))
if(compress) {
crayonstrings <- compressPixPal(pix)
} else {
crayonstrings <- matrix(
unlist(sapply(pixcols, function(x) {
make_style(x, bg = TRUE)(" ")})),
nrow = nrow(pix))

crayonstrings[t(pix) == "#00000000"] = " "
crayonstrings[,ncol(crayonstrings)] <- paste(crayonstrings[,ncol(crayonstrings)], "\n", sep = "")
crayonstrings[t(pix) == "#00000000"] = " "
crayonstrings[,ncol(crayonstrings)] <- paste(crayonstrings[,ncol(crayonstrings)], "\n", sep = "")
}
return(crayonstrings)
}

Expand All @@ -70,15 +76,23 @@ makePixPal <- function(filepath) {
#'
#' @examples
#'
#' # Error message
#' alamak(a + 4)
#'
#' # Warning message
#' alamak(as.numeric("alamak"))
#'
#' # Change Pixel Pal
#' alamak(as.numeric("alamak"), "Lenny")
#'
#' @importFrom crayon red
#' @importFrom cli console_width
#'
#' @export

alamak <- function(f, pixpal = "Jerry"){

# Input checks
if(all(class(pixpal) == "character" & pixpal %in% c("Jerry", "Lenny", "Buster", "Oniji", "E10N"))) {
pixpal = pixpals[[pixpal]]
} else if(class(pixpal) != "list") {
Expand All @@ -92,36 +106,37 @@ alamak <- function(f, pixpal = "Jerry"){
stop("Not a valid Pixel Pal! Needs \"Error\" and \"Warning\" elements. See ?makePixPal for an example")
}

warns = list()
errs = list()
#Error catching
warnings = list()
errors = list()

out <- tryCatch(withCallingHandlers(
expr = f,

warning = function(w) {
warns <<- c(warns, list(w))
invokeRestart("muffleWarning")
}),

warnings <<- c(warnings, list(w))
invokeRestart("muffleWarning")}),
error = function(e) {
errs <<- c(errs, list(e))
}
errors <<- c(errors, list(e))}
)

if(length(errs) > 0 | length(warns) > 0) {
if(length(errs) > 0){
if(length(errors) > 0 | length(warnings) > 0) {

if(length(errors) > 0){
errtype = "Error"
errmess = paste0(errtype, ": ", unlist(lapply(errs, function(x) {
errmess = paste0(errtype, ": ", unlist(lapply(errors, function(x) {
mess = gsub(x = as.character(x$message), pattern = "\\033\\[[0-9][0-9+]m", replacement = "", perl = TRUE)
return(mess)
})))
} else if(length(warns) > 0) {
} else if(length(warnings) > 0) {
errtype = "Warning"
errmess = paste0(errtype, ": ", unlist(lapply(warns, function(x) {
errmess = paste0(errtype, ": ", unlist(lapply(warnings, function(x) {
mess = gsub(x = as.character(x$message), pattern = "\\033\\[[3[0-9+]m", replacement = "", perl = TRUE)
return(mess)
})))
}

# Message wrangling

pal = pixpal$crayon

consolew = min(2*console_width() - 2 - ncol(pal), 100)
Expand All @@ -143,11 +158,8 @@ alamak <- function(f, pixpal = "Jerry"){

max_text_col = column_sizes[max_col_size]

#print(max_text_col)
#print(message_to_add[1])

finalmess = c(" ", rep("_", max_text_col), "____\n|",
rep(" ", max_text_col), " |\n")
finalmess = c("\u250c", rep("\u2500", max_text_col + 3), "\u2510\n\u2502",
rep(" ", max_text_col), " \u2502\n")

for(i in message_to_add) {
if(grepl("\\033\\[31", i, perl = TRUE)) {
Expand All @@ -156,19 +168,20 @@ alamak <- function(f, pixpal = "Jerry"){
padding = nchar(i)
}
finalmess = c(finalmess,
"| ", i,
rep(" ", max_text_col - padding), " |\n")
"\u2502 ", i,
rep(" ", max_text_col - padding), " \u2502\n")
}

finalmess = c(finalmess, "|_", rep("_", max_text_col), "___|\n")
finalmess = c(finalmess, "\u2502", rep(" ", max_text_col + 3), "\u2502\n",
"\u2514", rep("\u2500", max_text_col + 3), "\u2518\n")

finalmess_print = unlist(strsplit(paste0(finalmess, collapse = ""), split = "\n"))

difference = length(finalmess_print) - nrow(pal)
if(difference > 0) {
pal = rbind(pal,
do.call(rbind,
lapply(seq_len(difference), function(x) c(rep(" ", ncol(pal)-1), " \n"))
lapply(seq_len(difference), function(x) c(rep(" ", ncol(pal)-1), "\n"))
)
)
}
Expand All @@ -181,12 +194,71 @@ alamak <- function(f, pixpal = "Jerry"){
crayonmap_with_message[right_lines[i]] = gsub(x = crayonmap[right_lines[i]],
pattern = "\n",
replacement = paste0(" ", finalmess_print[i], "\n"))
}
cat(crayonmap_with_message, sep = "")
}

# Printing
cat("\n", crayonmap_with_message, "\n", sep = "")
}

if(length(errs) == 0) return(out)
if(length(errors) == 0) return(out)

}


#' Compress a pixel pal
#'
#' Compresses a pixel pal using Unicode Blocks
#'
#' @param pix a character indicating a path to a .PNG file.
#'
#' @return returns a compressed Pixel Pal character matrix that occupies half the
#' space in the terminal
#'
#' @details Internal use only. Note: the Apple Terminal does not deal well with
#' Unicode blocks. When the package is loaded, if Apple Terminal is detected,
#' compression is disabled.
#'
#' @references kindly suggested in https://github.com/gdagstn/alamak/issues/2
#' by Trevor L. Davis
#'
#' @importFrom crayon make_style combine_styles

compressPixPal <- function(pix){

pix = t(pix)

complist = list()

for(i in seq(1,nrow(pix), by = 2)) {

two_rows = t(pix[i:(i + 1),])

compressed = apply(two_rows, 1, function(x) {

if(x[1] == "#00000000" & x[2] != "#00000000") {
background1 = FALSE
x[1] = x[2]
pixel_char = "\u2584"
} else if(x[1] != "#00000000" & x[2] == "#00000000") {
x[2] = x[1]
background1 = FALSE
pixel_char = "\u2580"
} else if(x[1] == "#00000000" & x[2] == "#00000000"){
background1 = FALSE
pixel_char = " "
} else if(x[1] != "#00000000" & x[2] != "#00000000"){
background1 = TRUE
pixel_char = "\u2584"
}

combine_styles(make_style(x[1], bg = background1),
make_style(x[2], bg = FALSE))(pixel_char)
})

complist[[i]] = c(compressed, "\n")

}
return(do.call(rbind, complist[!is.null(complist)]))
}


16 changes: 11 additions & 5 deletions R/pixpals.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#' Code to create initial Pixel Pals
#' Sourced on loading

pixpals = list()

pixpals$Buster = list("crayon" = makePixPal("inst/extdata/buster.png"),
term = Sys.getenv("TERM_PROGRAM")
if(term == "Apple_Terminal") compression = FALSE else compression = TRUE

pixpals$Buster = list("crayon" = makePixPal("inst/extdata/buster.png", compress = compression),

"messages" = list(

Expand All @@ -23,7 +29,7 @@ pixpals$Buster = list("crayon" = makePixPal("inst/extdata/buster.png"),
)
)

pixpals$Jerry = list("crayon" = makePixPal("inst/extdata/jerry.png"),
pixpals$Jerry = list("crayon" = makePixPal("inst/extdata/jerry.png", compress = compression),

"messages" = list(

Expand Down Expand Up @@ -52,7 +58,7 @@ pixpals$Jerry = list("crayon" = makePixPal("inst/extdata/jerry.png"),
)
)

pixpals$Oniji = list("crayon" = makePixPal("inst/extdata/oniji.png"),
pixpals$Oniji = list("crayon" = makePixPal("inst/extdata/oniji.png", compress = compression),

"messages" = list(
"Error" = c("A single function / Mysterious operations / It has failed again.",
Expand All @@ -73,7 +79,7 @@ pixpals$Oniji = list("crayon" = makePixPal("inst/extdata/oniji.png"),
)


pixpals$Lenny = list("crayon" = makePixPal("inst/extdata/lenny.png"),
pixpals$Lenny = list("crayon" = makePixPal("inst/extdata/lenny.png", compress = compression),

"messages" = list(
"Error" = c("You got this!",
Expand All @@ -100,7 +106,7 @@ pixpals$Lenny = list("crayon" = makePixPal("inst/extdata/lenny.png"),
)
)

pixpals$E10N = list("crayon" = makePixPal("inst/extdata/e10n.png"),
pixpals$E10N = list("crayon" = makePixPal("inst/extdata/e10n.png", compress = compression),

"messages" = list(

Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
14 changes: 11 additions & 3 deletions man/alamak.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions man/compressPixPal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/makePixPal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions man/pixpals.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.