jalvesaq/colorout

colorout package interferes with pkg output, causes missing line breaks

slowkow opened this issue · 5 comments

colorout is interfering with the output from pkg, so line breaks are not appearing as they should.

I think I should stop using colorout now that crayon is gaining more usage.

With library(colorout) in my ~/.Rprofile:

Loading required package: colorout
> pkg::pkg_install("AnnotationDbi")
ℹ Checking for package metadata updates
✔ All 10 metadata files are current.

ℹ Loading session disk cached package metadata✔ Loading session disk cached package metadata✔ Using cached package metadata
Found  20  deps for  1/1  pkgs [⠙] Checking installed packagesFound  20  deps for  1/1  pkgs [⠹] Checking installed packagesFound  43  deps for  1/1  pkgs [⠸] Resolving standard (CRAN/BioC) packagesFound  43  deps for  1/1  pkgs [⠼] Resolving standard (CRAN/BioC) packages✔ 1 + 16 pkgs | kept 17, updated 0, new 0 | downloaded 0 (0 B) [4.4s]

Without colorout:

> pkg::pkg_install("AnnotationDbi")
ℹ Checking for package metadata updates
✔ All 10 metadata files are current.
✔ Loading session disk cached package metadata
✔ Using cached package metadata
✔ 1 + 16 pkgs | kept 17, updated 0, new 0 | downloaded 0 (0 B) [4.6s]

Details

iTerm2 Build 3.2.7

> devtools::session_info()
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 3.5.1 (2018-07-02)
 os       macOS High Sierra 10.13.6
 system   x86_64, darwin15.6.0
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/New_York
 date     2019-01-23

─ Packages ───────────────────────────────────────────────────────────────────
 package     * version    date       lib source
 assertthat    0.2.0      2017-04-11 [1] CRAN (R 3.5.0)
 backports     1.1.3      2018-12-14 [1] CRAN (R 3.5.0)
 callr         3.1.1      2018-12-21 [1] CRAN (R 3.5.0)
 cli           1.0.1      2018-09-25 [1] CRAN (R 3.5.0)
 colorout    * 1.2-0      2019-01-01 [1] local
 crayon        1.3.4      2017-09-16 [1] CRAN (R 3.5.0)
 curl          3.3        2019-01-10 [1] CRAN (R 3.5.2)
 desc          1.2.0      2018-05-01 [1] CRAN (R 3.5.0)
 devtools    * 2.0.1      2018-10-26 [1] CRAN (R 3.5.1)
 digest        0.6.18     2018-10-10 [1] CRAN (R 3.5.0)
 filelock      1.0.2      2018-10-05 [1] CRAN (R 3.5.0)
 fs            1.2.6      2018-08-23 [1] CRAN (R 3.5.0)
 glue          1.3.0      2018-07-17 [1] CRAN (R 3.5.0)
 magrittr      1.5        2014-11-22 [1] CRAN (R 3.5.0)
 memoise       1.1.0.9000 2019-01-18 [1] Github (hadley/memoise@1650ad7)
 pkg         * 0.1.1.9000 2019-01-23 [1] Github (r-lib/pkg@582d0d5)
 pkgbuild      1.0.2      2018-10-16 [1] CRAN (R 3.5.0)
 pkgload       1.0.2      2018-10-29 [1] CRAN (R 3.5.0)
 prettyunits   1.0.2      2015-07-13 [1] CRAN (R 3.5.0)
 processx      3.2.1      2018-12-05 [1] CRAN (R 3.5.0)
 ps            1.3.0      2018-12-21 [1] CRAN (R 3.5.0)
 R6            2.3.0      2018-10-04 [1] CRAN (R 3.5.0)
 Rcpp          1.0.0      2018-11-07 [1] CRAN (R 3.5.0)
 remotes       2.0.2      2018-10-30 [1] CRAN (R 3.5.0)
 rlang         0.3.1.9000 2019-01-23 [1] Github (r-lib/rlang@02bff45)
 rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.5.0)
 sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.5.0)
 testthat      2.0.1      2018-10-13 [1] CRAN (R 3.5.0)
 usethis     * 1.4.0      2018-08-14 [1] CRAN (R 3.5.0)
 withr         2.1.2      2018-03-15 [1] CRAN (R 3.5.0)

