Comments (8)
/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.
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.
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.
Two questions.
- If
Green("hello")
implementedfmt.Stringer
(in addition tofmt.Formatter
), does that qualify as "convertible to a string"? - In general, does the solution need to skip color output when writing to a non-tty?
from httpstat.
- Yes, and in that case there probably isn't a requirement to implement
both. - 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.
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.
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.
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)
- Package exists but is not a directory HOT 7
- go get is failing HOT 1
- will this support IOS and Android? HOT 2
- CSV output HOT 1
- Analyzing HTTP timeouts HOT 1
- Is there a speed_download size_download for curl? HOT 1
- Client for azure eventhub (Enhancement/help) HOT 1
- Multiple certificates are not supported when specifying a client certificate with -E
- Allow the user to specify the maximum time of the operation HOT 1
- Allow the user to specify additional CA certificates for SSL peer validation
- DNS through socks5 proxy HOT 1
- Flag: don't discard body, add syntax highlighting
- Flag/option to force DNS resolution HOT 4
- why did you say that reorx/httpstat is a shameless ripoff? HOT 3
- Multiple Parameters Support ? HOT 6
- httpstat returns 403, doesn't hide timings HOT 4
- function readClientCert use gloabl variable to read cert file
- Assets missing for 1.1.0 release HOT 2
- Can't add extra headers HOT 2
- Request for adding JSON output support
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from httpstat.