arduino / arduino/ArduinoCore-API
Improved print formatting
- Ngôn ngữ chính
- C++
- Star
- 306
- Fork
- 150
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
In the past, there has been talk of improving print formatting. e.g. on the mailinglist [here](https://groups.google.com/a/arduino.cc/d/topic/developers/y7p5wS4ICIY/discussion), [here](https://groups.google.com/a/arduino.cc/d/topic/developers/NWnjeSyuNek/discussion), [here](https://groups.google.com/a/arduino.cc/d/topic/developers/7KpdLDgFsO0/discussion). The bottom line is that there is a need for a way to have more control over print formatting (e.g. leading zeroes) and, IMHO, also ways to apply custom formatting and formatting options.
Note that printf is often suggested as the solution to this need. In the above threads, a lot of the discussion is about his, but there are IMHO sufficient reasons to *not* want to use printf (which I'll not repeat here). Please do *not* add comments here arguing for printf support. Instead, head over to #28 and #32 if you want to make a case for printf.
A related request is to allow printing multiple values in a single print call (e.g. to do things like "Serial.println("Received ", num, " bytes");`).
PR #30 has an implementation for this.
IMHO it would be good to tackle formatting options at the same time as variadic printing, since the implementations are likely related (and doing one without the other might cause compatibility problems in the future).
In the above discussions, I've suggested an approach where printable arguments can be intermixed with formatting directives, putting the formatting after the value (just like the current base and precision arguments). These formatting directives could then be either builtin-directives, or custom directives. For my previous thoughts, see [here](https://groups.google.com/a/arduino.cc/d/msg/developers/7KpdLDgFsO0/W4XGNr_t_kAJ) and [here](https://groups.google.com/a/arduino.cc/d/msg/developers/7KpdLDgFsO0/4FUnoRsppA8J). Since people seemed to like this approach, I did an initial implementation.
I still have a lot of documentation to do, and I'm not quite happy with the way that formatter customization works now, but in the interest of discussion, I've pushed my changes here: https://github.com/arduino/ArduinoCore-avr/blob/formatting/cores/arduino/Print.h For an example sketch that uses this, see https://gist.github.com/matthijskooijman/cbac69e8db907ab0f849775b5a1cac44. Note that this is just the sketch I was using during development, so it's not so much a realistic testcase as well as a way to make sure various things would (not) work. I'm not actually sure if any of this even compiles currently, it's been a while since I worked on this.
In #30, @cousteaulecommandant said:
> Maybe it'd be better to move formatting entirely out of Print.h (except for very simple things, for the sake of backwards compatibility) to a separate "value formatter" class that does all the formatting work, and leave Print::print for simple multi-argument printing.
This might be an interesting avenue to explore. It would mean that (custom/extended) formatting would work differently than the current base/precision formatting options, but if we come up with something significantly better, I guess deprecating the existing system and switching to something new would be ok. Implementing this might very well be simpler than what I did so far (it might even be a matter of creating custom `Printable` instances, though I suspect there might be some performance issues with that approach).
I have no time *right now* to go into this further, but wanted to at least create this issue as a place to collect further discussion.
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Hướng nghiên cứu
Bắt đầu bằng cách đọc phần triển khai được đề xuất trong cores/arduino/Print.h trên branch formatting, sau đó kiểm tra sketch ví dụ được liên kết và phần thảo luận liên quan trong các issue #28, #30 và #32. Công việc hoàn tất khi một phương án định dạng và in variadic đã được quyết định, triển khai, ghi lại tài liệu và xác minh là biên dịch được với các trường hợp sử dụng ví dụ.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- cpp
- Lĩnh vực
- api, embedded-iot
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100