Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate long messages #216

Merged
merged 1 commit into from
May 17, 2024
Merged

Truncate long messages #216

merged 1 commit into from
May 17, 2024

Conversation

foteinigk
Copy link
Contributor

@foteinigk foteinigk commented Apr 22, 2024

We utilize uilive library to update the terminal output in real-time during task execution. Due to the nature of uilive, multi-line errors retuned by service calls aren't always displayed accurately within the tasks section, leading to confusion, particularly during multitasking scenarios.
We can truncate long messages in these cases and print the entire messages in --silent flag cases where the terminal output is not updated in real time.

kbairak
kbairak previously approved these changes Apr 25, 2024
Copy link
Member

@kbairak kbairak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comments are minor, we could move ahead as is. It's up to you.

@@ -210,3 +210,12 @@ func isValidResolutionPolicy(policy string) (IsValid bool) {
return false

}

func truncateMessage(message string) string {
const maxLength = 75
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we try to detect the terminal's length here? In Python this can be done with the termios package. There must be a way to do this in Go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is this package https://pkg.go.dev/golang.org/x/crypto/ssh/terminal and it worked as expected locally, but I was getting some build errors with some other packages, so I'll check that later to be sure🙏

const maxLength = 75

if len(message) > maxLength {
return message[:maxLength] + ".."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: You could do

Suggested change
return message[:maxLength] + ".."
return message[:maxLength - 2] + ".."

here. The reasoning is this: Assume max length is 5. Then:

abc    -> abc
abcd   -> abcd
abcde  -> abcde
abcdef -> abc..

Copy link
Member

@kbairak kbairak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There could be extra complications:

Colors in terminal work with ANSI escape sequences. For example, to display something in cyan you need this: \x31[96m cyan text here \x31[0m.

Or as an array with one-byte elements (because \xXX is an escape sequence):

['\x31', '[', '9', '6', 'm', ' ', 'c', 'y', 'a', 'n', ' ', 't', 'e', 'x', 't', ' ', 'h', 'e', 'r', 'e', ' ', '\x31', '[', '0', 'm']

The last code, \x31[0m, is a "reset" code. If we remove this reset code during truncation, all following text will be colored until the color is changed or another reset happens.

Generally in our messages, the coloring happens early so this is unlikely to happen. Based on this and based on the fact that it would be very hard to cover every ANSI code I suggest we ignore this.

https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

@foteinigk foteinigk merged commit fc80657 into devel May 17, 2024
6 checks passed
@foteinigk foteinigk deleted the TX-15426_cli_errors branch May 17, 2024 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants