-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7cbf57
commit 3302047
Showing
4 changed files
with
223 additions
and
8 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
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,144 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
tea "github.com/charmbracelet/bubbletea/v2" | ||
"github.com/charmbracelet/lipgloss/v2" | ||
"github.com/charmbracelet/lipgloss/v2/impure" | ||
) | ||
|
||
var ( | ||
frameColor = impure.AdaptiveColor{Light: lipgloss.Color("#C5ADF9"), Dark: lipgloss.Color("#864EFF")} | ||
textColor = impure.AdaptiveColor{Light: lipgloss.Color("#696969"), Dark: lipgloss.Color("#bdbdbd")} | ||
keywordColor = impure.AdaptiveColor{Light: lipgloss.Color("#37CD96"), Dark: lipgloss.Color("#22C78A")} | ||
inactiveBgColor = impure.AdaptiveColor{Light: lipgloss.Color(0x988F95), Dark: lipgloss.Color(0x978692)} | ||
inactiveFgColor = impure.AdaptiveColor{Light: lipgloss.Color(0xFDFCE3), Dark: lipgloss.Color(0xFBFAE7)} | ||
) | ||
|
||
// Style definitions. | ||
type styles struct { | ||
frame, | ||
paragraph, | ||
text, | ||
keyword, | ||
activeButton, | ||
inactiveButton lipgloss.Style | ||
} | ||
|
||
// Styles are initialized based on the background color of the terminal. | ||
func newStyles() (s styles) { | ||
// Define some styles. adaptive.Color() can be used to choose the | ||
// appropriate light or dark color based on the detected background color. | ||
s.frame = lipgloss.NewStyle(). | ||
Border(lipgloss.RoundedBorder()). | ||
BorderForeground(frameColor). | ||
Padding(1, 3). | ||
Margin(1, 3) | ||
s.paragraph = lipgloss.NewStyle(). | ||
Width(40). | ||
MarginBottom(1). | ||
Align(lipgloss.Center) | ||
s.text = lipgloss.NewStyle(). | ||
Foreground(textColor) | ||
s.keyword = lipgloss.NewStyle(). | ||
Foreground(keywordColor). | ||
Bold(true) | ||
|
||
s.activeButton = lipgloss.NewStyle(). | ||
Padding(0, 3). | ||
Background(lipgloss.Color(0xFF6AD2)). // you can also use octal format for colors, i.e 0xff38ec. | ||
Foreground(lipgloss.Color(0xFFFCC2)) | ||
s.inactiveButton = s.activeButton. | ||
Background(inactiveBgColor). | ||
Foreground(inactiveFgColor) | ||
return s | ||
} | ||
|
||
type model struct { | ||
styles styles | ||
yes bool | ||
chosen bool | ||
aborted bool | ||
} | ||
|
||
func (m model) Init() (tea.Model, tea.Cmd) { | ||
m.yes = true | ||
m.styles = newStyles() | ||
return m, nil | ||
} | ||
|
||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case tea.KeyPressMsg: | ||
switch msg.String() { | ||
case "q", "esc", "ctrl+c": | ||
m.aborted = true | ||
return m, tea.Quit | ||
case "enter": | ||
m.chosen = true | ||
return m, tea.Quit | ||
case "left", "right", "h", "l": | ||
m.yes = !m.yes | ||
case "y": | ||
m.yes = true | ||
m.chosen = true | ||
return m, tea.Quit | ||
case "n": | ||
m.yes = false | ||
m.chosen = true | ||
return m, tea.Quit | ||
} | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
func (m model) View() string { | ||
if m.chosen || m.aborted { | ||
// We're about to exit, so wipe the UI. | ||
return "" | ||
} | ||
|
||
var ( | ||
s = m.styles | ||
y = "Yes" | ||
n = "No" | ||
) | ||
|
||
if m.yes { | ||
y = s.activeButton.Render(y) | ||
n = s.inactiveButton.Render(n) | ||
} else { | ||
y = s.inactiveButton.Render(y) | ||
n = s.activeButton.Render(n) | ||
} | ||
|
||
return s.frame.Render( | ||
lipgloss.JoinVertical(lipgloss.Center, | ||
s.paragraph.Render( | ||
s.text.Render("Are you sure you want to eat that ")+ | ||
s.keyword.Render("moderatly ripe")+ | ||
s.text.Render(" banana?"), | ||
), | ||
y+" "+n, | ||
), | ||
) | ||
} | ||
|
||
func main() { | ||
m, err := tea.NewProgram(model{}).Run() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Uh oh: %v", err) | ||
os.Exit(1) | ||
} | ||
|
||
if m := m.(model); m.chosen { | ||
if m.yes { | ||
fmt.Println("Are you sure? It's not ripe yet.") | ||
} else { | ||
fmt.Println("Well, alright. It was probably good, though.") | ||
} | ||
} | ||
} |
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,66 @@ | ||
// This example illustrates how to detect the terminal's background color and | ||
// choose either light or dark colors accordingly when using Lip Gloss in a. | ||
// standalone fashion, i.e. independent of Bubble Tea. | ||
// | ||
// For an example of how to do this in a Bubble Tea program, see the | ||
// 'bubbletea' example. | ||
package main | ||
|
||
import ( | ||
"github.com/charmbracelet/lipgloss/v2" | ||
"github.com/charmbracelet/lipgloss/v2/impure" | ||
) | ||
|
||
var ( | ||
frameColor = impure.AdaptiveColor{Light: lipgloss.Color("#C5ADF9"), Dark: lipgloss.Color("#864EFF")} | ||
textColor = impure.AdaptiveColor{Light: lipgloss.Color("#696969"), Dark: lipgloss.Color("#bdbdbd")} | ||
keywordColor = impure.AdaptiveColor{Light: lipgloss.Color("#37CD96"), Dark: lipgloss.Color("#22C78A")} | ||
inactiveBgColor = impure.AdaptiveColor{Light: lipgloss.Color(0x988F95), Dark: lipgloss.Color(0x978692)} | ||
inactiveFgColor = impure.AdaptiveColor{Light: lipgloss.Color(0xFDFCE3), Dark: lipgloss.Color(0xFBFAE7)} | ||
) | ||
|
||
func main() { | ||
// Define some styles. adaptive.Color() can be used to choose the | ||
// appropriate light or dark color based on the detected background color. | ||
frameStyle := lipgloss.NewStyle(). | ||
Border(lipgloss.RoundedBorder()). | ||
BorderForeground(frameColor). | ||
Padding(1, 3). | ||
Margin(1, 3) | ||
paragraphStyle := lipgloss.NewStyle(). | ||
Width(40). | ||
MarginBottom(1). | ||
Align(lipgloss.Center) | ||
textStyle := lipgloss.NewStyle(). | ||
Foreground(textColor) | ||
keywordStyle := lipgloss.NewStyle(). | ||
Foreground(keywordColor). | ||
Bold(true) | ||
|
||
activeButton := lipgloss.NewStyle(). | ||
Padding(0, 3). | ||
Background(lipgloss.Color(0xFF6AD2)). // you can also use octal format for colors, i.e 0xff38ec. | ||
Foreground(lipgloss.Color(0xFFFCC2)) | ||
inactiveButton := activeButton. | ||
Background(inactiveBgColor). | ||
Foreground(inactiveFgColor) | ||
|
||
// Build layout. | ||
text := paragraphStyle.Render( | ||
textStyle.Render("Are you sure you want to eat that ") + | ||
keywordStyle.Render("moderatly ripe") + | ||
textStyle.Render(" banana?"), | ||
) | ||
buttons := activeButton.Render("Yes") + " " + inactiveButton.Render("No") | ||
block := frameStyle.Render( | ||
lipgloss.JoinVertical(lipgloss.Center, text, buttons), | ||
) | ||
|
||
// Print the block to stdout. It's important to use Lip Gloss's print | ||
// functions to ensure that colors are downsampled correctly. If output | ||
// isn't a TTY (i.e. we're logging to a file) colors will be stripped | ||
// entirely. | ||
// | ||
// Note that in Bubble Tea downsampling happens automatically. | ||
impure.Println(block) | ||
} |