You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We cannot directly interact with the TTY using fmt.Println because this makes the program difficult to test and difficult to change the output render if we ever need it. We have to create an abstraction layer (like UI object) where we can change stuff in the future if we need it.
I attached a draft of module we could use to perform such output to the user.
package main
import (
"github.com/charmbracelet/glamour""io""os"
)
// StdOutput is a shorthand for io.WritertypeStdOutput io.Writer// StdError is a shorthand for io.WritertypeStdError io.Writer// LogLevel indicates the verbosity of what's going to be printed on screentypeLogLeveluint8// UIOptions is a typedef for setting additional options to the UItypeUIOptionsfunc(*UI, string) stringconst (
// DEBUG will print debug information on screenDEBUGLogLevel=iota// VERBOSE will print almost everythingVERBOSE// NORMAL is for output that is printed without any particular flagNORMAL// QUIET only prints warnings (error will always be printed on StdError)QUIET
)
// UI is the final endpoint for writing output to the final user. It represents a TTY, and it has functionalities for rendering the output even in a markdown flavoured fashiontypeUIstruct {
outputStdOutputerrorStdErrorterm*glamour.TermRenderer
}
// WithMarkdownSyntax is an UIOptions to print the content in a markdown rendered outputfuncWithMarkdownSyntax(ui*UI, contentstring) string {
render, err:=ui.term.Render(content)
iferr!=nil {
panic(err)
}
returnrender
}
func (U*UI) out(levelLogLevel, sstring, opts...UIOptions) (nint, errerror) {
// FIXME loglevelout:=sfor_, opt:=rangeopts {
out=opt(U, s)
}
returnU.output.Write([]byte(out))
}
func (U*UI) markdownOut(sstring) (nint, errerror) {
render, err:=U.term.Render(s)
iferr!=nil {
return0, err
}
returnU.output.Write([]byte(render))
}
func (U*UI) err(sstring) (nint, errerror) {
returnU.error.Write([]byte(s))
}
// NewStdOut returns os.StdoutfuncNewStdOut() StdOutput {
returnos.Stdout
}
// NewStdErr returns os.StderrfuncNewStdErr() StdError {
returnos.Stderr
}
// NewUI creates a brand-new UI structfuncNewUI(outStdOutput, errorOutStdError) *UI {
glamourTermRender, err:=glamour.NewTermRenderer(
glamour.WithAutoStyle(),
glamour.WithWordWrap(80),
glamour.WithEmoji(),
)
iferr!=nil {
panic("Impossible to create terminal. Is this a valid TTY?")
}
return&UI{
output: out,
error: errorOut,
term: glamourTermRender,
}
}
This means we have to check out all the fmt.Print{ln}... in the codebase and rewrite it using this object.
Combined with #11 we can easily have this object injected in all the classes we need to output stuff without ever bothering about initializing one "by hand"
The text was updated successfully, but these errors were encountered:
We cannot directly interact with the TTY using
fmt.Println
because this makes the program difficult to test and difficult to change the output render if we ever need it. We have to create an abstraction layer (likeUI
object) where we can change stuff in the future if we need it.I attached a draft of module we could use to perform such output to the user.
This means we have to check out all the
fmt.Print{ln}...
in the codebase and rewrite it using this object.Combined with #11 we can easily have this object injected in all the classes we need to output stuff without ever bothering about initializing one "by hand"
The text was updated successfully, but these errors were encountered: