forked from muesli/termenv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
termenv.Output can be initialized with a custom os.File as an output and supports auto detection of its capabilities. Alternatively you can also skip auto detection and create a new instance with a specific profile. Styles (just like Colors) can now be created for specific profiles, with the Ascii profile stripping away any applied styling.
- Loading branch information
Showing
5 changed files
with
389 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package termenv | ||
|
||
import "os" | ||
|
||
// Output is a terminal output. | ||
type Output struct { | ||
Profile | ||
tty *os.File | ||
} | ||
|
||
// NewOutput returns a new Output for the given file descriptor. | ||
func NewOutput(tty *os.File) *Output { | ||
p := Ascii | ||
if isTTY(tty.Fd()) { | ||
p = envColorProfile() | ||
} | ||
|
||
return NewOutputWithProfile(tty, p) | ||
} | ||
|
||
// NewOutputWithProfile returns a new Output for the given file descriptor and | ||
// profile. | ||
func NewOutputWithProfile(tty *os.File, profile Profile) *Output { | ||
return &Output{ | ||
Profile: profile, | ||
tty: tty, | ||
} | ||
} | ||
|
||
// ForegroundColor returns the terminal's default foreground color. | ||
func (o Output) ForegroundColor() Color { | ||
if !isTTY(o.tty.Fd()) { | ||
return NoColor{} | ||
} | ||
|
||
return o.foregroundColor() | ||
} | ||
|
||
// BackgroundColor returns the terminal's default background color. | ||
func (o Output) BackgroundColor() Color { | ||
if !isTTY(o.tty.Fd()) { | ||
return NoColor{} | ||
} | ||
|
||
return o.backgroundColor() | ||
} | ||
|
||
// HasDarkBackground returns whether terminal uses a dark-ish background. | ||
func (o Output) HasDarkBackground() bool { | ||
c := ConvertToRGB(o.BackgroundColor()) | ||
_, _, l := c.Hsl() | ||
return l < 0.5 | ||
} |
Oops, something went wrong.