Skip to content

Commit

Permalink
feat: Use tibbles for lsos
Browse files Browse the repository at this point in the history
  • Loading branch information
csgillespie committed Jul 19, 2023
1 parent 16633e2 commit 1c5e3df
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 56 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: rprofile
Title: My Rprofile in a Handy R Package
Version: 0.1.13
Version: 0.1.14
Authors@R:
person(given = "Colin",
family = "Gillespie",
Expand Down Expand Up @@ -36,4 +36,4 @@ Remotes:
jalvesaq/colorout
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
13 changes: 0 additions & 13 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,10 @@ export(set_rstudio)
export(set_startup_info)
export(set_startup_options)
export(set_terminal)
importFrom(cli,symbol)
importFrom(crayon,blue)
importFrom(crayon,bold)
importFrom(crayon,green)
importFrom(crayon,italic)
importFrom(crayon,make_style)
importFrom(crayon,red)
importFrom(crayon,yellow)
importFrom(gert,git_status)
importFrom(memoise,memoise)
importFrom(memuse,Sys.procmem)
importFrom(memuse,mu.size)
importFrom(memuse,mu.unit)
importFrom(prompt,git_arrows)
importFrom(prompt,git_branch)
importFrom(prompt,git_dirty)
importFrom(remotes,install_github)
importFrom(tibble,tibble)
importFrom(utils,capture.output)
importFrom(utils,object.size)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# rprofile 0.1.14 _2023-07-19_
* refactor: Remove importFrom where possible
* feat: Use tibbles for lsos

# rprofile 0.1.13 _2023-06-05_
* fix: Use prompts from {prompt} to avoid odd terminal resizing.

Expand Down
3 changes: 0 additions & 3 deletions R/library.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
## tweak autoinst
## It has now been adapted

#' @importFrom memoise memoise
available_packages = memoise::memoise(utils::available.packages)

gh_pkg = memoise::memoise(function(pkg) {
Expand Down Expand Up @@ -38,7 +37,6 @@ display_matches = function(matches) {
cat(multicol(paste0(sprintf(paste0("%", width_nums, "s"), nums), "| ", matches)), sep = "")
}

#' @importFrom remotes install_github
# From gaborcsardi/crayon/R/utils.r
multicol = function(x) {
xs = x
Expand All @@ -54,7 +52,6 @@ multicol = function(x) {
}

# TODO: use lighter version of install_github
#' @importFrom remotes install_github
autoinst = function(package, ...) {
pkg = try(as.character(package), silent = TRUE)
if (inherits(pkg, "try-error")) {
Expand Down
27 changes: 8 additions & 19 deletions R/list-object-sizes.R
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
#' @importFrom utils capture.output object.size
# SO: http://stackoverflow.com/q/1358003/203420
# improved list of objects
ls.objects = function(pos = 1, order.by,
decreasing = FALSE, head = FALSE, n = 10) {
lsos = function(order.by = c("PrettySize", "Type", "Size", "Rows", "Columns"),
pos = 1) {
napply = function(names, fn) sapply(names, function(x) fn(get(x, pos = pos)))
names = ls(pos = pos)

obj.class = napply(names, function(x) as.character(class(x))[1])
obj.mode = napply(names, mode)
obj.type = ifelse(is.na(obj.class), obj.mode, obj.class)
obj.prettysize = napply(names, function(x) {
capture.output(print(object.size(x), units = "auto"))
utils::capture.output(print(utils::object.size(x), units = "auto"))
})
obj.size = napply(names, object.size)
obj.size = napply(names, utils::object.size)
obj.dim = t(napply(names, function(x) as.numeric(dim(x))[1:2]))

if (length(names) > 0) {
vec = is.na(obj.dim)[, 1] & (obj.type != "function")
obj.dim[vec, 1] = napply(names, length)[vec]
out = data.frame(obj.type, obj.size, obj.prettysize, obj.dim)
} else {
out = data.frame("a", "b", "c", "d", "e")
out = tibble::tibble("a", "b", "c", "d", "e")
out = out[FALSE, ]
}
names(out) = c("Type", "Size", "PrettySize", "Rows", "Columns")
if (!missing(order.by))
out = out[order(out[[order.by]], decreasing = decreasing), ]
if (head)
out = head(out, n)
out
}

lsos = function(..., n = 10) {
ls.objects(...,
order.by = "Size",
decreasing = TRUE,
head = TRUE,
n = n)
order.by = match.arg(order.by)
out = out[order(out[[order.by]], decreasing = TRUE), ]
tibble::tibble(out)
}
5 changes: 0 additions & 5 deletions R/prompt.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#' @importFrom memuse Sys.procmem mu.size mu.unit
display_memuse = function() {
current = memuse::Sys.procmem()[[1]]
if (memuse::mu.size(current, FALSE) < 1e9) return("")
Expand All @@ -12,10 +11,6 @@ grey = function() {
crayon::make_style("grey70")
}

#' @importFrom crayon green red blue yellow italic bold make_style
#' @importFrom prompt git_branch git_dirty git_arrows
#' @importFrom cli symbol
#' @importFrom gert git_status
rprofile_prompt = function(expr, value, ok, visible) {
status = if (ok) crayon::green(cli::symbol$tick)
else crayon::red(cli::symbol$cross)
Expand Down
26 changes: 13 additions & 13 deletions R/set-startup-info.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#TODO
#ip link
#cat /sys/class/net/<interface>/speed # Ethernet speed

# Used in glue, so namespacing is annoying
#' @importFrom crayon green red blue yellow italic bold make_style
get_darwin_internet = function() {

# Connections
Expand All @@ -28,13 +29,13 @@ get_darwin_internet = function() {
signal = as.numeric(wifi_quality)
wifi_signal = signif((signal + 110) * 10 / 7, 0)
wifi_strength = if (signal > 70) {
green(wifi_signal)
crayon::green(wifi_signal)
} else if (wifi_signal > 60) {
green(wifi_signal)
crayon::green(wifi_signal)
} else if (wifi_signal > 50) {
yellow(wifi_signal)
crayon::yellow(wifi_signal)
} else {
red(wifi_signal)
crayon::red(wifi_signal)
}
wifi_string = glue::glue_col("{green {cli::symbol$tick}} {wifi_name} (Strength: {wifi_strength}, lastTXRate: {lastTXRate}, maxRate: {maxRate})")
# nolint end
Expand All @@ -54,7 +55,7 @@ get_darwin_internet = function() {
get_linux_internet = function() {
cons = system2("nmcli", args = c("connection", "show"), stdout = TRUE)

if (length(cons) <= 1L) return(crayon::red(symbol$cross))
if (length(cons) <= 1L) return(crayon::red(cli::symbol$cross))
start_locs = stringr::str_locate(cons[1], c("NAME", "UUID", "TYPE", "DEVICE"))
start_locs = start_locs[, 1]
end_locs = numeric(4)
Expand All @@ -79,13 +80,13 @@ get_linux_internet = function() {

wifi_signal = signif((signal + 110) * 10 / 7, 0)
wifi_strength = if (signal > 70) {
green(wifi_signal)
crayon::green(wifi_signal)
} else if (wifi_signal > 60) {
green(wifi_signal)
crayon::green(wifi_signal)
} else if (wifi_signal > 50) {
yellow(wifi_signal)
crayon::yellow(wifi_signal)
} else {
red(wifi_signal)
crayon::red(wifi_signal)
}
wifi_string = glue::glue_col("{green {cli::symbol$tick}} {wifi_name} ({wifi_strength})")
} else {
Expand All @@ -109,7 +110,6 @@ get_windows_internet = function() {
glue::glue("{wifi_name} (", trimws("{wifi_signal}"), ")")
}

#' @importFrom tibble tibble
get_internet = function() {
if (Sys.info()[["sysname"]] == "Windows") {
con_str = get_windows_internet()
Expand Down Expand Up @@ -138,13 +138,13 @@ get_r_sessions = function() {
get_active_rproj = function() {
wd = get_shorten_paths(getwd())

if (isFALSE(rstudioapi::isAvailable())) return(glue::glue("{red(symbol$cross)} ({wd})"))
if (isFALSE(rstudioapi::isAvailable())) return(glue::glue("{red(cli::symbol$cross)} ({wd})"))
active_proj = rstudioapi::getActiveProject()
rproj = list.files(pattern = "\\.Rproj$")
if (length(rproj) == 0L && is.null(active_proj)) return(NULL)

if (is.null(active_proj)) {
msg = glue::glue_col("{blue}{symbol$info} {rproj} available ({wd})")
msg = glue::glue_col("{blue}{cli::symbol$info} {rproj} available ({wd})")
} else {
active_proj = basename(active_proj)
msg = glue::glue_col("{green}{active_proj} ({wd})")
Expand Down
2 changes: 1 addition & 1 deletion man/set_startup_options.Rd

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

0 comments on commit 1c5e3df

Please sign in to comment.