Simplify convoluted nested condition logic in text handler#59
Simplify convoluted nested condition logic in text handler#59
Conversation
|
👋 Thanks for opening this PR, @Copilot! Here's what will happen next:
Please make sure:
|
Co-authored-by: adcondev <38170282+adcondev@users.noreply.github.com>
Co-authored-by: adcondev <38170282+adcondev@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
This PR simplifies the text printing logic in the handleText function by removing confusing nested conditional statements and replacing them with a clearer, more maintainable structure.
- Refactored the text printing conditional logic from nested if-statements to a straightforward structure
- The new implementation checks if content exists first, then decides whether to print with or without a newline based on the
NewLineflag
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if err := printer.Print(cmd.Content); err != nil { | ||
| return err | ||
| } | ||
| } |
There was a problem hiding this comment.
This refactoring changes the behavior when cmd.Content is empty but cmd.NewLine is true. The previous code would print a blank line in this case, but the new code skips printing entirely. While the new logic is cleaner and more intuitive (using FeedCommand for blank lines), this behavioral change could affect existing code or JSON documents that rely on printing blank lines via text commands with empty content. Consider whether this breaking change is intentional and if it requires documentation or a migration guide.
| } | |
| } | |
| } else if cmd.NewLine { | |
| // Mantener compatibilidad: imprimir línea en blanco si Content está vacío y NewLine es true | |
| if err := printer.PrintLine(""); err != nil { | |
| return err | |
| } |
* fix(encoding): improve logging for unsupported code tables Enhance the logging messages for unsupported encoding and code tables to provide clearer context. This change helps in debugging and understanding fallback behavior to Windows-1252. - Updated log messages to specify unsupported code tables - Ensured proper handling of character code table selection Signed-off-by: Adrián Constante <ad_con.reload@proton.me> * feat(document): add document building and execution features - Introduce a new Builder for creating print documents - Implement an Executor to handle document execution - Support commands for text, images, separators, feeds, and cuts - Add JSON parsing and serialization for documents This enhances the library's capability to build and print documents programmatically or from JSON files. Signed-off-by: Adrián Constante <ad_con.reload@proton.me> * refactor(graphics): improve variable naming for clarity Updated variable names in base64_example.go and escpos_profile.go to enhance code readability. Removed unused font mappings in escpos_profile.go to streamline the profile structure. Signed-off-by: Adrián Constante <ad_con.reload@proton.me> * docs(pkg): add motivation documents for project overview - Introduce MOTIVATION1.md and MOTIVATION2.md files - Provide strategic analysis and architectural proposals - Enhance understanding of the ESC/POS library development Signed-off-by: Adrián Constante <ad_con.reload@proton.me> * fix(escpos): simplify convoluted nested condition logic in text handler (#59) * Initial plan * refactor(document): simplify nested condition logic in text handler Co-authored-by: adcondev <38170282+adcondev@users.noreply.github.com> * fix(document): ensure styles are always reset after text command Co-authored-by: adcondev <38170282+adcondev@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: adcondev <38170282+adcondev@users.noreply.github.com> * fix(converter): add error logging for JSON marshaling This enhancement will aid in debugging and ensure that issues are captured and reported effectively. Signed-off-by: Adrián Constante <ad_con.reload@proton.me> --------- Signed-off-by: Adrián Constante <ad_con.reload@proton.me> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Descripción
Refactors nested conditional logic in
handleTextthat was flagged as convoluted and difficult to understand. The original implementation had a confusing double-nested condition where the outer checkedcmd.NewLine || cmd.Content == ""and the inner checkedcmd.NewLine || (cmd.Content != "" && !cmd.NewLine).Before:
After:
The refactored version makes the logic explicit: skip empty content, use
PrintLinefor newline-terminated content, otherwise usePrint. Style reset logic remains unchanged and executes in all cases.Tipo de cambio
¿Cómo se ha probado?
Checklist
Notas adicionales
Addresses feedback from PR #58: #58 (comment)
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.