0

I am trying to print text repeatedly on the same line in R (windows). This works if I use the carriage return in the following way:

text = c("word 1", "word 2", "word 3")
for (word in text) {
  cat("\rText =",word)
  Sys.sleep(0.4)
}

This overrides each line like it should. However, I would like to print the words in bold and use the ANSI codes \u001b[1m and the reset \u001b[0m which results in:

text = c("word 1", "word 2", "word 3")
for (word in text) {
  cat("\rText =\u001b[1m",word,"\u001b[0m")
  Sys.sleep(0.4)
}

This works for the first two prints, but eventually results in the output:

Text =Text = word 3 Interestingly enough this does work if ALL output text is in bold font and not only part of it.

How can I get this to work?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Héctor van den Boorn
  • 1,218
  • 13
  • 32

1 Answers1

0

I know this is probably not what you're looking for, but maybe it will give you a clue... I've just spent an hour with this puzzle and \r and loops of \b to backtrack through printed text are all showing bizarre behavior. This worked, but refreshes at the top of the console:

text = c("word 1", "word 2", "word 3", "word 4")
for (word in text) {
  cat("\014")
  cat("Text =","\u001b[1m",word)
  Sys.sleep(0.4);
  cat("\u001b[0m");
  flush.console(); 
}
mysteRious
  • 4,102
  • 2
  • 16
  • 36