Skip to content

Commit

Permalink
Merge branch 'charmbracelet:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
BigJk authored Dec 22, 2023
2 parents 5e8e510 + b669547 commit 25c0e0b
Show file tree
Hide file tree
Showing 57 changed files with 2,267 additions and 809 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.golden -text
8 changes: 6 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ jobs:
GO111MODULE: "on"
steps:
- name: Install Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Download Go modules
run: go mod download
Expand All @@ -32,6 +32,10 @@ jobs:
run: go build -v ./...
working-directory: ./examples

- name: Test examples
run: go test -v ./...
working-directory: ./examples

- name: Build tutorials
run: go build -v ./...
working-directory: ./tutorials
4 changes: 2 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ jobs:
GO111MODULE: "on"
steps:
- name: Install Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Coverage
env:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ jobs:
contents: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '^1'
cache: true
- shell: bash
run: |
(cd ./examples && go mod tidy)
(cd ./tutorials && go mod tidy)
- uses: stefanzweifel/git-auto-commit-action@v4
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "chore: go mod tidy tutorials and examples"
branch: master
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/lint-soft.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ^1

- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ^1

- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,10 @@ For some Bubble Tea programs in production, see:
* [gh-dash](https://www.github.com/dlvhdr/gh-dash): a GitHub CLI extension for PRs and issues
* [gitflow-toolkit](https://github.com/mritd/gitflow-toolkit): a GitFlow submission tool
* [Glow](https://github.com/charmbracelet/glow): a markdown reader, browser, and online markdown stash
* [go-sweep](https://github.com/maxpaulus43/go-sweep): Minesweeper in the terminal
* [gocovsh](https://github.com/orlangure/gocovsh): explore Go coverage reports from the CLI
* [got](https://github.com/fedeztk/got): a simple translator and text-to-speech app build on top of simplytranslate's APIs
* [hiSHtory](https://github.com/ddworken/hishtory): your shell history in context, synced, and queryable
* [httpit](https://github.com/gonetx/httpit): a rapid http(s) benchmark tool
* [IDNT](https://github.com/r-darwish/idnt): a batch software uninstaller
* [kboard](https://github.com/CamiloGarciaLaRotta/kboard): a typing game
Expand All @@ -351,24 +353,29 @@ For some Bubble Tea programs in production, see:
* [mergestat](https://github.com/mergestat/mergestat): run SQL queries on git repositories
* [Neon Modem Overdrive](https://github.com/mrusme/neonmodem): a BBS-style TUI client for Discourse, Lemmy, Lobste.rs and Hacker News
* [Noted](https://github.com/torbratsberg/noted): a note viewer and manager
* [nom](https://github.com/guyfedwards/nom): RSS reader and manager
* [pathos](https://github.com/chip/pathos): a PATH env variable editor
* [portal](https://github.com/ZinoKader/portal): secure transfers between computers
* [redis-viewer](https://github.com/SaltFishPr/redis-viewer): a Redis databases browser
* [scrabbler](https://github.com/wI2L/scrabbler): Automatic draw TUI for your duplicate Scrabble games
* [sku](https://github.com/fedeztk/sku): Sudoku on the CLI
* [Slides](https://github.com/maaslalani/slides): a markdown-based presentation tool
* [SlurmCommander](https://github.com/CLIP-HPC/SlurmCommander): a Slurm workload manager TUI
* [Soft Serve](https://github.com/charmbracelet/soft-serve): a command-line-first Git server that runs a TUI over SSH
* [solitaire-tui](https://github.com/brianstrauch/solitaire-tui): Klondike Solitaire for the terminal
* [StormForge Optimize Controller](https://github.com/thestormforge/optimize-controller): a tool for experimenting with application configurations in Kubernetes
* [Storydb](https://github.com/grrlopes/storydb): a bash/zsh ctrl+r improved command history finder.
* [STTG](https://github.com/wille1101/sttg): a teletext client for SVT, Sweden’s national public television station
* [sttr](https://github.com/abhimanyu003/sttr): a general-purpose text transformer
* [tasktimer](https://github.com/caarlos0/tasktimer): a dead-simple task timer
* [termdbms](https://github.com/mathaou/termdbms): a keyboard and mouse driven database browser
* [ticker](https://github.com/achannarasappa/ticker): a terminal stock viewer and stock position tracker
* [tran](https://github.com/abdfnx/tran): securely transfer stuff between computers (based on [portal](https://github.com/ZinoKader/portal))
* [Typer](https://github.com/maaslalani/typer): a typing test
* [typioca](https://github.com/bloznelis/typioca): Cozy typing speed tester in terminal
* [tz](https://github.com/oz/tz): an aid for scheduling across multiple time zones
* [ugm](https://github.com/ariasmn/ugm): a unix user and group browser
* [walk](https://github.com/antonmedv/walk): a terminal navigator
* [wander](https://github.com/robinovitch61/wander): a HashiCorp Nomad terminal client
* [WG Commander](https://github.com/AndrianBdn/wg-cmd): a TUI for a simple WireGuard VPN setup
* [wishlist](https://github.com/charmbracelet/wishlist): an SSH directory
Expand Down
19 changes: 18 additions & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// return tea.Batch(someCommand, someOtherCommand)
// }
func Batch(cmds ...Cmd) Cmd {
var validCmds []Cmd
var validCmds []Cmd //nolint:prealloc
for _, c := range cmds {
if c == nil {
continue
Expand Down Expand Up @@ -170,3 +170,20 @@ func Sequentially(cmds ...Cmd) Cmd {
return nil
}
}

// setWindowTitleMsg is an internal message used to set the window title.
type setWindowTitleMsg string

// SetWindowTitle produces a command that sets the terminal title.
//
// For example:
//
// func (m model) Init() Cmd {
// // Set title.
// return tea.SetWindowTitle("My App")
// }
func SetWindowTitle(title string) Cmd {
return func() Msg {
return setWindowTitleMsg(title)
}
}
107 changes: 107 additions & 0 deletions examples/autocomplete/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

func main() {
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
}

type gotReposSuccessMsg []repo
type gotReposErrMsg error

type repo struct {
Name string `json:"name"`
}

const reposURL = "https://api.github.com/orgs/charmbracelet/repos"

func getRepos() tea.Msg {
req, err := http.NewRequest(http.MethodGet, reposURL, nil)
if err != nil {
return gotReposErrMsg(err)
}

req.Header.Add("Accept", "application/vnd.github+json")
req.Header.Add("X-GitHub-Api-Version", "2022-11-28")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return gotReposErrMsg(err)
}
defer resp.Body.Close() // nolint: errcheck

data, err := io.ReadAll(resp.Body)
if err != nil {
return gotReposErrMsg(err)
}

var repos []repo

err = json.Unmarshal(data, &repos)
if err != nil {
return gotReposErrMsg(err)
}

return gotReposSuccessMsg(repos)
}

type model struct {
textInput textinput.Model
}

func initialModel() model {
ti := textinput.New()
ti.Prompt = "charmbracelet/"
ti.Placeholder = "repo..."
ti.Cursor.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("63"))
ti.Focus()
ti.CharLimit = 50
ti.Width = 20
ti.ShowSuggestions = true
return model{textInput: ti}
}

func (m model) Init() tea.Cmd {
return tea.Batch(getRepos, textinput.Blink)
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEnter, tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
}
case gotReposSuccessMsg:
var suggestions []string
for _, r := range msg {
suggestions = append(suggestions, r.Name)
}
m.textInput.SetSuggestions(suggestions)
}

var cmd tea.Cmd
m.textInput, cmd = m.textInput.Update(msg)
return m, cmd
}

func (m model) View() string {
return fmt.Sprintf(
"What’s your favorite Charm repository?\n\n%s\n\n%s\n",
m.textInput.View(),
"(tab to complete, ctrl+n/ctrl+p to cycle through suggestions, esc to quit)",
)
}
30 changes: 13 additions & 17 deletions examples/cellbuffer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ const (
fps = 60
frequency = 7.5
damping = 0.15
asterisk = "*"
)

func drawEllipse(cb *cellbuffer, xc, yc, rx, ry float64) {
const c = "*"
var (
dx, dy, d1, d2 float64
x float64 = 0
y = ry
x float64
y = ry
)

d1 = ry*ry - rx*rx*ry + 0.25*rx*rx
dx = 2 * ry * ry * x
dy = 2 * rx * rx * y

for dx < dy {
cb.set(c, int(x+xc), int(y+yc))
cb.set(c, int(-x+xc), int(y+yc))
cb.set(c, int(x+xc), int(-y+yc))
cb.set(c, int(-x+xc), int(-y+yc))
cb.set(int(x+xc), int(y+yc))
cb.set(int(-x+xc), int(y+yc))
cb.set(int(x+xc), int(-y+yc))
cb.set(int(-x+xc), int(-y+yc))
if d1 < 0 {
x++
dx = dx + (2 * ry * ry)
Expand All @@ -53,10 +53,10 @@ func drawEllipse(cb *cellbuffer, xc, yc, rx, ry float64) {
d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) + ((rx * rx) * ((y - 1) * (y - 1))) - (rx * rx * ry * ry)

for y >= 0 {
cb.set(c, int(x+xc), int(y+yc))
cb.set(c, int(-x+xc), int(y+yc))
cb.set(c, int(x+xc), int(-y+yc))
cb.set(c, int(-x+xc), int(-y+yc))
cb.set(int(x+xc), int(y+yc))
cb.set(int(-x+xc), int(y+yc))
cb.set(int(x+xc), int(-y+yc))
cb.set(int(-x+xc), int(-y+yc))
if d2 > 0 {
y--
dy = dy - (2 * rx * rx)
Expand Down Expand Up @@ -85,16 +85,12 @@ func (c *cellbuffer) init(w, h int) {
c.wipe()
}

func (c cellbuffer) set(v string, x, y int) {
func (c cellbuffer) set(x, y int) {
i := y*c.stride + x
if i > len(c.cells)-1 || x < 0 || y < 0 || x >= c.width() || y >= c.height() {
return
}
c.cells[i] = v
}

func (c *cellbuffer) clear(x, y int) {
c.set(" ", x, y)
c.cells[i] = asterisk
}

func (c *cellbuffer) wipe() {
Expand Down
2 changes: 1 addition & 1 deletion examples/composable-views/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} else {
m.Next()
m.resetSpinner()
cmds = append(cmds, spinner.Tick)
cmds = append(cmds, m.spinner.Tick)
}
}
switch m.state {
Expand Down
7 changes: 4 additions & 3 deletions examples/credit-card-form/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ func ccnValidator(s string) error {
return fmt.Errorf("CCN is too long")
}

if len(s) == 0 || len(s)%5 != 0 && (s[len(s)-1] < '0' || s[len(s)-1] > '9') {
return fmt.Errorf("CCN is invalid")
}

// The last digit should be a number unless it is a multiple of 4 in which
// case it should be a space
if len(s)%5 == 0 && s[len(s)-1] != ' ' {
return fmt.Errorf("CCN must separate groups with spaces")
}
if len(s)%5 != 0 && (s[len(s)-1] < '0' || s[len(s)-1] > '9') {
return fmt.Errorf("CCN is invalid")
}

// The remaining digits should be integers
c := strings.ReplaceAll(s, " ", "")
Expand Down
Loading

0 comments on commit 25c0e0b

Please sign in to comment.