Skip to content
Myr

color.go

This file

Path

color.go

95 lines · 3.0 KiB · Go

Last changed by

docs: fix command and environment godoc examples (#1833)Hsnbefore adoption33f6a40

History of this file

Take it

1package tea23import (4	"image/color"56	uv "github.com/charmbracelet/ultraviolet"7)89// backgroundColorMsg is a message that requests the terminal background color.10type backgroundColorMsg struct{}1112// RequestBackgroundColor is a command that requests the terminal background color.13// Use [WithInput] to enable input, since the reply cannot be read otherwise.14func RequestBackgroundColor() Msg {15	return backgroundColorMsg{}16}1718// foregroundColorMsg is a message that requests the terminal foreground color.19type foregroundColorMsg struct{}2021// RequestForegroundColor is a command that requests the terminal foreground color.22// Use [WithInput] to enable input, since the reply cannot be read otherwise.23func RequestForegroundColor() Msg {24	return foregroundColorMsg{}25}2627// cursorColorMsg is a message that requests the terminal cursor color.28type cursorColorMsg struct{}2930// RequestCursorColor is a command that requests the terminal cursor color.31// Use [WithInput] to enable input, since the reply cannot be read otherwise.32func RequestCursorColor() Msg {33	return cursorColorMsg{}34}3536// ForegroundColorMsg represents a foreground color message. This message is37// emitted when the program requests the terminal foreground color with the38// [RequestForegroundColor] Cmd.39type ForegroundColorMsg struct{ color.Color }4041// String returns the hex representation of the color.42func (e ForegroundColorMsg) String() string {43	return uv.ForegroundColorEvent(e).String()44}4546// IsDark returns whether the color is dark.47func (e ForegroundColorMsg) IsDark() bool {48	return uv.ForegroundColorEvent(e).IsDark()49}5051// BackgroundColorMsg represents a background color message. This message is52// emitted when the program requests the terminal background color with the53// [RequestBackgroundColor] Cmd.54//55// This is commonly used in [Model.Init] to get the terminal background color56// for style definitions. For that you'll want to call57// [BackgroundColorMsg.IsDark] to determine if the color is dark or light. For58// example:59//60//	func (m model) Init() Cmd {61//	  return RequestBackgroundColor62//	}63//64//	func (m model) Update(msg Msg) (Model, Cmd) {65//	  switch msg := msg.(type) {66//	  case BackgroundColorMsg:67//	      m.styles = newStyles(msg.IsDark())68//	  }69//	  return m, nil70//	}71type BackgroundColorMsg struct{ color.Color }7273// String returns the hex representation of the color.74func (e BackgroundColorMsg) String() string {75	return uv.BackgroundColorEvent(e).String()76}7778// IsDark returns whether the color is dark.79func (e BackgroundColorMsg) IsDark() bool {80	return uv.BackgroundColorEvent(e).IsDark()81}8283// CursorColorMsg represents a cursor color change message. This message is84// emitted when the program requests the terminal cursor color.85type CursorColorMsg struct{ color.Color }8687// String returns the hex representation of the color.88func (e CursorColorMsg) String() string {89	return uv.CursorColorEvent(e).String()90}9192// IsDark returns whether the color is dark.93func (e CursorColorMsg) IsDark() bool {94	return uv.CursorColorEvent(e).IsDark()95}