Skip to content

Commit

Permalink
use readChar() to read clipboard on macOS because the clipboard may n…
Browse files Browse the repository at this point in the history
…ot contain a final EOL (#54)
  • Loading branch information
yihui committed Mar 15, 2021
1 parent e4a7579 commit ff21861
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions R/tidy.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ tidy_source = function(
) {
if (is.null(text)) {
if (source == 'clipboard' && Sys.info()['sysname'] == 'Darwin') {
source = pipe('pbpaste')
source = pipe('pbpaste'); on.exit(close(source), add = TRUE)
# use readChar() instead of readLines() in case users didn't copy the last
# \n into clipboard, e.g., https://github.com/yihui/formatR/issues/54
text = readChar(source, getOption('formatR.clipboard.size', 1e5))
text = unlist(strsplit(text, '\n'))
} else {
text = readLines(source, warn = FALSE)
}
text = readLines(source, warn = FALSE)
}
enc = special_encoding(text)
if (length(text) == 0L || all(grepl('^\\s*$', text))) {
Expand Down

0 comments on commit ff21861

Please sign in to comment.