New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow unmanaged output above the app in standard renderer #249
Merged
meowgorithm
merged 12 commits into
charmbracelet:master
from
Adjective-Object:println-renderer
Jun 22, 2022
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
db140c8
merge Adjective-Object/tea_log_renderer into standard renderer
Adjective-Object d7204d3
rename queuedMessages -> queuedMessageLines & break apart strings dur…
33fa6f5
delete cursorDownBy
c604d40
+= 1 -> ++ to make the linter happy
Adjective-Object d8bde7d
add skipLines[] tracking back to standard renderer, and add rename sk…
3bd5e17
request repaint when a message is recieved
794be04
Convert Println and Printf to commands
meowgorithm a8e1aac
Add package manager example demonstrating tea.Printf
meowgorithm 54c29ad
Use Unix instead of UnixMicro for Go 1.13 support in CI
meowgorithm c4c903e
fix off by one in std renderer
c2ab77d
add Printf/Println to tea.go
a3d0fd3
revert attempt at sequence compression + cursorUpBy
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/charmbracelet/bubbles/progress" | ||
"github.com/charmbracelet/bubbles/spinner" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
type model struct { | ||
packages []string | ||
index int | ||
width int | ||
height int | ||
spinner spinner.Model | ||
progress progress.Model | ||
done bool | ||
} | ||
|
||
var ( | ||
currentPkgNameStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("211")) | ||
subtleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("239")) | ||
doneStyle = lipgloss.NewStyle().Margin(1, 2) | ||
checkMark = lipgloss.NewStyle().Foreground(lipgloss.Color("42")).SetString("✓") | ||
) | ||
|
||
func newModel() model { | ||
p := progress.New( | ||
progress.WithDefaultGradient(), | ||
progress.WithWidth(40), | ||
progress.WithoutPercentage(), | ||
) | ||
s := spinner.New() | ||
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("63")) | ||
return model{ | ||
packages: getPackages(), | ||
spinner: s, | ||
progress: p, | ||
} | ||
} | ||
|
||
func (m model) Init() tea.Cmd { | ||
return tea.Batch(downloadAndInstall(m.packages[m.index]), m.spinner.Tick) | ||
} | ||
|
||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case tea.WindowSizeMsg: | ||
m.width, m.height = msg.Width, msg.Height | ||
case tea.KeyMsg: | ||
switch msg.String() { | ||
case "ctrl+c", "esc", "q": | ||
return m, tea.Quit | ||
} | ||
case installedPkgMsg: | ||
if m.index >= len(m.packages)-1 { | ||
// Everything's been installed. We're done! | ||
m.done = true | ||
return m, tea.Quit | ||
} | ||
|
||
// Update progress bar | ||
progressCmd := m.progress.SetPercent(float64(m.index) / float64(len(m.packages)-1)) | ||
|
||
m.index++ | ||
return m, tea.Batch( | ||
progressCmd, | ||
tea.Printf("%s %s", checkMark, m.packages[m.index]), // print success message above our program | ||
downloadAndInstall(m.packages[m.index]), // download the next package | ||
) | ||
case spinner.TickMsg: | ||
var cmd tea.Cmd | ||
m.spinner, cmd = m.spinner.Update(msg) | ||
return m, cmd | ||
case progress.FrameMsg: | ||
newModel, cmd := m.progress.Update(msg) | ||
if newModel, ok := newModel.(progress.Model); ok { | ||
m.progress = newModel | ||
} | ||
return m, cmd | ||
} | ||
return m, nil | ||
} | ||
|
||
func (m model) View() string { | ||
n := len(m.packages) | ||
w := lipgloss.Width(fmt.Sprintf("%d", n)) | ||
|
||
if m.done { | ||
return doneStyle.Render(fmt.Sprintf("Done! Installed %d packages.\n", n)) | ||
} | ||
|
||
pkgCount := fmt.Sprintf(" %*d/%*d", w, m.index, w, n-1) | ||
|
||
spin := m.spinner.View() + " " | ||
prog := m.progress.View() | ||
cellsAvail := max(0, m.width-lipgloss.Width(spin+prog+pkgCount)) | ||
|
||
pkgName := currentPkgNameStyle.Render(m.packages[m.index]) | ||
info := lipgloss.NewStyle().MaxWidth(cellsAvail).Render("Installing " + pkgName) | ||
|
||
cellsRemaining := max(0, m.width-lipgloss.Width(spin+info+prog+pkgCount)) | ||
gap := strings.Repeat(" ", cellsRemaining) | ||
|
||
return spin + info + gap + prog + pkgCount | ||
} | ||
|
||
type installedPkgMsg string | ||
|
||
func downloadAndInstall(pkg string) tea.Cmd { | ||
// This is where you'd do i/o stuff to download and install packages. In | ||
// our case we're just pausing for a moment to simulate the process. | ||
d := time.Millisecond * time.Duration(rand.Intn(500)) | ||
return tea.Tick(d, func(t time.Time) tea.Msg { | ||
return installedPkgMsg(pkg) | ||
}) | ||
} | ||
|
||
func max(a, b int) int { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} | ||
|
||
func main() { | ||
rand.Seed(time.Now().Unix()) | ||
|
||
if err := tea.NewProgram(newModel()).Start(); err != nil { | ||
fmt.Println("Error running program:", err) | ||
os.Exit(1) | ||
} | ||
} |
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,52 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
) | ||
|
||
var packages = []string{ | ||
"vegeutils", | ||
"libgardening", | ||
"currykit", | ||
"spicerack", | ||
"fullenglish", | ||
"eggy", | ||
"bad-kitty", | ||
"chai", | ||
"hojicha", | ||
"libtacos", | ||
"babys-monads", | ||
"libpurring", | ||
"currywurst-devel", | ||
"xmodmeow", | ||
"licorice-utils", | ||
"cashew-apple", | ||
"rock-lobster", | ||
"standmixer", | ||
"coffee-CUPS", | ||
"libesszet", | ||
"zeichenorientierte-benutzerschnittstellen", | ||
"schnurrkit", | ||
"old-socks-devel", | ||
"jalapeño", | ||
"molasses-utils", | ||
"xkohlrabi", | ||
"party-gherkin", | ||
"snow-peas", | ||
"libyuzu", | ||
} | ||
|
||
func getPackages() []string { | ||
pkgs := packages | ||
copy(pkgs, packages) | ||
|
||
rand.Shuffle(len(pkgs), func(i, j int) { | ||
pkgs[i], pkgs[j] = pkgs[j], pkgs[i] | ||
}) | ||
|
||
for k := range pkgs { | ||
pkgs[k] += fmt.Sprintf("-%d.%d.%d", rand.Intn(10), rand.Intn(10), rand.Intn(10)) | ||
} | ||
return pkgs | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newLines might be greater than the size of the appliation, so we can't use it as a loop tracker anymore for the above loop.
Might be worth renaming linesRendered to something like "linesInAppOnLastFlush", or "appHeight" or something similar to avoid confusing the size of the current render with the size we're considering the bubbletea-controlled area of the terminal.