mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-10-18 16:38:56 +03:00
Compare commits
31 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
bdd909a5d7 | ||
|
b08b3efa02 | ||
|
83b6a2205f | ||
|
c06af8962d | ||
|
3a65be950a | ||
|
2dd6e0c80b | ||
|
1e50e6d291 | ||
|
573f90a876 | ||
|
a498b857d6 | ||
|
82097a1c79 | ||
|
a8ec421800 | ||
|
9c10cd0586 | ||
|
154f3763f7 | ||
|
eff931bdea | ||
|
0d7b3ed8e8 | ||
|
cbafae84f9 | ||
|
959598c9aa | ||
|
5f7c8b5375 | ||
|
75df6b96a1 | ||
|
084d0acefd | ||
|
e8526ec4ef | ||
|
68f11d0b63 | ||
|
f78a02cc29 | ||
|
9a4fbb3be1 | ||
|
d4a0ecd3ee | ||
|
34b1f47068 | ||
|
dbd40713ce | ||
|
54a0d84255 | ||
|
004511e00f | ||
|
7fa53ea961 | ||
|
0cc5e71a63 |
37
.github/workflows/build.yml
vendored
Normal file
37
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
name: build
|
||||||
|
on: [push, pull_request]
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: [1.13.x, 1.14.x, 1.15.x]
|
||||||
|
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
env:
|
||||||
|
GO111MODULE: "on"
|
||||||
|
steps:
|
||||||
|
- name: Install Go
|
||||||
|
uses: actions/setup-go@v1
|
||||||
|
with:
|
||||||
|
go-version: ${{ matrix.go-version }}
|
||||||
|
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Download Go modules
|
||||||
|
run: go mod download
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: go build -v ./...
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: go test ./...
|
||||||
|
|
||||||
|
- name: Coverage
|
||||||
|
env:
|
||||||
|
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
run: |
|
||||||
|
go test -race -covermode atomic -coverprofile=profile.cov ./...
|
||||||
|
GO111MODULE=off go get github.com/mattn/goveralls
|
||||||
|
$(go env GOPATH)/bin/goveralls -coverprofile=profile.cov -service=github
|
||||||
|
if: matrix.go-version == '1.15.x' && matrix.os == 'ubuntu-latest'
|
20
.github/workflows/lint.yml
vendored
Normal file
20
.github/workflows/lint.yml
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
name: lint
|
||||||
|
on: [push, pull_request]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
golangci:
|
||||||
|
name: lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: golangci-lint
|
||||||
|
uses: golangci/golangci-lint-action@v2
|
||||||
|
with:
|
||||||
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
version: v1.30
|
||||||
|
# Optional: golangci-lint command line arguments.
|
||||||
|
args: --issues-exit-code=0
|
||||||
|
# Optional: working directory, useful for monorepos
|
||||||
|
# working-directory: somedir
|
||||||
|
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||||
|
only-new-issues: true
|
26
.golangci.yml
Normal file
26
.golangci.yml
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
run:
|
||||||
|
tests: false
|
||||||
|
|
||||||
|
issues:
|
||||||
|
max-issues-per-linter: 0
|
||||||
|
max-same-issues: 0
|
||||||
|
|
||||||
|
linters:
|
||||||
|
enable:
|
||||||
|
- bodyclose
|
||||||
|
- dupl
|
||||||
|
- exportloopref
|
||||||
|
- goconst
|
||||||
|
- godot
|
||||||
|
- godox
|
||||||
|
- goimports
|
||||||
|
- gomnd
|
||||||
|
- goprintffuncname
|
||||||
|
- gosec
|
||||||
|
- misspell
|
||||||
|
- prealloc
|
||||||
|
- rowserrcheck
|
||||||
|
- sqlclosecheck
|
||||||
|
- unconvert
|
||||||
|
- unparam
|
||||||
|
- whitespace
|
73
README.md
73
README.md
@@ -1,34 +1,79 @@
|
|||||||
# Bubbles
|
Bubbles
|
||||||
|
=======
|
||||||
|
|
||||||
Some components for [Bubble Tea](https://github.com/charmbraclet/bubbletea):
|
[](https://github.com/charmbracelet/bubbles/actions)
|
||||||
|
[](http://goreportcard.com/report/charmbracelet/bubbles)
|
||||||
|
[](https://pkg.go.dev/github.com/charmbracelet/bubbles)
|
||||||
|
|
||||||
|
Some components for [Bubble Tea](https://github.com/charmbracelet/bubbletea) applications.
|
||||||
|
|
||||||
|
These components are used in production in [Glow][glow] and [Charm][charm].
|
||||||
|
|
||||||
|
[glow]: https://github.com/charmbracelet/glow
|
||||||
|
[charm]: https://github.com/charmbracelet/charm
|
||||||
|
|
||||||
* Spinner
|
|
||||||
* Text Input
|
|
||||||
* Paginator
|
|
||||||
* Viewport
|
|
||||||
|
|
||||||
## Spinner
|
## Spinner
|
||||||
|
|
||||||
A spinner, useful for indicating that some kind of invisible operation is
|
<img src="https://stuff.charm.sh/bubbles-examples/spinner.gif" width="400" alt="Spinner Example">
|
||||||
happening. There are a couple default ones, but you can also pass your own
|
|
||||||
”frames.”
|
A spinner, useful for indicating that some kind an operation is happening.
|
||||||
|
There are a couple default ones, but you can also pass your own ”frames.”
|
||||||
|
|
||||||
|
* [Example code](https://github.com/charmbracelet/tea/tree/master/examples/spinner/main.go)
|
||||||
|
|
||||||
|
|
||||||
## Text Input
|
## Text Input
|
||||||
|
|
||||||
A text input field, akin to an `<input type="text">` in HTML.
|
<img src="https://stuff.charm.sh/bubbles-examples/textinput.gif" width="400" alt="Text Input Example">
|
||||||
|
|
||||||
|
A text input field, akin to an `<input type="text">` in HTML. Supports unicode,
|
||||||
|
pasting, in-place scrolling when the value exceeds the width of the element and
|
||||||
|
the common, and many customization options.
|
||||||
|
|
||||||
|
* [Example code, one field](https://github.com/charmbracelet/tea/tree/master/examples/textinput/main.go)
|
||||||
|
* [Example code, many fields](https://github.com/charmbracelet/tea/tree/master/examples/textinput/main.go)
|
||||||
|
|
||||||
|
|
||||||
## Paginator
|
## Paginator
|
||||||
|
|
||||||
|
<img src="https://stuff.charm.sh/bubbles-examples/pagination.gif" width="200" alt="Paginator Example">
|
||||||
|
|
||||||
A component for handling pagination logic and optionally drawing pagination UI.
|
A component for handling pagination logic and optionally drawing pagination UI.
|
||||||
|
Supports "dot-style" pagination (similar to what you might see on iOS) and
|
||||||
|
numeric page numbering, but you could also just use this component for the
|
||||||
|
logic and visualize pagination however you like.
|
||||||
|
|
||||||
|
This component is used in [Glow][glow] to browse documents and [Charm][charm] to
|
||||||
|
browse SSH keys.
|
||||||
|
|
||||||
|
|
||||||
## Viewport
|
## Viewport
|
||||||
|
|
||||||
A viewport for vertically scrolling content which optionally includes standard
|
<img src="https://stuff.charm.sh/bubbles-examples/viewport.gif" width="600" alt="Viewport Example">
|
||||||
|
|
||||||
|
A viewport for vertically scrolling content. Optionally includes standard
|
||||||
pager keybindings and mouse wheel support. A high performance mode is available
|
pager keybindings and mouse wheel support. A high performance mode is available
|
||||||
for applications which make use of the alterate screen buffer. This is
|
for applications which make use of the alterate screen buffer.
|
||||||
generally only necessary when dealing with content with a very large amount of
|
|
||||||
ANSI escape sequences.
|
* [Example code](https://github.com/charmbracelet/tea/tree/master/examples/pager/main.go)
|
||||||
|
|
||||||
|
This component is well complimented with [Reflow][reflow] for ANSI-aware
|
||||||
|
indenting and text wrapping.
|
||||||
|
|
||||||
|
[reflow]: https://github.com/muesli/reflow
|
||||||
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
[MIT](https://github.com/charmbracelet/teaparty/raw/master/LICENSE)
|
[MIT](https://github.com/charmbracelet/teaparty/raw/master/LICENSE)
|
||||||
|
|
||||||
|
|
||||||
|
***
|
||||||
|
|
||||||
|
Part of [Charm](https://charm.sh).
|
||||||
|
|
||||||
|
<a href="https://charm.sh/"><img alt="the Charm logo" src="https://stuff.charm.sh/charm-badge.jpg" width="400"></a>
|
||||||
|
|
||||||
|
Charm热爱开源! / Charm loves open source!
|
||||||
|
|
||||||
|
12
go.mod
12
go.mod
@@ -3,12 +3,10 @@ module github.com/charmbracelet/bubbles
|
|||||||
go 1.13
|
go 1.13
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/atotto/clipboard v0.1.2 // indirect
|
github.com/atotto/clipboard v0.1.2
|
||||||
github.com/charmbracelet/bubbletea v0.9.1-0.20200713153904-2f53eeb54b90
|
github.com/charmbracelet/bubbletea v0.12.1
|
||||||
github.com/mattn/go-runewidth v0.0.9
|
github.com/mattn/go-runewidth v0.0.9
|
||||||
github.com/muesli/termenv v0.5.3-0.20200625163851-04b5c30e4c04
|
github.com/muesli/termenv v0.7.4
|
||||||
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 // indirect
|
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
|
||||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
|
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a // indirect
|
||||||
)
|
)
|
||||||
|
|
||||||
replace github.com/charmbracelet/bubbletea => ../bubbletea
|
|
||||||
|
31
go.sum
31
go.sum
@@ -1,5 +1,9 @@
|
|||||||
github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY=
|
github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY=
|
||||||
github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
||||||
|
github.com/charmbracelet/bubbletea v0.12.1 h1:t21pkG2IDBRduPbt2J64Dx5yt8yIidAkXwhhrc11SzY=
|
||||||
|
github.com/charmbracelet/bubbletea v0.12.1/go.mod h1:3gZkYELUOiEUOp0bTInkxguucy/xRbGSOcbMs1geLxg=
|
||||||
|
github.com/containerd/console v1.0.1 h1:u7SFAJyRqWcG6ogaMAx3KjSTy1e3hT9QxqX7Jco7dRc=
|
||||||
|
github.com/containerd/console v1.0.1/go.mod h1:XUsP6YE/mKtz6bxc+I8UiKKTP04qjQL4qcS3XoQ5xkw=
|
||||||
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4=
|
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4=
|
||||||
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4=
|
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4=
|
||||||
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
|
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
|
||||||
@@ -8,21 +12,24 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX
|
|||||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||||
github.com/muesli/termenv v0.5.3-0.20200625163851-04b5c30e4c04 h1:Wr876oXlAk6avTWi0daXAriOr+r5fqIuyDmtNc/KwY0=
|
github.com/muesli/termenv v0.7.2 h1:r1raklL3uKE7rOvWgSenmEm2px+dnc33OTisZ8YR1fw=
|
||||||
github.com/muesli/termenv v0.5.3-0.20200625163851-04b5c30e4c04/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=
|
github.com/muesli/termenv v0.7.2/go.mod h1:ct2L5N2lmix82RaY3bMWwVu/jUFc9Ule0KGDCiKYPh8=
|
||||||
github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03 h1:pd4YKIqCB0U7O2I4gWHgEUA2mCEOENmco0l/bM957bU=
|
github.com/muesli/termenv v0.7.4 h1:/pBqvU5CpkY53tU0vVn+xgs2ZTX63aH5nY+SSps5Xa8=
|
||||||
github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03/go.mod h1:Z9+Ul5bCbBKnbCvdOWbLqTHhJiYV414CURZJba6L8qA=
|
github.com/muesli/termenv v0.7.4/go.mod h1:pZ7qY9l3F7e5xsAOS0zCew2tME+p7bWeBkotCEcIIcc=
|
||||||
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 h1:vEg9joUBmeBcK9iSJftGNf3coIG4HqZElCPehJsfAYM=
|
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee h1:4yd7jl+vXjalO5ztz6Vc1VADv+S/80LGJmyl1ROJ2AI=
|
||||||
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 h1:DZhuSZLsGlFL4CmhA8BcRA0mnthyA/nZ00AqCUo7vHg=
|
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 h1:pLI5jrR7OSLijeIDcmRxNmw2api+jEfxLoykJVice/E=
|
||||||
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4 h1:5/PjkGUjvEU5Gl6BxmvKRPpqo2uNMv4rcHBMwzk/st8=
|
golang.org/x/sys v0.0.0-20200916030750-2334cc1a136f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 h1:bNEHhJCnrwMKNMmOx3yAynp5vs5/gRy+XWFtZFu7NBM=
|
||||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
|
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a h1:e3IU37lwO4aq3uoRKINC7JikojFmE5gO7xhfxs8VC34=
|
||||||
|
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
@@ -13,7 +13,7 @@ import (
|
|||||||
// Type specifies the way we render pagination.
|
// Type specifies the way we render pagination.
|
||||||
type Type int
|
type Type int
|
||||||
|
|
||||||
// Pagination rendering options
|
// Pagination rendering options.
|
||||||
const (
|
const (
|
||||||
Arabic Type = iota
|
Arabic Type = iota
|
||||||
Dots
|
Dots
|
||||||
@@ -164,11 +164,7 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// View renders the pagination to a string.
|
// View renders the pagination to a string.
|
||||||
func View(model tea.Model) string {
|
func View(m Model) string {
|
||||||
m, ok := model.(Model)
|
|
||||||
if !ok {
|
|
||||||
return "could not perform assertion on model"
|
|
||||||
}
|
|
||||||
switch m.Type {
|
switch m.Type {
|
||||||
case Dots:
|
case Dots:
|
||||||
return dotsView(m)
|
return dotsView(m)
|
||||||
|
@@ -29,7 +29,7 @@ type Model struct {
|
|||||||
// Type is the set of frames to use. See Spinner.
|
// Type is the set of frames to use. See Spinner.
|
||||||
Frames Spinner
|
Frames Spinner
|
||||||
|
|
||||||
// FPS is the speed at which the ticker should tick
|
// FPS is the speed at which the ticker should tick.
|
||||||
FPS time.Duration
|
FPS time.Duration
|
||||||
|
|
||||||
// ForegroundColor sets the background color of the spinner. It can be a
|
// ForegroundColor sets the background color of the spinner. It can be a
|
||||||
@@ -44,7 +44,76 @@ type Model struct {
|
|||||||
// (per github.com/muesli/termenv).
|
// (per github.com/muesli/termenv).
|
||||||
BackgroundColor string
|
BackgroundColor string
|
||||||
|
|
||||||
frame int
|
// MinimumLifetime is the minimum amount of time the spinner can run. Any
|
||||||
|
// logic around this can be implemented in view that implements this
|
||||||
|
// spinner. If HideFor is set MinimumLifetime will be added on top of
|
||||||
|
// HideFor. In other words, if HideFor is 100ms and MinimumLifetime is
|
||||||
|
// 200ms then MinimumLifetime will expire after 300ms.
|
||||||
|
//
|
||||||
|
// MinimumLifetime is optional.
|
||||||
|
//
|
||||||
|
// This is considered experimental and may not appear in future versions of
|
||||||
|
// this library.
|
||||||
|
MinimumLifetime time.Duration
|
||||||
|
|
||||||
|
// HideFor can be used to wait to show the spinner until a certain amount
|
||||||
|
// of time has passed. This can be useful for preventing flicking when load
|
||||||
|
// times are very fast.
|
||||||
|
// Optional.
|
||||||
|
//
|
||||||
|
// This is considered experimental and may not appear in future versions of
|
||||||
|
// this library.
|
||||||
|
HideFor time.Duration
|
||||||
|
|
||||||
|
frame int
|
||||||
|
startTime time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start resets resets the spinner start time. For use with MinimumLifetime and
|
||||||
|
// MinimumStartTime. Optional.
|
||||||
|
//
|
||||||
|
// This is considered experimental and may not appear in future versions of
|
||||||
|
// this library.
|
||||||
|
func (m *Model) Start() {
|
||||||
|
m.startTime = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
// hidden returns whether or not Model.HideFor is in effect.
|
||||||
|
func (m Model) hidden() bool {
|
||||||
|
if m.startTime.IsZero() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if m.HideFor == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return m.startTime.Add(m.HideFor).After(time.Now())
|
||||||
|
}
|
||||||
|
|
||||||
|
// finished returns whether Model.MinimumLifetimeReached has been met.
|
||||||
|
func (m Model) finished() bool {
|
||||||
|
if m.startTime.IsZero() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if m.MinimumLifetime == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return m.startTime.Add(m.HideFor).Add(m.MinimumLifetime).Before(time.Now())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Visible returns whether or not the view should be rendered. Works in
|
||||||
|
// conjunction with Model.HideFor and Model.MinimumLifetimeReached. You should
|
||||||
|
// use this message directly to determine whether or not to render this view in
|
||||||
|
// the parent view and whether to continue sending spin messaging in the
|
||||||
|
// parent update function.
|
||||||
|
//
|
||||||
|
// Also note that using this function is optional and generally considered for
|
||||||
|
// advanced use only. Most of the time your application logic will determine
|
||||||
|
// whether or not this view should be used.
|
||||||
|
//
|
||||||
|
// This is considered experimental and may not appear in future versions of
|
||||||
|
// this library.
|
||||||
|
func (m Model) Visible() bool {
|
||||||
|
return !m.hidden() && !m.finished()
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewModel returns a model with default values.
|
// NewModel returns a model with default values.
|
||||||
@@ -56,7 +125,9 @@ func NewModel() Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TickMsg indicates that the timer has ticked and we should render a frame.
|
// TickMsg indicates that the timer has ticked and we should render a frame.
|
||||||
type TickMsg struct{}
|
type TickMsg struct {
|
||||||
|
Time time.Time
|
||||||
|
}
|
||||||
|
|
||||||
// Update is the Tea update function. This will advance the spinner one frame
|
// Update is the Tea update function. This will advance the spinner one frame
|
||||||
// every time it's called, regardless the message passed, so be sure the logic
|
// every time it's called, regardless the message passed, so be sure the logic
|
||||||
@@ -93,7 +164,9 @@ func View(model Model) string {
|
|||||||
|
|
||||||
// Tick is the command used to advance the spinner one frame.
|
// Tick is the command used to advance the spinner one frame.
|
||||||
func Tick(m Model) tea.Cmd {
|
func Tick(m Model) tea.Cmd {
|
||||||
return tea.Tick(m.FPS, func(time.Time) tea.Msg {
|
return tea.Tick(m.FPS, func(t time.Time) tea.Msg {
|
||||||
return TickMsg{}
|
return TickMsg{
|
||||||
|
Time: t,
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -11,32 +11,49 @@ import (
|
|||||||
"github.com/muesli/termenv"
|
"github.com/muesli/termenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultBlinkSpeed = time.Millisecond * 600
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultBlinkSpeed = time.Millisecond * 600
|
// EchoNormal displays text as is. This is the default behavior.
|
||||||
|
EchoNormal EchoMode = iota
|
||||||
|
|
||||||
|
// EchoPassword displays the EchoCharacter mask instead of actual
|
||||||
|
// characters. This is commonly used for password fields.
|
||||||
|
EchoPassword
|
||||||
|
|
||||||
|
// EchoNone displays nothing as characters are entered. This is commonly
|
||||||
|
// seen for password fields on the command line.
|
||||||
|
EchoNone
|
||||||
|
|
||||||
|
// EchoOnEdit
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// EchoMode sets the input behavior of the text input field.
|
||||||
|
type EchoMode int
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// color is a helper for returning colors
|
// color is a helper for returning colors.
|
||||||
color func(s string) termenv.Color = termenv.ColorProfile().Color
|
color func(s string) termenv.Color = termenv.ColorProfile().Color
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrMsg indicates there's been an error. We don't handle errors in the this
|
// Model is the Bubble Tea model for this text input element.
|
||||||
// package; we're expecting errors to be handle in the program that implements
|
|
||||||
// this text input.
|
|
||||||
type ErrMsg error
|
|
||||||
|
|
||||||
// Model is the Tea model for this text input element.
|
|
||||||
type Model struct {
|
type Model struct {
|
||||||
Err error
|
Err error
|
||||||
Prompt string
|
|
||||||
Cursor string
|
Prompt string
|
||||||
BlinkSpeed time.Duration
|
Placeholder string
|
||||||
Placeholder string
|
|
||||||
|
Cursor string
|
||||||
|
BlinkSpeed time.Duration
|
||||||
|
|
||||||
TextColor string
|
TextColor string
|
||||||
BackgroundColor string
|
BackgroundColor string
|
||||||
PlaceholderColor string
|
PlaceholderColor string
|
||||||
CursorColor string
|
CursorColor string
|
||||||
|
|
||||||
|
EchoMode EchoMode
|
||||||
|
EchoCharacter rune
|
||||||
|
|
||||||
// CharLimit is the maximum amount of characters this input element will
|
// CharLimit is the maximum amount of characters this input element will
|
||||||
// accept. If 0 or less, there's no limit.
|
// accept. If 0 or less, there's no limit.
|
||||||
CharLimit int
|
CharLimit int
|
||||||
@@ -46,21 +63,21 @@ type Model struct {
|
|||||||
// viewport. If 0 or less this setting is ignored.
|
// viewport. If 0 or less this setting is ignored.
|
||||||
Width int
|
Width int
|
||||||
|
|
||||||
// Underlying text value
|
// Underlying text value.
|
||||||
value []rune
|
value []rune
|
||||||
|
|
||||||
// Focus indicates whether user input focus should be on this input
|
// Focus indicates whether user input focus should be on this input
|
||||||
// component. When false, don't blink and ignore keyboard input.
|
// component. When false, don't blink and ignore keyboard input.
|
||||||
focus bool
|
focus bool
|
||||||
|
|
||||||
// Cursor blink state
|
// Cursor blink state.
|
||||||
blink bool
|
blink bool
|
||||||
|
|
||||||
// Cursor position
|
// Cursor position.
|
||||||
pos int
|
pos int
|
||||||
|
|
||||||
// Used to emulate a viewport when width is set and the content is
|
// Used to emulate a viewport when width is set and the content is
|
||||||
// overflowing
|
// overflowing.
|
||||||
offset int
|
offset int
|
||||||
offsetRight int
|
offsetRight int
|
||||||
}
|
}
|
||||||
@@ -84,8 +101,8 @@ func (m Model) Value() string {
|
|||||||
return string(m.value)
|
return string(m.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cursor start moves the cursor to the given position. If the position is out
|
// SetCursor start moves the cursor to the given position. If the position is
|
||||||
// of bounds the cursor will be moved to the start or end accordingly.
|
// out of bounds the cursor will be moved to the start or end accordingly.
|
||||||
func (m *Model) SetCursor(pos int) {
|
func (m *Model) SetCursor(pos int) {
|
||||||
m.pos = clamp(pos, 0, len(m.value))
|
m.pos = clamp(pos, 0, len(m.value))
|
||||||
m.handleOverflow()
|
m.handleOverflow()
|
||||||
@@ -181,7 +198,6 @@ func (m *Model) handleOverflow() {
|
|||||||
m.offsetRight = min(m.offsetRight, len(m.value))
|
m.offsetRight = min(m.offsetRight, len(m.value))
|
||||||
|
|
||||||
if m.pos < m.offset {
|
if m.pos < m.offset {
|
||||||
|
|
||||||
m.offset = m.pos
|
m.offset = m.pos
|
||||||
|
|
||||||
w := 0
|
w := 0
|
||||||
@@ -196,9 +212,7 @@ func (m *Model) handleOverflow() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
m.offsetRight = m.offset + i
|
m.offsetRight = m.offset + i
|
||||||
|
|
||||||
} else if m.pos >= m.offsetRight {
|
} else if m.pos >= m.offsetRight {
|
||||||
|
|
||||||
m.offsetRight = m.pos
|
m.offsetRight = m.pos
|
||||||
|
|
||||||
w := 0
|
w := 0
|
||||||
@@ -213,7 +227,6 @@ func (m *Model) handleOverflow() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
m.offset = m.offsetRight - (len(runes) - 1 - i)
|
m.offset = m.offsetRight - (len(runes) - 1 - i)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,7 +284,7 @@ func (m *Model) wordRight() {
|
|||||||
i := m.pos
|
i := m.pos
|
||||||
|
|
||||||
for i < len(m.value) {
|
for i < len(m.value) {
|
||||||
if unicode.IsSpace(rune(m.value[i])) {
|
if unicode.IsSpace(m.value[i]) {
|
||||||
m.pos++
|
m.pos++
|
||||||
i++
|
i++
|
||||||
} else {
|
} else {
|
||||||
@@ -280,7 +293,7 @@ func (m *Model) wordRight() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i < len(m.value) {
|
for i < len(m.value) {
|
||||||
if !unicode.IsSpace(rune(m.value[i])) {
|
if !unicode.IsSpace(m.value[i]) {
|
||||||
m.pos++
|
m.pos++
|
||||||
i++
|
i++
|
||||||
} else {
|
} else {
|
||||||
@@ -289,19 +302,36 @@ func (m *Model) wordRight() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m Model) echoTransform(v string) string {
|
||||||
|
switch m.EchoMode {
|
||||||
|
case EchoPassword:
|
||||||
|
return strings.Repeat(string(m.EchoCharacter), rw.StringWidth(v))
|
||||||
|
case EchoNone:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
default:
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// BlinkMsg is sent when the cursor should alternate it's blinking state.
|
// BlinkMsg is sent when the cursor should alternate it's blinking state.
|
||||||
type BlinkMsg struct{}
|
type BlinkMsg struct{}
|
||||||
|
|
||||||
// NewModel creates a new model with default settings.
|
// NewModel creates a new model with default settings.
|
||||||
func NewModel() Model {
|
func NewModel() Model {
|
||||||
return Model{
|
return Model{
|
||||||
Prompt: "> ",
|
Prompt: "> ",
|
||||||
BlinkSpeed: defaultBlinkSpeed,
|
Placeholder: "",
|
||||||
Placeholder: "",
|
|
||||||
|
BlinkSpeed: defaultBlinkSpeed,
|
||||||
|
|
||||||
TextColor: "",
|
TextColor: "",
|
||||||
PlaceholderColor: "240",
|
PlaceholderColor: "240",
|
||||||
CursorColor: "",
|
CursorColor: "",
|
||||||
CharLimit: 0,
|
|
||||||
|
EchoCharacter: '*',
|
||||||
|
|
||||||
|
CharLimit: 0,
|
||||||
|
|
||||||
value: nil,
|
value: nil,
|
||||||
focus: false,
|
focus: false,
|
||||||
@@ -320,9 +350,7 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.Type {
|
switch msg.Type {
|
||||||
case tea.KeyBackspace:
|
case tea.KeyBackspace: // delete character before cursor
|
||||||
fallthrough
|
|
||||||
case tea.KeyDelete:
|
|
||||||
if len(m.value) > 0 {
|
if len(m.value) > 0 {
|
||||||
m.value = append(m.value[:max(0, m.pos-1)], m.value[m.pos:]...)
|
m.value = append(m.value[:max(0, m.pos-1)], m.value[m.pos:]...)
|
||||||
if m.pos > 0 {
|
if m.pos > 0 {
|
||||||
@@ -349,13 +377,13 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
fallthrough
|
fallthrough
|
||||||
case tea.KeyCtrlB: // ^B, back one charcter
|
case tea.KeyCtrlB: // ^B, back one charcter
|
||||||
fallthrough
|
fallthrough
|
||||||
case tea.KeyCtrlA: // ^A, go to beginning
|
case tea.KeyHome, tea.KeyCtrlA: // ^A, go to beginning
|
||||||
m.CursorStart()
|
m.CursorStart()
|
||||||
case tea.KeyCtrlD: // ^D, delete char under cursor
|
case tea.KeyDelete, tea.KeyCtrlD: // ^D, delete char under cursor
|
||||||
if len(m.value) > 0 && m.pos < len(m.value) {
|
if len(m.value) > 0 && m.pos < len(m.value) {
|
||||||
m.value = append(m.value[:m.pos], m.value[m.pos+1:]...)
|
m.value = append(m.value[:m.pos], m.value[m.pos+1:]...)
|
||||||
}
|
}
|
||||||
case tea.KeyCtrlE: // ^E, go to end
|
case tea.KeyCtrlE, tea.KeyEnd: // ^E, go to end
|
||||||
m.CursorEnd()
|
m.CursorEnd()
|
||||||
case tea.KeyCtrlK: // ^K, kill text after cursor
|
case tea.KeyCtrlK: // ^K, kill text after cursor
|
||||||
m.value = m.value[:m.pos]
|
m.value = m.value[:m.pos]
|
||||||
@@ -386,9 +414,6 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case ErrMsg:
|
|
||||||
m.Err = msg
|
|
||||||
|
|
||||||
case BlinkMsg:
|
case BlinkMsg:
|
||||||
m.blink = !m.blink
|
m.blink = !m.blink
|
||||||
return m, Blink(m)
|
return m, Blink(m)
|
||||||
@@ -400,12 +425,7 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// View renders the textinput in its current state.
|
// View renders the textinput in its current state.
|
||||||
func View(model tea.Model) string {
|
func View(m Model) string {
|
||||||
m, ok := model.(Model)
|
|
||||||
if !ok {
|
|
||||||
return "could not perform assertion on model"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Placeholder text
|
// Placeholder text
|
||||||
if len(m.value) == 0 && m.Placeholder != "" {
|
if len(m.value) == 0 && m.Placeholder != "" {
|
||||||
return placeholderView(m)
|
return placeholderView(m)
|
||||||
@@ -413,12 +433,11 @@ func View(model tea.Model) string {
|
|||||||
|
|
||||||
value := m.value[m.offset:m.offsetRight]
|
value := m.value[m.offset:m.offsetRight]
|
||||||
pos := max(0, m.pos-m.offset)
|
pos := max(0, m.pos-m.offset)
|
||||||
|
v := m.colorText(m.echoTransform(string(value[:pos])))
|
||||||
v := m.colorText(string(value[:pos]))
|
|
||||||
|
|
||||||
if pos < len(value) {
|
if pos < len(value) {
|
||||||
v += cursorView(string(value[pos]), m) // cursor and text under it
|
v += cursorView(m.echoTransform(string(value[pos])), m) // cursor and text under it
|
||||||
v += m.colorText(string(value[pos+1:])) // text after cursor
|
v += m.colorText(m.echoTransform(string(value[pos+1:]))) // text after cursor
|
||||||
} else {
|
} else {
|
||||||
v += cursorView(" ", m)
|
v += cursorView(" ", m)
|
||||||
}
|
}
|
||||||
@@ -440,7 +459,7 @@ func View(model tea.Model) string {
|
|||||||
return m.Prompt + v
|
return m.Prompt + v
|
||||||
}
|
}
|
||||||
|
|
||||||
// placeholderView
|
// placeholderView.
|
||||||
func placeholderView(m Model) string {
|
func placeholderView(m Model) string {
|
||||||
var (
|
var (
|
||||||
v string
|
v string
|
||||||
@@ -449,10 +468,7 @@ func placeholderView(m Model) string {
|
|||||||
|
|
||||||
// Cursor
|
// Cursor
|
||||||
if m.blink && m.PlaceholderColor != "" {
|
if m.blink && m.PlaceholderColor != "" {
|
||||||
v += cursorView(
|
v += cursorView(m.colorPlaceholder(p[:1]), m)
|
||||||
m.colorPlaceholder(p[:1]),
|
|
||||||
m,
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
v += cursorView(p[:1], m)
|
v += cursorView(p[:1], m)
|
||||||
}
|
}
|
||||||
|
@@ -37,21 +37,6 @@ type Model struct {
|
|||||||
lines []string
|
lines []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: do we really need this?
|
|
||||||
func NewModel(width, height int) Model {
|
|
||||||
return Model{
|
|
||||||
Width: width,
|
|
||||||
Height: height,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: do we really need this?
|
|
||||||
func (m Model) SetSize(yPos, width, height int) {
|
|
||||||
m.YPosition = yPos
|
|
||||||
m.Width = width
|
|
||||||
m.Height = height
|
|
||||||
}
|
|
||||||
|
|
||||||
// AtTop returns whether or not the viewport is in the very top position.
|
// AtTop returns whether or not the viewport is in the very top position.
|
||||||
func (m Model) AtTop() bool {
|
func (m Model) AtTop() bool {
|
||||||
return m.YOffset <= 0
|
return m.YOffset <= 0
|
||||||
@@ -88,7 +73,7 @@ func (m *Model) SetContent(s string) {
|
|||||||
m.lines = strings.Split(s, "\n")
|
m.lines = strings.Split(s, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the lines that should currently be visible in the viewport
|
// Return the lines that should currently be visible in the viewport.
|
||||||
func (m Model) visibleLines() (lines []string) {
|
func (m Model) visibleLines() (lines []string) {
|
||||||
if len(m.lines) > 0 {
|
if len(m.lines) > 0 {
|
||||||
top := max(0, m.YOffset)
|
top := max(0, m.YOffset)
|
||||||
@@ -302,7 +287,6 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
var cmd tea.Cmd
|
var cmd tea.Cmd
|
||||||
|
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
|
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
// Down one page
|
// Down one page
|
||||||
@@ -320,14 +304,14 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Down half page
|
// Down half page
|
||||||
case "d":
|
case "d", "ctrl+d":
|
||||||
lines := m.HalfViewDown()
|
lines := m.HalfViewDown()
|
||||||
if m.HighPerformanceRendering {
|
if m.HighPerformanceRendering {
|
||||||
cmd = ViewDown(m, lines)
|
cmd = ViewDown(m, lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Up half page
|
// Up half page
|
||||||
case "u":
|
case "u", "ctrl+u":
|
||||||
lines := m.HalfViewUp()
|
lines := m.HalfViewUp()
|
||||||
if m.HighPerformanceRendering {
|
if m.HighPerformanceRendering {
|
||||||
cmd = ViewUp(m, lines)
|
cmd = ViewUp(m, lines)
|
||||||
@@ -349,8 +333,7 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case tea.MouseMsg:
|
case tea.MouseMsg:
|
||||||
switch msg.Button {
|
switch msg.Type {
|
||||||
|
|
||||||
case tea.MouseWheelUp:
|
case tea.MouseWheelUp:
|
||||||
lines := m.LineUp(3)
|
lines := m.LineUp(3)
|
||||||
if m.HighPerformanceRendering {
|
if m.HighPerformanceRendering {
|
||||||
@@ -372,7 +355,6 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
|
|
||||||
// View renders the viewport into a string.
|
// View renders the viewport into a string.
|
||||||
func View(m Model) string {
|
func View(m Model) string {
|
||||||
|
|
||||||
if m.HighPerformanceRendering {
|
if m.HighPerformanceRendering {
|
||||||
// Just send newlines since we're doing to be rendering the actual
|
// Just send newlines since we're doing to be rendering the actual
|
||||||
// content seprately. We still need send something that equals the
|
// content seprately. We still need send something that equals the
|
||||||
@@ -407,7 +389,3 @@ func max(a, b int) int {
|
|||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func clamp(val, low, high int) int {
|
|
||||||
return max(low, min(high, val))
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user