Giter Club home page Giter Club logo

Comments (8)

davecheney avatar davecheney commented on May 17, 2024

/cc @ChrisHines you might enjoy this challenge. I think we might end up having to create a separate type for each color (there are a limited number in use, so this isn't so gross) so that we can still have something that is a string, but also has a Format method. Something like

type Green string 

func (g Green) Format(f fmt.State, c rune) { ... }

I'd love to be able to do this with a function, but I don't know how just yet. Something like

func Green(s string) fmt.Formatter { ... }

Which sounds straight forward, but not in a way that the underlying value in the interface is directly covertable to a string, for example, if Green is a type, then we can do

greenGreeting := Green("hello") + " world"

But if it's a function that returns a fmt.Formatter, the above won't work.

from httpstat.

ChrisHines avatar ChrisHines commented on May 17, 2024
type Green string

func (g Green) Format(f fmt.State, c rune) { ... }

greenGreeting := Green("hello") + " world"
fmt.Println(greenGreeting)

What part of the output from the above code should be green? It seems like the intent is that only "hello" would be green, but greenGreeting has type Green, so the whole message would get formatted by Green.Format which would not be able to tell what portion should be green.

Also, the following wouldn't work, and this inconvenience seems to dilute the value of the approach somewhat.

type Green string

type Blue string

func main() {
    greeting := Green("hello") + Blue(" world") // invalid operation: Green("hello") + Blue(" world") (mismatched types Green and Blue)
    fmt.Printf(greeting)
}

from httpstat.

davecheney avatar davecheney commented on May 17, 2024

I'm sorry, what I wrote previosly kind of got me high on my own supply. What we have now is something like this (simplified to remove the unimportant parts)

name, value := "Connection", "close"
fmt.Printf("%s: %s\n", Green(name), Cyan(value))

Which is all fine and good, but what is happening is fmt.Printf is called with something like this

fmt.Printf("%s: %s\n", "\x1b32mConnection\x1b[0m", "\x1b[36mclose\x1b[0m")

Which to my mind has mixed the data and the presentation up. What I'd like to see is something equivalent to

fmt.Printf("%s: %s\n", fmt.Formatter(Green("Connection")), fmt.Formatter(Cyan("close")))

(The fmt.Formatter conversions are not necessary, just demonstrating that the thing that comes out of Green/Cyan should be a fmt.Formatter)

And if possible the result from Green("Connection") was still convertible to a string, even if it required an explicit conversion.

var s string = string(Green("hello"))

The goal is to push the decision on which terminal formatting codes to apply to the data until as late as possible, way down in the guts of the fmt.Printf.

from httpstat.

ChrisHines avatar ChrisHines commented on May 17, 2024

Two questions.

  1. If Green("hello") implemented fmt.Stringer (in addition to fmt.Formatter), does that qualify as "convertible to a string"?
  2. In general, does the solution need to skip color output when writing to a non-tty?

from httpstat.

davecheney avatar davecheney commented on May 17, 2024
  1. Yes, and in that case there probably isn't a requirement to implement
    both.
  2. Yes

On Mon, 26 Sep 2016, 00:46 Chris Hines [email protected] wrote:

Two questions.

If Green("hello") implemented fmt.Stringer (in addition to
fmt.Formatter), does that qualify as "convertible to a string"?
2.

In general, does the solution need to skip color output when writing
to a non-tty?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#45 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAcA5v43gj30NVWpXqwDIqdatoNbYnLks5qtokzgaJpZM4KF0ZZ
.

from httpstat.

dayvonjersen avatar dayvonjersen commented on May 17, 2024

Is this along the lines of what you were thinking?

https://play.golang.org/p/REwtOS6cLs

(escape codes work in terminal but not in playground)

from httpstat.

ChrisHines avatar ChrisHines commented on May 17, 2024

I just remembered once seeing a package that tackled this problem by providing a specialized version of the fmt functions. The package is at https://github.com/nhooyr/color, but the README now says it's deprecated as of last month. I post it here in case it stimulates any new ideas.

from httpstat.

davecheney avatar davecheney commented on May 17, 2024

I experimented with implementing different coloured strings as types but the result was a net increase in lines and I still had too much generation of a string, adding color tags, passing it around as a string.

I'm unhappy that the presentation (colouring) logic is smeared through the business logic, but I'll suck it up as the alternative is more verbose.

from httpstat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.