Skip to content

Commit

Permalink
Add emoji hash
Browse files Browse the repository at this point in the history
Closes #26.
  • Loading branch information
gaborcsardi committed Oct 4, 2021
1 parent 08e5c7c commit 892c748
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 12 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Description: Query and print information about the current R
License: GPL-2
URL: https://github.com/r-lib/sessioninfo#readme
BugReports: https://github.com/r-lib/sessioninfo/issues
Depends:
R (>= 2.10)
Imports:
cli,
tools,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ S3method(as.character,packages_info)
S3method(as.character,platform_info)
S3method(as.character,python_info)
S3method(as.character,session_info)
S3method(c,platform_info)
S3method(format,external_info)
S3method(format,packages_info)
S3method(format,platform_info)
S3method(format,python_info)
S3method(format,session_info)
S3method(print,external_info)
S3method(print,packages_info)
S3method(print,platform_info)
Expand Down
43 changes: 43 additions & 0 deletions R/hash.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

# TODO: this can use cli, once a new version with cli::hash_emoji() is
# release to CRAN

add_hash <- function(si, size = 3) {
stopifnot(size >= 1, size <= 4)

orig <- si
# drop the date for hashing
if ("platform" %in% names(si) && "date" %in% names(si$platform)) {
si$platform$date <- NULL
}

old <- options(
cli.unicode = FALSE,
cli.num_colors = 1,
sessioninfo.emoji = FALSE
)
on.exit(options(old), add = TRUE)
tmp <- tempfile()
writeLines(format(si), tmp)
on.exit(unlink(tmp), add = TRUE)

md5 <- tools::md5sum(tmp)[[1]]
md513 <- substr(md5, 1, 13)
mdint <- as.integer(as.hexmode(strsplit(md513, "")[[1]]))
hash <- sum(mdint * 16^(0:12))

base <- nrow(emojis)
ehash <- hash %% (base ** size)
digits <- integer()
while (ehash > 0) {
digits <- c(digits, ehash %% base)
ehash <- ehash %/% base
}
digits <- c(digits, rep(0, 10))[1:size]

nms <- emojis$name[digits + 1]
emo <- emojis$emoji[digits + 1]

orig$hash <- list(emoji = emo, emo_text = nms)
orig
}
16 changes: 14 additions & 2 deletions R/platform-info.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#' platform_info()

platform_info <- function() {
structure(drop_null(list(
as_platform_info(drop_null(list(
version = R.version.string,
os = os_name(),
system = version$system,
Expand All @@ -36,7 +36,7 @@ platform_info <- function() {
date = format(Sys.Date()),
rstudio_version = get_rstudio_version(),
pandoc_version = get_pandoc_version()
)), class = c("platform_info", "list"))
)))
}

get_rstudio_version <- function() {
Expand Down Expand Up @@ -98,3 +98,15 @@ print.platform_info <- function(x, ...) {
as.character.platform_info <- function(x, ...) {
format(x, ...)
}

#' @export

c.platform_info <- function(...) {
as_platform_info(NextMethod())
}

as_platform_info <- function(x) {
stopifnot(is.list(x))
class(x) <- c("platform_info", "list")
x
}
6 changes: 3 additions & 3 deletions R/printing.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

#' @importFrom cli symbol

rule <- function (..., pad = NULL) {
if (is.null(pad)) pad <- symbol$line
rule <- function (..., pad = NULL, double = FALSE) {
if (is.null(pad)) pad <- if (double) symbol$double_line else symbol$line
title <- if (length(list(...))) paste0(" ", ..., " ") else ""

width <- max(cli::console_width() - nchar(title) - 3, 0)
width <- max(cli::console_width() - cli::ansi_nchar(title, "width") - 3, 0)
paste(pad, title, paste(rep(pad, width), collapse = ""), sep = "")
}

Expand Down
59 changes: 52 additions & 7 deletions R/session-info.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ session_info <- function(
class = c("session_info", "list")
)

si <- add_hash(si)

if (is_string(to_file)) {
writeLines(as.character(si), to_file)
invisible(si)
Expand All @@ -100,24 +102,67 @@ session_info <- function(

#' @export

as.character.session_info <- function(x, ...) {
c(if ("platform" %in% names(x)) {
c(rule("Session info"), as.character(x$platform), "")
format.session_info <- function(x, ...) {

has_platform <- !is.null(x$platform)

c(if (!"platform" %in% names(x)) {
c(rule(paste("Session info", emo_hash(x)), double = TRUE), text_hash(x))
},
if ("platform" %in% names(x)) {
c(rule(paste("Session info", emo_hash(x))),
text_hash(x),
format(x$platform),
""
)
},
if ("packages" %in% names(x)) {
c(rule("Packages"), as.character(x$packages), "")
c(rule("Packages"), format(x$packages), "")
},
if ("external" %in% names(x)) {
c(rule("External software"), as.character(x$external), "")
c(rule("External software"), format(x$external), "")
},
if ("python" %in% names(x)) {
c(rule("Python configuration"), as.character(x$python), "")
c(rule("Python configuration"), format(x$python), "")
}
)
}

#' @export

as.character.session_info <- function(x, ...) {
format(x, ...)
}

has_emoji <- function () {
if (isTRUE(opt <- getOption("sessioninfo.emoji"))) {
TRUE
} else if (identical(opt, FALSE)) {
FALSE
} else if (!cli::is_utf8_output()) {
FALSE
} else {
Sys.info()[["sysname"]] == "Darwin"
}
}

emo_hash <- function(x) {
if (is.null(x$hash)) return("")
if (has_emoji()) {
paste0(" ", paste(x$hash$emoji, " ", collapse = ""))
} else {
""
}
}

text_hash <- function(x) {
if (!is.null(x$hash) && !has_emoji()) {
c(paste0(" hash: ", paste(x$hash$emo_text, collapse = ", ")), "")
}
}

#' @export

print.session_info <- function(x, ...) {
cat(as.character(x), sep = "\n")
cat(format(x), sep = "\n")
}
Binary file added R/sysdata.rda
Binary file not shown.
Loading

0 comments on commit 892c748

Please sign in to comment.