[1] /Library/Frameworks/R.framework/Versions/3.5/Resources/library

Colorout already checks whether the string is colorized by another package:
https://github.com/jalvesaq/colorout/blob/master/src/colorout.c#L254

But it seems that R's output does not come in a single string. R breaks it in many pieces, and an ANSI color code might have been output in a previous string and still be active. So, I will not be surprised if colorout interferes in the colorizing made by other packages, but it should not mess with line breaks.

Perhaps the problem is here:
https://github.com/jalvesaq/colorout/blob/master/src/colorout.c#L261

Maybe a workaround: define a hook for every call of the problematic function (in your case the pkg_install).
I tried with the devtools::install_github function. From my .Rprofile:

# modify a function from a package as soon as the package is loaded
setHook(hookName=packageEvent(pkgname="devtools", event="attach"),
        value=function(...) {
                  # save the function to be overwritten
                  original_install_github <- devtools::install_github
                  # define new function
                  my_install_github <- function(...) {
                      # first detach colorout if loaded
                      if (any(search() == "package:colorout")) {
                          message("setHook: detach colorout package ... ", appendLF=F)
                          detach(package:colorout, unload=T)
                          if (!any(search() == "package:colorout")) {
                              message("SUCCESS")
                          } else {
                              message("FAILED")
                          }
                      }
                      # run the original function
                      message("setHook: now run devtools::install_github ...")
                      original_install_github(...)
                      # reload colorout package
                      message("setHook: reload colorout package ... ", appendLF=F)
                      suppressMessages(suppressWarnings(require("colorout", character.only=T)))
                      if (any(search() == "package:colorout")) {
                          message("SUCCESS")
                      } else {
                          message("FAILED")
                      }
                  }
                  # overwrite the original function of the package
                  unlockBinding("install_github", as.environment("package:devtools"))
                  assign("install_github", my_install_github, "package:devtools")
              } # hook function
        ) # setHook

Now, within R, there are 2 install_github functions:

> getAnywhere(install_github)
2 differing objects matching ‘install_github’ were found
in the following places
  package:devtools
  namespace:remotes
Use [] to view one of them

The original function is unchanged and interferes with the colorout package in an undesired way:

> any(search() == "package:colorout")
[1] TRUE
> devtools::install_github('rstudio/bookdown', force=T)
Downloading GitHub repo rstudio/bookdown@master
32m✔39m  38;5;247mchecking for file ‘/tmp/RtmpbU5ARO/remotes3da26e4a1da2/rstudio-bookdown-44af2c0/DESCRIPTION’39m36m39m
38;5;247m─39m38;5;247m  39m38;5;247mpreparing ‘bookdown’:39m36m39m
32m✔39m  38;5;247mchecking DESCRIPTION meta-information39m36m39m
38;5;247m─39m38;5;247m  39m38;5;247mchecking for LF line-endings in source and make files and shell scripts39m36m39m
38;5;247m─39m38;5;247m  39m38;5;247mchecking for empty or unneeded directories39m36m39m
38;5;247m─39m38;5;247m  39m38;5;247mbuilding ‘bookdown_0.9.1.tar.gz’39m36m39m
...

Through the hook, however, we can use our own function instead:

> any(search() == "package:colorout")
[1] TRUE
> install_github('rstudio/bookdown', force=T)
setHook: detach colorout package ... SUCCESS
setHook: now run devtools:install_github ...
Downloading GitHub repo rstudio/bookdown@master
✔  checking for file ‘/tmp/RtmpbU5ARO/remotes3da239ca9904/rstudio-bookdown-44af2c0/DESCRIPTION’ ...
─  preparing ‘bookdown’:
✔  checking DESCRIPTION meta-information ...
─  checking for LF line-endings in source and make files and shell scripts
─  checking for empty or unneeded directories
─  building ‘bookdown_0.9.1.tar.gz’
...
setHook: reload colorout package ... SUCCESS

Maybe this helps as a workaround? Good luck with your thesis ...

You may call the function noColorOut() to disable the colorizing of R output, and ColorOut() to enable it again. This will be faster than detaching colorout and attaching it again.

Good point ty!

I guess this bug is finally fixed on the branch fix_nl.

When the output already had colors, it was always printed on stdout.