arduino / arduino-ide

Arduino IDE 2.x
https://www.arduino.cc/en/software
GNU Affero General Public License v3.0
2.33k stars 395 forks source link

Output view does not correctly handle carriage return #1473

Open per1234 opened 2 years ago

per1234 commented 2 years ago

Describe the problem

Some command line tools generate an animated progress bar by using a carriage return without newline to repeatedly overwrite the same line.

🐛 This does not work when their output is printed in the "Output" view. The carriage return causes a line break to be added.

To reproduce

  1. Upload a sketch via the Arduino CLI command line interface to a board that uses an upload tool that produces an animated progress bar (e.g., Nano 33 BLE). 🙂 There is a nice looking animated progress bar: cli3
  2. Do the same using Arduino IDE 1.8.19. 🙂 There is a nice looking animated progress bar: ide-1 x
  3. Do the same using Arduino IDE 2.x 🐛 The progress bar is not animated: ide-2 x

Expected behavior

The current line is overwritten from the start of the line in the "Output" view with subsequent text.

For example, this text:

foobar\rbaz

Should result in this content in the output view:

bazbar

Arduino IDE version

2.0.0-snapshot-4e590ab

Operating system

Windows, Ubuntu

Operating system version

Windows 10, Ubuntu 20.04

Additional context

Support was added in Arduino IDE 1.x by https://github.com/arduino/Arduino/pull/9954


Originally reported at https://forum.arduino.cc/t/arduino-2-0-0-mac-ugh-why-so-backwards/1033549

Issue checklist

cmaglie commented 2 years ago

This is a problem in the console text area of the IDE becuase running arduino-cli from the command line shows the expected progress bar: this means that the standard-out stream from avrdude is correctly sent through gRPC.

We had this problem before in the Arduino IDE 1.8.x, it basically boils down to the text area not correctly handling the CR (Carriage Return) character:

CR and LF are control characters or bytecode that can be used to mark a line break in a text file.

CR = Carriage Return (\r, 0x0D in hexadecimal, 13 in decimal) — moves the cursor to the beginning of the line without advancing to the next line. LF = Line Feed (\n, 0x0A in hexadecimal, 10 in decimal) — moves the cursor down to the next line without returning to the beginning of the line.

To render the progress bar, avrdude repeatedly overwrites the same line using CR as a control character to move the cursor back at the beginning of the line, without advancing to the next one. The issue here is that the IDE text area interprets CR as CR+LF moving the cursor to the next line.

IMHO this issue should be moved to the Arduino IDE repo.

cmaglie commented 2 years ago

Another question is: isn't the "console" text area already supporting some terminal control standard?

If we must implement all control characters from an ANSI terminal it will become a project on its own... maybe there is already an implementation of a "terminal" or a "console" text area that support ANSI (or vt100 or any other terminal standard) that can be reused.

per1234 commented 2 years ago

Thank @cmaglie. I didn't interpret the gRPC response content correctly. You are correct that Arduino CLI is handling it correctly and this is a bug in Arduino IDE. I have updated the issue and transferred it to the correct repo.

kittaakos commented 2 years ago

The Output view is an editor and not a terminal. If we want terminal behavior, we must use a Pseudoterminal from VS Code. This change requires a bigger effort on the IDE2 side.