Skip to content

term: add writeln_color() #24463

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

Merged
merged 2 commits into from
May 15, 2025

Conversation

kbkpbot
Copy link
Contributor

@kbkpbot kbkpbot commented May 12, 2025

The PR add write_color() and writeln_color() to term module, enable output ANSI colorful string.

example:

module main
import term
import strings

fn main() {
        mut sb := strings.new_builder(100)
        term.write_color(mut sb, '[Warning]', fg : .red)
        sb.write_string('Please make sure ')
        term.write_color(mut sb, 'value > 0', fg: .cyan)
        sb.write_string(' and ')
        term.write_color(mut sb, 'format is string', fg : .cyan)
        sb.writeln('')

        output := sb.str()
        println(output)
}

will get output:
image

Copy link

Connected to Huly®: V_0.6-22833

@Delta456
Copy link
Member

But why? We already have term module for that

@spytheman
Copy link
Member

spytheman commented May 12, 2025

I agree with @Delta456 - this should be in term if it is needed, i.e. something like this:

import term
...
term.writeln(mut sb, 'hello', styles :[.bold, .italic, .underline], fg: .red, bg : .cyan)

@kbkpbot
Copy link
Contributor Author

kbkpbot commented May 12, 2025

I agree with @Delta456 - this should be in term if it is needed, i.e. something like this:

import term
...
term.writeln(mut sb, 'hello', styles :[.bold, .italic, .underline], fg: .red, bg : .cyan)

If so, there is a little confuse jump from strings to term and jump back.

a.v

module main
import term
import strings

fn main() {
        mut sb := strings.new_builder(100)
        term.write_color(mut sb, '[Warning]', fg : .red)
        sb.write_string('Please make sure ')
        term.write_color(mut sb, 'value > 0', fg: .cyan)
        sb.write_string(' and ')
        term.write_color(mut sb, 'format is string', fg : .cyan)
        sb.writeln('')

        output := sb.str()
        println(output)
}

And I prefer build the string in string builder, not just output it directly.

image

@spytheman
Copy link
Member

spytheman commented May 12, 2025

It makes no logical sense for a specific formatting function (using ANSI escape codes) to be part of the string builder.

@spytheman
Copy link
Member

And I prefer build the string in string builder, not just output it directly.

I agree - that is why the first argument is a mutable string builder.

That:

term.writeln(mut sb, 'hello', styles :[.bold, .italic, .underline], fg: .red, bg : .cyan)

is a direct equivalent to your:

sb.writeln_color('hello', styles :[.bold, .italic, .underline], fg: .red, bg : .cyan)

It will not print anything on its own to stdout, it will just append to the string builder like yours does.

@spytheman
Copy link
Member

@medvednikov what do you think?

@medvednikov
Copy link
Member

I agree with spy here

@kbkpbot kbkpbot changed the title strings: builder add writeln_color() term: add writeln_color() May 14, 2025
@spytheman spytheman merged commit 4c53021 into vlang:master May 15, 2025
64 checks passed
@kbkpbot kbkpbot deleted the strings-builder-add-write-color branch May 31, 2025 09:09
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.

4 participants