mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-10-18 16:38:56 +03:00
Compare commits
152 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
d06aa4aa48 | ||
|
10962af2f2 | ||
|
5082ae6f31 | ||
|
4365990396 | ||
|
1405b09ec8 | ||
|
f53c742d6c | ||
|
9449cc7e41 | ||
|
58a177394e | ||
|
f016c31d83 | ||
|
74436326b9 | ||
|
158097df66 | ||
|
0d5d7e5acd | ||
|
ddd48d9ab7 | ||
|
7242bbe8dc | ||
|
9e324205c2 | ||
|
b2f42066a2 | ||
|
a0d7cb77a1 | ||
|
046b9ca129 | ||
|
da9a4049de | ||
|
490a599c05 | ||
|
f719cc8cb1 | ||
|
c8f4855d20 | ||
|
43d43d14ae | ||
|
de359c53bb | ||
|
c7d69b61f0 | ||
|
11f56f9b6b | ||
|
3be2d0585b | ||
|
37a85afcd1 | ||
|
f9c79eef64 | ||
|
c303de1e85 | ||
|
7e5ef42924 | ||
|
79ca4d09d1 | ||
|
f48e53556a | ||
|
5d6d8cb0fb | ||
|
47b8d9c6a8 | ||
|
b78277e7ec | ||
|
6a768905a6 | ||
|
0f06d78b92 | ||
|
1e16eca939 | ||
|
d81e5713d4 | ||
|
b10dbcb4dd | ||
|
a2e0a2e72e | ||
|
3dea7d036e | ||
|
68e87e08c5 | ||
|
65a17d039e | ||
|
38d59517fb | ||
|
06109f45ce | ||
|
9cb8e8d90a | ||
|
e6219572e5 | ||
|
81feaacf5b | ||
|
9a25d8b8b9 | ||
|
e8e052c64b | ||
|
3d7cd43046 | ||
|
9c38e101d2 | ||
|
84f7b047bb | ||
|
9e1e435bba | ||
|
0fd072ddcc | ||
|
a07ab1d6af | ||
|
8e49ee609e | ||
|
d02641f6b5 | ||
|
9b47f26bdd | ||
|
9c780011ff | ||
|
8148e61443 | ||
|
ce7d8da084 | ||
|
d14fdf585c | ||
|
bf7719e6c1 | ||
|
1b530b293c | ||
|
7d1c04164e | ||
|
97020cd0d2 | ||
|
5dbcf95877 | ||
|
f58fead10d | ||
|
703de11da4 | ||
|
03461d6804 | ||
|
4445acbace | ||
|
bd161e8ded | ||
|
d9716a97f6 | ||
|
a0fe547fdb | ||
|
1cb36774ed | ||
|
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 | ||
|
f11ca377f4 | ||
|
10022c964c | ||
|
5357dd61bd | ||
|
5a26cb0d8e | ||
|
88469a499e | ||
|
b130d96434 | ||
|
185a19e56f | ||
|
dbb1d93970 | ||
|
2f909886c1 | ||
|
5720cfb35a | ||
|
3c07b97d37 | ||
|
9b3e5476c3 | ||
|
74e84eca55 | ||
|
ccf4c90b6b | ||
|
f341e3c896 | ||
|
c9196e5407 | ||
|
f967f6a87f | ||
|
35c3cd626d | ||
|
2525319d72 | ||
|
e9dd6b06e0 | ||
|
7cef3a6f59 | ||
|
1cdc2045c7 | ||
|
cc480dd2f3 | ||
|
0c1781fbb3 | ||
|
5255143e87 | ||
|
1629afe087 | ||
|
da3150ded7 | ||
|
b82cf5071d | ||
|
d095a6554c | ||
|
34ac608122 | ||
|
09ae5da7c3 | ||
|
5572542e2e | ||
|
3688351ddf | ||
|
d9c03fc0b0 | ||
|
3321ac12a9 | ||
|
0eaea5cc5d | ||
|
0243dff9d3 | ||
|
68ec6c7ffc | ||
|
f332bf2cc2 | ||
|
0b19d41e0a | ||
|
786ec557d4 | ||
|
d3192a3e70 | ||
|
74cc86fce5 |
28
.github/workflows/build.yml
vendored
Normal file
28
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
name: build
|
||||||
|
on: [push, pull_request]
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: [~1.13, ^1]
|
||||||
|
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
env:
|
||||||
|
GO111MODULE: "on"
|
||||||
|
steps:
|
||||||
|
- name: Install Go
|
||||||
|
uses: actions/setup-go@v2
|
||||||
|
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 ./...
|
28
.github/workflows/coverage.yml
vendored
Normal file
28
.github/workflows/coverage.yml
vendored
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
name: coverage
|
||||||
|
on: [push, pull_request]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
coverage:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: [^1]
|
||||||
|
os: [ubuntu-latest]
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
env:
|
||||||
|
GO111MODULE: "on"
|
||||||
|
steps:
|
||||||
|
- name: Install Go
|
||||||
|
uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: ${{ matrix.go-version }}
|
||||||
|
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- 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
|
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
|
27
.golangci.yml
Normal file
27
.golangci.yml
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
run:
|
||||||
|
tests: false
|
||||||
|
|
||||||
|
issues:
|
||||||
|
max-issues-per-linter: 0
|
||||||
|
max-same-issues: 0
|
||||||
|
|
||||||
|
linters:
|
||||||
|
enable:
|
||||||
|
- bodyclose
|
||||||
|
- dupl
|
||||||
|
- exportloopref
|
||||||
|
- goconst
|
||||||
|
- godot
|
||||||
|
- godox
|
||||||
|
- goimports
|
||||||
|
- golint
|
||||||
|
- gomnd
|
||||||
|
- goprintffuncname
|
||||||
|
- gosec
|
||||||
|
- misspell
|
||||||
|
- prealloc
|
||||||
|
- rowserrcheck
|
||||||
|
- sqlclosecheck
|
||||||
|
- unconvert
|
||||||
|
- unparam
|
||||||
|
- whitespace
|
91
README.md
91
README.md
@@ -1,9 +1,94 @@
|
|||||||
# Tea Party
|
Bubbles
|
||||||
|
=======
|
||||||
|
|
||||||
Components for [Tea](https://github.com/charmbraclet/tea)
|
<p>
|
||||||
|
<img src="https://stuff.charm.sh/bubbles/bubbles-github.png" width="233" alt="The Bubbles Logo">
|
||||||
|
</p>
|
||||||
|
|
||||||
|
[](https://github.com/charmbracelet/bubbles/releases)
|
||||||
|
[](https://pkg.go.dev/github.com/charmbracelet/bubbles)
|
||||||
|
[](https://github.com/charmbracelet/bubbles/actions)
|
||||||
|
[](https://goreportcard.com/report/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
|
||||||
|
|
||||||
|
<img src="https://stuff.charm.sh/bubbles-examples/spinner.gif" width="400" alt="Spinner Example">
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
<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/textinputs/main.go)
|
||||||
|
|
||||||
|
|
||||||
|
## Progress
|
||||||
|
|
||||||
|
<img src="https://stuff.charm.sh/bubbles-examples/progress.gif" width="800" alt="Progressbar Example">
|
||||||
|
|
||||||
|
A simple, customizable progress meter. Supports solid and gradient fills. The
|
||||||
|
empty and filled runes can be set to whatever you'd like. The percentage readout
|
||||||
|
is customizable and can also be omitted entirely.
|
||||||
|
|
||||||
|
* [Example code](https://github.com/charmbracelet/bubbletea/blob/master/examples/progress/main.go)
|
||||||
|
|
||||||
|
|
||||||
|
## 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.
|
||||||
|
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
|
||||||
|
|
||||||
|
<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
|
||||||
|
for applications which make use of the alternate screen buffer.
|
||||||
|
|
||||||
|
* [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
|
||||||
|
|
||||||
⚠️ This project is a pre-release! Check back later.
|
|
||||||
|
|
||||||
## 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-unrounded.jpg" width="400"></a>
|
||||||
|
|
||||||
|
Charm热爱开源 • Charm loves open source
|
||||||
|
12
go.mod
12
go.mod
@@ -3,8 +3,12 @@ module github.com/charmbracelet/bubbles
|
|||||||
go 1.13
|
go 1.13
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/charmbracelet/bubbletea v0.8.0
|
github.com/atotto/clipboard v0.1.2
|
||||||
github.com/muesli/termenv v0.5.3-0.20200526053627-d728968dcf83
|
github.com/charmbracelet/bubbletea v0.13.1
|
||||||
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 // indirect
|
github.com/charmbracelet/lipgloss v0.1.2
|
||||||
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
|
github.com/lucasb-eyer/go-colorful v1.2.0
|
||||||
|
github.com/mattn/go-runewidth v0.0.12
|
||||||
|
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68
|
||||||
|
github.com/muesli/termenv v0.8.1
|
||||||
|
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
|
||||||
)
|
)
|
||||||
|
47
go.sum
47
go.sum
@@ -1,26 +1,41 @@
|
|||||||
github.com/charmbracelet/bubbletea v0.8.0 h1:ruZFaFF+2kgCI1IwNG40KTYDW5ZvE2+hPy4odlBdUko=
|
github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY=
|
||||||
github.com/charmbracelet/bubbletea v0.8.0/go.mod h1:DzNhKkQQJI30eb+kBcaOs1+z86zTSqcMgSHoFY+uCsg=
|
github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
||||||
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4=
|
github.com/charmbracelet/bubbletea v0.13.1 h1:huvX8mPaeMZ8DLulT50iEWRF+iitY5FNEDqDVLu69nM=
|
||||||
|
github.com/charmbracelet/bubbletea v0.13.1/go.mod h1:tp9tr9Dadh0PLhgiwchE5zZJXm5543JYjHG9oY+5qSg=
|
||||||
|
github.com/charmbracelet/lipgloss v0.1.2 h1:D+LUMg34W7n2pkuMrevKVxT7HXqnoRHm7IoomkX3/ZU=
|
||||||
|
github.com/charmbracelet/lipgloss v0.1.2/go.mod h1:5D8zradw52m7QmxRF6QgwbwJi9je84g8MkWiGN07uKg=
|
||||||
|
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/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/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||||
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/muesli/termenv v0.5.3-0.20200526053627-d728968dcf83 h1:AfshZBlqAwhCZ27NJ1aPlMcPBihF1squ1GpaollhLQk=
|
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||||
github.com/muesli/termenv v0.5.3-0.20200526053627-d728968dcf83/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=
|
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||||
github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03 h1:pd4YKIqCB0U7O2I4gWHgEUA2mCEOENmco0l/bM957bU=
|
github.com/mattn/go-runewidth v0.0.12 h1:Y41i/hVW3Pgwr8gV+J23B9YEY0zxjptBuCWEaxmAOow=
|
||||||
github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03/go.mod h1:Z9+Ul5bCbBKnbCvdOWbLqTHhJiYV414CURZJba6L8qA=
|
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||||
|
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68 h1:y1p/ycavWjGT9FnmSjdbWUlLGvcxrY0Rw3ATltrxOhk=
|
||||||
|
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
|
||||||
|
github.com/muesli/termenv v0.7.2/go.mod h1:ct2L5N2lmix82RaY3bMWwVu/jUFc9Ule0KGDCiKYPh8=
|
||||||
|
github.com/muesli/termenv v0.8.1 h1:9q230czSP3DHVpkaPDXGp0TOfAwyjyYwXlUCQxQSaBk=
|
||||||
|
github.com/muesli/termenv v0.8.1/go.mod h1:kzt/D/4a88RoheZmwfqorY3A+tnsSMA9HJC/fQSFKo0=
|
||||||
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
|
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||||
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
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-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw=
|
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 h1:pLI5jrR7OSLijeIDcmRxNmw2api+jEfxLoykJVice/E=
|
||||||
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 h1:vEg9joUBmeBcK9iSJftGNf3coIG4HqZElCPehJsfAYM=
|
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/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-20200523222454-059865788121 h1:rITEj+UZHYC927n8GT97eC3zrpzXdb/voyeOuVKS46o=
|
golang.org/x/sys v0.0.0-20200916030750-2334cc1a136f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y=
|
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=
|
||||||
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/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=
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
// package paginator provides a Bubble Tea package for calulating pagination
|
// Package paginator provides a Bubble Tea package for calulating pagination
|
||||||
// and rendering pagination info. Note that this package does not render actual
|
// and rendering pagination info. Note that this package does not render actual
|
||||||
// pages: it's purely for handling keystrokes related to pagination, and
|
// pages: it's purely for handling keystrokes related to pagination, and
|
||||||
// rendering pagination status.
|
// rendering pagination status.
|
||||||
@@ -13,13 +13,13 @@ 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
|
||||||
)
|
)
|
||||||
|
|
||||||
// Model is the Tea model for this user interface.
|
// Model is the Bubble Tea model for this user interface.
|
||||||
type Model struct {
|
type Model struct {
|
||||||
Type Type
|
Type Type
|
||||||
Page int
|
Page int
|
||||||
@@ -28,6 +28,7 @@ type Model struct {
|
|||||||
ActiveDot string
|
ActiveDot string
|
||||||
InactiveDot string
|
InactiveDot string
|
||||||
ArabicFormat string
|
ArabicFormat string
|
||||||
|
UsePgUpPgDownKeys bool
|
||||||
UseLeftRightKeys bool
|
UseLeftRightKeys bool
|
||||||
UseUpDownKeys bool
|
UseUpDownKeys bool
|
||||||
UseHLKeys bool
|
UseHLKeys bool
|
||||||
@@ -39,8 +40,8 @@ type Model struct {
|
|||||||
// used for other things beyond navigating sets. Note that it both returns the
|
// used for other things beyond navigating sets. Note that it both returns the
|
||||||
// number of total pages and alters the model.
|
// number of total pages and alters the model.
|
||||||
func (m *Model) SetTotalPages(items int) int {
|
func (m *Model) SetTotalPages(items int) int {
|
||||||
if items == 0 {
|
if items < 1 {
|
||||||
return 0
|
return m.TotalPages
|
||||||
}
|
}
|
||||||
n := items / m.PerPage
|
n := items / m.PerPage
|
||||||
if items%m.PerPage > 0 {
|
if items%m.PerPage > 0 {
|
||||||
@@ -90,7 +91,7 @@ func (m *Model) NextPage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// LastPage returns whether or not we're on the last page.
|
// OnLastPage returns whether or not we're on the last page.
|
||||||
func (m Model) OnLastPage() bool {
|
func (m Model) OnLastPage() bool {
|
||||||
return m.Page == m.TotalPages-1
|
return m.Page == m.TotalPages-1
|
||||||
}
|
}
|
||||||
@@ -105,6 +106,7 @@ func NewModel() Model {
|
|||||||
ActiveDot: "•",
|
ActiveDot: "•",
|
||||||
InactiveDot: "○",
|
InactiveDot: "○",
|
||||||
ArabicFormat: "%d/%d",
|
ArabicFormat: "%d/%d",
|
||||||
|
UsePgUpPgDownKeys: true,
|
||||||
UseLeftRightKeys: true,
|
UseLeftRightKeys: true,
|
||||||
UseUpDownKeys: false,
|
UseUpDownKeys: false,
|
||||||
UseHLKeys: true,
|
UseHLKeys: true,
|
||||||
@@ -113,9 +115,17 @@ func NewModel() Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update is the Tea update function which binds keystrokes to pagination.
|
// Update is the Tea update function which binds keystrokes to pagination.
|
||||||
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
|
if m.UsePgUpPgDownKeys {
|
||||||
|
switch msg.String() {
|
||||||
|
case "pgup":
|
||||||
|
m.PrevPage()
|
||||||
|
case "pgdown":
|
||||||
|
m.NextPage()
|
||||||
|
}
|
||||||
|
}
|
||||||
if m.UseLeftRightKeys {
|
if m.UseLeftRightKeys {
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
case "left":
|
case "left":
|
||||||
@@ -154,20 +164,16 @@ 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 (m Model) View() 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 m.dotsView()
|
||||||
default:
|
default:
|
||||||
return arabicView(m)
|
return m.arabicView()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func dotsView(m Model) string {
|
func (m Model) dotsView() string {
|
||||||
var s string
|
var s string
|
||||||
for i := 0; i < m.TotalPages; i++ {
|
for i := 0; i < m.TotalPages; i++ {
|
||||||
if i == m.Page {
|
if i == m.Page {
|
||||||
@@ -179,7 +185,7 @@ func dotsView(m Model) string {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func arabicView(m Model) string {
|
func (m Model) arabicView() string {
|
||||||
return fmt.Sprintf(m.ArabicFormat, m.Page+1, m.TotalPages)
|
return fmt.Sprintf(m.ArabicFormat, m.Page+1, m.TotalPages)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
192
progress/progress.go
Normal file
192
progress/progress.go
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
package progress
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/lucasb-eyer/go-colorful"
|
||||||
|
"github.com/muesli/reflow/ansi"
|
||||||
|
"github.com/muesli/termenv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const defaultWidth = 40
|
||||||
|
|
||||||
|
var color func(string) termenv.Color = termenv.ColorProfile().Color
|
||||||
|
|
||||||
|
// Option is used to set options in NewModel. For example:
|
||||||
|
//
|
||||||
|
// progress := NewModel(
|
||||||
|
// WithRamp("#ff0000", "#0000ff"),
|
||||||
|
// WithoutPercentage(),
|
||||||
|
// )
|
||||||
|
type Option func(*Model) error
|
||||||
|
|
||||||
|
// WithDefaultGradient sets a gradient fill with default colors.
|
||||||
|
func WithDefaultGradient() Option {
|
||||||
|
return WithGradient("#5A56E0", "#EE6FF8")
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithGradient sets a gradient fill blending between two colors.
|
||||||
|
func WithGradient(colorA, colorB string) Option {
|
||||||
|
return func(m *Model) error {
|
||||||
|
return m.setRamp(colorA, colorB, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithDefaultScaledGradient sets a gradient with default colors, and scales the
|
||||||
|
// gradient to fit the filled portion of the ramp.
|
||||||
|
func WithDefaultScaledGradient() Option {
|
||||||
|
return WithScaledGradient("#5A56E0", "#EE6FF8")
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithScaledGradient scales the gradient to fit the width of the filled portion of
|
||||||
|
// the progress bar.
|
||||||
|
func WithScaledGradient(colorA, colorB string) Option {
|
||||||
|
return func(m *Model) error {
|
||||||
|
return m.setRamp(colorA, colorB, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithSolidFill sets the progress to use a solid fill with the given color.
|
||||||
|
func WithSolidFill(color string) Option {
|
||||||
|
return func(m *Model) error {
|
||||||
|
m.FullColor = color
|
||||||
|
m.useRamp = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithoutPercentage hides the numeric percentage.
|
||||||
|
func WithoutPercentage() Option {
|
||||||
|
return func(m *Model) error {
|
||||||
|
m.ShowPercentage = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithWidth sets the initial width of the progress bar. Note that you can also
|
||||||
|
// set the width via the Width property, which can come in handy if you're
|
||||||
|
// waiting for a tea.WindowSizeMsg.
|
||||||
|
func WithWidth(w int) Option {
|
||||||
|
return func(m *Model) error {
|
||||||
|
m.Width = w
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Model stores values we'll use when rendering the progress bar.
|
||||||
|
type Model struct {
|
||||||
|
|
||||||
|
// Total width of the progress bar, including percentage, if set.
|
||||||
|
Width int
|
||||||
|
|
||||||
|
// "Filled" sections of the progress bar
|
||||||
|
Full rune
|
||||||
|
FullColor string
|
||||||
|
|
||||||
|
// "Empty" sections of progress bar
|
||||||
|
Empty rune
|
||||||
|
EmptyColor string
|
||||||
|
|
||||||
|
// Settings for rendering the numeric percentage
|
||||||
|
ShowPercentage bool
|
||||||
|
PercentFormat string // a fmt string for a float
|
||||||
|
PercentageStyle *termenv.Style
|
||||||
|
|
||||||
|
useRamp bool
|
||||||
|
rampColorA colorful.Color
|
||||||
|
rampColorB colorful.Color
|
||||||
|
|
||||||
|
// When true, we scale the gradient to fit the width of the filled section
|
||||||
|
// of the progress bar. When false, the width of the gradient will be set
|
||||||
|
// to the full width of the progress bar.
|
||||||
|
scaleRamp bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewModel returns a model with default values.
|
||||||
|
func NewModel(opts ...Option) (*Model, error) {
|
||||||
|
m := &Model{
|
||||||
|
Width: defaultWidth,
|
||||||
|
Full: '█',
|
||||||
|
FullColor: "#7571F9",
|
||||||
|
Empty: '░',
|
||||||
|
EmptyColor: "#606060",
|
||||||
|
ShowPercentage: true,
|
||||||
|
PercentFormat: " %3.0f%%",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
if err := opt(m); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// View renders the progress bar as a given percentage.
|
||||||
|
func (m Model) View(percent float64) string {
|
||||||
|
b := strings.Builder{}
|
||||||
|
if m.ShowPercentage {
|
||||||
|
percentage := fmt.Sprintf(m.PercentFormat, percent*100) //nolint:gomnd
|
||||||
|
if m.PercentageStyle != nil {
|
||||||
|
percentage = m.PercentageStyle.Styled(percentage)
|
||||||
|
}
|
||||||
|
m.bar(&b, percent, ansi.PrintableRuneWidth(percentage))
|
||||||
|
b.WriteString(percentage)
|
||||||
|
} else {
|
||||||
|
m.bar(&b, percent, 0)
|
||||||
|
}
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Model) bar(b *strings.Builder, percent float64, textWidth int) {
|
||||||
|
var (
|
||||||
|
tw = m.Width - textWidth // total width
|
||||||
|
fw = int(float64(tw) * percent) // filled width
|
||||||
|
p float64
|
||||||
|
)
|
||||||
|
|
||||||
|
if m.useRamp {
|
||||||
|
// Gradient fill
|
||||||
|
for i := 0; i < fw; i++ {
|
||||||
|
if m.scaleRamp {
|
||||||
|
p = float64(i) / float64(fw)
|
||||||
|
} else {
|
||||||
|
p = float64(i) / float64(tw)
|
||||||
|
}
|
||||||
|
c := m.rampColorA.BlendLuv(m.rampColorB, p).Hex()
|
||||||
|
b.WriteString(termenv.
|
||||||
|
String(string(m.Full)).
|
||||||
|
Foreground(color(c)).
|
||||||
|
String(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Solid fill
|
||||||
|
s := termenv.String(string(m.Full)).Foreground(color(m.FullColor)).String()
|
||||||
|
b.WriteString(strings.Repeat(s, fw))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Empty fill
|
||||||
|
e := termenv.String(string(m.Empty)).Foreground(color(m.EmptyColor)).String()
|
||||||
|
b.WriteString(strings.Repeat(e, tw-fw))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Model) setRamp(colorA, colorB string, scaled bool) error {
|
||||||
|
a, err := colorful.Hex(colorA)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := colorful.Hex(colorB)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.useRamp = true
|
||||||
|
m.scaleRamp = scaled
|
||||||
|
m.rampColorA = a
|
||||||
|
m.rampColorB = b
|
||||||
|
return nil
|
||||||
|
}
|
@@ -1,118 +1,241 @@
|
|||||||
package spinner
|
package spinner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/muesli/termenv"
|
"github.com/charmbracelet/lipgloss"
|
||||||
|
"github.com/muesli/reflow/ansi"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Spinner is a set of frames used in animating the spinner.
|
// Spinner is a set of frames used in animating the spinner.
|
||||||
type Spinner = int
|
type Spinner struct {
|
||||||
|
Frames []string
|
||||||
// Available types of spinners
|
FPS time.Duration
|
||||||
const (
|
|
||||||
Line Spinner = iota
|
|
||||||
Dot
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
defaultFPS = 9
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// Spinner frames
|
|
||||||
spinners = map[Spinner][]string{
|
|
||||||
Line: {"|", "/", "-", "\\"},
|
|
||||||
Dot: {"⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
color = termenv.ColorProfile().Color
|
// Some spinners to choose from. You could also make your own.
|
||||||
|
var (
|
||||||
|
Line = Spinner{
|
||||||
|
Frames: []string{"|", "/", "-", "\\"},
|
||||||
|
FPS: time.Second / 10, //nolint:gomnd
|
||||||
|
}
|
||||||
|
Dot = Spinner{
|
||||||
|
Frames: []string{"⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "},
|
||||||
|
FPS: time.Second / 10, //nolint:gomnd
|
||||||
|
}
|
||||||
|
MiniDot = Spinner{
|
||||||
|
Frames: []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
|
||||||
|
FPS: time.Second / 12, //nolint:gomnd
|
||||||
|
}
|
||||||
|
Jump = Spinner{
|
||||||
|
Frames: []string{"⢄", "⢂", "⢁", "⡁", "⡈", "⡐", "⡠"},
|
||||||
|
FPS: time.Second / 10, //nolint:gomnd
|
||||||
|
}
|
||||||
|
Pulse = Spinner{
|
||||||
|
Frames: []string{"█", "▓", "▒", "░"},
|
||||||
|
FPS: time.Second / 8, //nolint:gomnd
|
||||||
|
}
|
||||||
|
Points = Spinner{
|
||||||
|
Frames: []string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●"},
|
||||||
|
FPS: time.Second / 7, //nolint:gomnd
|
||||||
|
}
|
||||||
|
Globe = Spinner{
|
||||||
|
Frames: []string{"🌍", "🌎", "🌏"},
|
||||||
|
FPS: time.Second / 4, //nolint:gomnd
|
||||||
|
}
|
||||||
|
Moon = Spinner{
|
||||||
|
Frames: []string{"🌑", "🌒", "🌓", "🌔", "🌕", "🌖", "🌗", "🌘"},
|
||||||
|
FPS: time.Second / 8, //nolint:gomnd
|
||||||
|
}
|
||||||
|
Monkey = Spinner{
|
||||||
|
Frames: []string{"🙈", "🙉", "🙊"},
|
||||||
|
FPS: time.Second / 3, //nolint:gomnd
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Model contains the state for the spinner. Use NewModel to create new models
|
// Model contains the state for the spinner. Use NewModel to create new models
|
||||||
// rather than using Model as a struct literal.
|
// rather than using Model as a struct literal.
|
||||||
type Model struct {
|
type Model struct {
|
||||||
|
|
||||||
// Type is the set of frames to use. See Spinner.
|
// Spinner settings to use. See type Spinner.
|
||||||
Type Spinner
|
Spinner Spinner
|
||||||
|
|
||||||
// FPS is the speed at which the ticker should tick
|
// Style sets the styling for the spinner. Most of the time you'll just
|
||||||
FPS int
|
// want foreground and background coloring, and potentially some padding.
|
||||||
|
//
|
||||||
|
// For an introduction to styling with Lip Gloss see:
|
||||||
|
// https://github.com/charmbracelet/lipgloss
|
||||||
|
Style lipgloss.Style
|
||||||
|
|
||||||
// ForegroundColor sets the background color of the spinner. It can be a
|
// MinimumLifetime is the minimum amount of time the spinner can run. Any
|
||||||
// hex code or one of the 256 ANSI colors. If the terminal emulator can't
|
// logic around this can be implemented in view that implements this
|
||||||
// doesn't support the color specified it will automatically degrade
|
// spinner. If HideFor is set MinimumLifetime will be added on top of
|
||||||
// (per github.com/muesli/termenv).
|
// HideFor. In other words, if HideFor is 100ms and MinimumLifetime is
|
||||||
ForegroundColor string
|
// 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
|
||||||
|
|
||||||
// BackgroundColor sets the background color of the spinner. It can be a
|
// HideFor can be used to wait to show the spinner until a certain amount
|
||||||
// hex code or one of the 256 ANSI colors. If the terminal emulator can't
|
// of time has passed. This can be useful for preventing flicking when load
|
||||||
// doesn't support the color specified it will automatically degrade
|
// times are very fast.
|
||||||
// (per github.com/muesli/termenv).
|
// Optional.
|
||||||
BackgroundColor string
|
//
|
||||||
|
// This is considered experimental and may not appear in future versions of
|
||||||
// CustomMsgFunc can be used to a custom message on tick. This can be
|
// this library.
|
||||||
// useful when you have spinners in different parts of your application and
|
HideFor time.Duration
|
||||||
// want to differentiate between the messages for clarity and simplicity.
|
|
||||||
// If nil, this setting is ignored.
|
|
||||||
CustomMsgFunc func() tea.Msg
|
|
||||||
|
|
||||||
frame int
|
frame int
|
||||||
|
startTime time.Time
|
||||||
|
tag int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start resets resets the spinner start time. For use with MinimumLifetime and
|
||||||
|
// MinimumStartTime. Optional.
|
||||||
|
//
|
||||||
|
// This function is optional and generally considered for advanced use only.
|
||||||
|
// Most of the time your application logic will obviate the need for this
|
||||||
|
// method.
|
||||||
|
//
|
||||||
|
// This is considered experimental and may not appear in future versions of
|
||||||
|
// this library.
|
||||||
|
func (m *Model) Start() {
|
||||||
|
m.startTime = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finish sets the internal timer to a completed state so long as the spinner
|
||||||
|
// isn't flagged to be showing. If it is showing, finish has no effect. The
|
||||||
|
// idea here is that you call Finish if your operation has completed and, if
|
||||||
|
// the spinner isn't showing yet (by virtue of HideFor) then Visible() doesn't
|
||||||
|
// show the spinner at all.
|
||||||
|
//
|
||||||
|
// This is intended to work in conjunction with MinimumLifetime and
|
||||||
|
// MinimumStartTime, is completely optional.
|
||||||
|
//
|
||||||
|
// This function is optional and generally considered for advanced use only.
|
||||||
|
// Most of the time your application logic will obviate the need for this
|
||||||
|
// method.
|
||||||
|
//
|
||||||
|
// This is considered experimental and may not appear in future versions of
|
||||||
|
// this library.
|
||||||
|
func (m *Model) Finish() {
|
||||||
|
if m.hidden() {
|
||||||
|
m.startTime = time.Time{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// advancedMode returns whether or not the user is making use of HideFor and
|
||||||
|
// MinimumLifetime properties.
|
||||||
|
func (m Model) advancedMode() bool {
|
||||||
|
return m.HideFor > 0 && m.MinimumLifetime > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 the minimum lifetime of this spinner has been
|
||||||
|
// exceeded.
|
||||||
|
func (m Model) finished() bool {
|
||||||
|
if m.startTime.IsZero() || 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 method 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.
|
||||||
|
//
|
||||||
|
// This function is optional and generally considered for advanced use only.
|
||||||
|
// Most of the time your application logic will obviate the need for this
|
||||||
|
// method.
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
func NewModel() Model {
|
func NewModel() Model {
|
||||||
return Model{
|
return Model{Spinner: Line}
|
||||||
Type: Line,
|
|
||||||
FPS: defaultFPS,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
tag int
|
||||||
|
}
|
||||||
|
|
||||||
// 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
|
||||||
// is setup so as not to call this Update needlessly.
|
// is setup so as not to call this Update needlessly.
|
||||||
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||||
|
switch msg := msg.(type) {
|
||||||
|
case TickMsg:
|
||||||
|
// If a tag is set, and it's not the one we expect, reject the message.
|
||||||
|
// This prevents the spinner from receiving too many messages and
|
||||||
|
// thus spinning too fast.
|
||||||
|
if msg.tag > 0 && msg.tag != m.tag {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
m.frame++
|
m.frame++
|
||||||
if m.frame >= len(spinners[m.Type]) {
|
if m.frame >= len(m.Spinner.Frames) {
|
||||||
m.frame = 0
|
m.frame = 0
|
||||||
}
|
}
|
||||||
if m.CustomMsgFunc != nil {
|
|
||||||
return m, Tick(m)
|
m.tag++
|
||||||
|
return m, m.tick(m.tag)
|
||||||
|
default:
|
||||||
|
return m, nil
|
||||||
}
|
}
|
||||||
return m, Tick(m)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// View renders the model's view.
|
// View renders the model's view.
|
||||||
func View(model Model) string {
|
func (m Model) View() string {
|
||||||
s := spinners[model.Type]
|
if m.frame >= len(m.Spinner.Frames) {
|
||||||
if model.frame >= len(s) {
|
return "(error)"
|
||||||
return "[error]"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
str := s[model.frame]
|
frame := m.Spinner.Frames[m.frame]
|
||||||
|
|
||||||
if model.ForegroundColor != "" || model.BackgroundColor != "" {
|
// If we're using the fine-grained hide/show spinner rules and those rules
|
||||||
return termenv.
|
// deem that the spinner should be hidden, draw an empty space in place of
|
||||||
String(str).
|
// the spinner.
|
||||||
Foreground(color(model.ForegroundColor)).
|
if m.advancedMode() && !m.Visible() {
|
||||||
Background(color(model.BackgroundColor)).
|
frame = strings.Repeat(" ", ansi.PrintableRuneWidth(frame))
|
||||||
String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return str
|
return m.Style.Render(frame)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tick is the command used to advance the spinner one frame.
|
// Tick is the command used to advance the spinner one frame. Use this command
|
||||||
func Tick(model Model) tea.Cmd {
|
// to effectively start the spinner.
|
||||||
return func() tea.Msg {
|
func Tick() tea.Msg {
|
||||||
time.Sleep(time.Second / time.Duration(model.FPS))
|
return TickMsg{Time: time.Now()}
|
||||||
if model.CustomMsgFunc != nil {
|
|
||||||
return model.CustomMsgFunc()
|
|
||||||
}
|
}
|
||||||
return TickMsg{}
|
|
||||||
|
func (m Model) tick(tag int) tea.Cmd {
|
||||||
|
return tea.Tick(m.Spinner.FPS, func(t time.Time) tea.Msg {
|
||||||
|
return TickMsg{
|
||||||
|
Time: t,
|
||||||
|
tag: tag,
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,147 +1,373 @@
|
|||||||
package viewport
|
package viewport
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MODEL
|
const (
|
||||||
|
spacebar = " "
|
||||||
|
mouseWheelDelta = 3
|
||||||
|
)
|
||||||
|
|
||||||
|
// Model is the Bubble Tea model for this viewport element.
|
||||||
type Model struct {
|
type Model struct {
|
||||||
Err error
|
|
||||||
Width int
|
Width int
|
||||||
Height int
|
Height int
|
||||||
Y int
|
|
||||||
|
// YOffset is the vertical scroll position.
|
||||||
|
YOffset int
|
||||||
|
|
||||||
|
// YPosition is the position of the viewport in relation to the terminal
|
||||||
|
// window. It's used in high performance rendering.
|
||||||
|
YPosition int
|
||||||
|
|
||||||
|
// HighPerformanceRendering bypasses the normal Bubble Tea renderer to
|
||||||
|
// provide higher performance rendering. Most of the time the normal Bubble
|
||||||
|
// Tea rendering methods will suffice, but if you're passing content with
|
||||||
|
// a lot of ANSI escape codes you may see improved rendering in certain
|
||||||
|
// terminals with this enabled.
|
||||||
|
//
|
||||||
|
// This should only be used in program occupying the entire terminal,
|
||||||
|
// which is usually via the alternate screen buffer.
|
||||||
|
HighPerformanceRendering bool
|
||||||
|
|
||||||
lines []string
|
lines []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scrollpercent returns the amount scrolled as a float between 0 and 1.
|
// AtTop returns whether or not the viewport is in the very top position.
|
||||||
|
func (m Model) AtTop() bool {
|
||||||
|
return m.YOffset <= 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// AtBottom returns whether or not the viewport is at or past the very bottom
|
||||||
|
// position.
|
||||||
|
func (m Model) AtBottom() bool {
|
||||||
|
return m.YOffset >= len(m.lines)-1-m.Height
|
||||||
|
}
|
||||||
|
|
||||||
|
// PastBottom returns whether or not the viewport is scrolled beyond the last
|
||||||
|
// line. This can happen when adjusting the viewport height.
|
||||||
|
func (m Model) PastBottom() bool {
|
||||||
|
return m.YOffset > len(m.lines)-1-m.Height
|
||||||
|
}
|
||||||
|
|
||||||
|
// ScrollPercent returns the amount scrolled as a float between 0 and 1.
|
||||||
func (m Model) ScrollPercent() float64 {
|
func (m Model) ScrollPercent() float64 {
|
||||||
if m.Height >= len(m.lines) {
|
if m.Height >= len(m.lines) {
|
||||||
return 1.0
|
return 1.0
|
||||||
}
|
}
|
||||||
y := float64(m.Y)
|
y := float64(m.YOffset)
|
||||||
h := float64(m.Height)
|
h := float64(m.Height)
|
||||||
t := float64(len(m.lines))
|
t := float64(len(m.lines) - 1)
|
||||||
return y / (t - h)
|
v := y / (t - h)
|
||||||
|
return math.Max(0.0, math.Min(1.0, v))
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetContent set the pager's text content.
|
// SetContent set the pager's text content. For high performance rendering the
|
||||||
|
// Sync command should also be called.
|
||||||
func (m *Model) SetContent(s string) {
|
func (m *Model) SetContent(s string) {
|
||||||
s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings
|
s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings
|
||||||
m.lines = strings.Split(s, "\n")
|
m.lines = strings.Split(s, "\n")
|
||||||
|
|
||||||
|
if m.YOffset > len(m.lines)-1 {
|
||||||
|
m.GotoBottom()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewModel creates a new pager model. Pass the dimensions of the pager.
|
// Return the lines that should currently be visible in the viewport.
|
||||||
func NewModel(width, height int) Model {
|
func (m Model) visibleLines() (lines []string) {
|
||||||
return Model{
|
if len(m.lines) > 0 {
|
||||||
Width: width,
|
top := max(0, m.YOffset)
|
||||||
Height: height,
|
bottom := clamp(m.YOffset+m.Height, top, len(m.lines))
|
||||||
|
lines = m.lines[top:bottom]
|
||||||
}
|
}
|
||||||
|
return lines
|
||||||
}
|
}
|
||||||
|
|
||||||
// ViewDown moves the view down by the number of lines in the viewport.
|
// ViewDown moves the view down by the number of lines in the viewport.
|
||||||
// Basically, "page down".
|
// Basically, "page down".
|
||||||
func (m *Model) ViewDown() {
|
func (m *Model) ViewDown() []string {
|
||||||
m.Y = min(len(m.lines)-m.Height, m.Y+m.Height)
|
if m.AtBottom() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
m.YOffset = min(
|
||||||
|
m.YOffset+m.Height, // target
|
||||||
|
len(m.lines)-1-m.Height, // fallback
|
||||||
|
)
|
||||||
|
|
||||||
|
return m.visibleLines()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ViewUp moves the view up by one height of the viewport. Basically, "page up".
|
// ViewUp moves the view up by one height of the viewport. Basically, "page up".
|
||||||
func (m *Model) ViewUp() {
|
func (m *Model) ViewUp() []string {
|
||||||
m.Y = max(0, m.Y-m.Height)
|
if m.AtTop() {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HalfViewUp moves the view up by half the height of the viewport.
|
m.YOffset = max(
|
||||||
func (m *Model) HalfViewUp() {
|
m.YOffset-m.Height, // target
|
||||||
m.Y = max(0, m.Y-m.Height/2)
|
0, // fallback
|
||||||
|
)
|
||||||
|
|
||||||
|
return m.visibleLines()
|
||||||
}
|
}
|
||||||
|
|
||||||
// HalfViewDown moves the view down by half the height of the viewport.
|
// HalfViewDown moves the view down by half the height of the viewport.
|
||||||
func (m *Model) HalfViewDown() {
|
func (m *Model) HalfViewDown() (lines []string) {
|
||||||
m.Y = min(len(m.lines)-m.Height, m.Y+m.Height/2)
|
if m.AtBottom() {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LineDown moves the view up by the given number of lines.
|
m.YOffset = min(
|
||||||
func (m *Model) LineDown(n int) {
|
m.YOffset+m.Height/2, // target
|
||||||
m.Y = min(len(m.lines)-m.Height, m.Y+n)
|
len(m.lines)-1-m.Height, // fallback
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(m.lines) > 0 {
|
||||||
|
top := max(m.YOffset+m.Height/2, 0)
|
||||||
|
bottom := clamp(m.YOffset+m.Height, top, len(m.lines)-1)
|
||||||
|
lines = m.lines[top:bottom]
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines
|
||||||
|
}
|
||||||
|
|
||||||
|
// HalfViewUp moves the view up by half the height of the viewport.
|
||||||
|
func (m *Model) HalfViewUp() (lines []string) {
|
||||||
|
if m.AtTop() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
m.YOffset = max(
|
||||||
|
m.YOffset-m.Height/2, // target
|
||||||
|
0, // fallback
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(m.lines) > 0 {
|
||||||
|
top := max(m.YOffset, 0)
|
||||||
|
bottom := clamp(m.YOffset+m.Height/2, top, len(m.lines)-1)
|
||||||
|
lines = m.lines[top:bottom]
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines
|
||||||
}
|
}
|
||||||
|
|
||||||
// LineDown moves the view down by the given number of lines.
|
// LineDown moves the view down by the given number of lines.
|
||||||
func (m *Model) LineUp(n int) {
|
func (m *Model) LineDown(n int) (lines []string) {
|
||||||
m.Y = max(0, m.Y-n)
|
if m.AtBottom() || n == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure the number of lines by which we're going to scroll isn't
|
||||||
|
// greater than the number of lines we actually have left before we reach
|
||||||
|
// the bottom.
|
||||||
|
maxDelta := (len(m.lines) - 1) - (m.YOffset + m.Height) // number of lines - viewport bottom edge
|
||||||
|
n = min(n, maxDelta)
|
||||||
|
|
||||||
|
m.YOffset = min(
|
||||||
|
m.YOffset+n, // target
|
||||||
|
len(m.lines)-1-m.Height, // fallback
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(m.lines) > 0 {
|
||||||
|
top := max(m.YOffset+m.Height-n, 0)
|
||||||
|
bottom := clamp(m.YOffset+m.Height, top, len(m.lines)-1)
|
||||||
|
lines = m.lines[top:bottom]
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines
|
||||||
|
}
|
||||||
|
|
||||||
|
// LineUp moves the view down by the given number of lines. Returns the new
|
||||||
|
// lines to show.
|
||||||
|
func (m *Model) LineUp(n int) (lines []string) {
|
||||||
|
if m.AtTop() || n == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure the number of lines by which we're going to scroll isn't
|
||||||
|
// greater than the number of lines we are from the top.
|
||||||
|
n = min(n, m.YOffset)
|
||||||
|
|
||||||
|
m.YOffset = max(m.YOffset-n, 0)
|
||||||
|
|
||||||
|
if len(m.lines) > 0 {
|
||||||
|
top := max(0, m.YOffset)
|
||||||
|
bottom := clamp(m.YOffset+n, top, len(m.lines)-1)
|
||||||
|
lines = m.lines[top:bottom]
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines
|
||||||
|
}
|
||||||
|
|
||||||
|
// GotoTop sets the viewport to the top position.
|
||||||
|
func (m *Model) GotoTop() (lines []string) {
|
||||||
|
if m.AtTop() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
m.YOffset = 0
|
||||||
|
|
||||||
|
if len(m.lines) > 0 {
|
||||||
|
top := m.YOffset
|
||||||
|
bottom := clamp(m.YOffset+m.Height, top, len(m.lines)-1)
|
||||||
|
lines = m.lines[top:bottom]
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines
|
||||||
|
}
|
||||||
|
|
||||||
|
// GotoBottom sets the viewport to the bottom position.
|
||||||
|
func (m *Model) GotoBottom() (lines []string) {
|
||||||
|
m.YOffset = max(len(m.lines)-1-m.Height, 0)
|
||||||
|
|
||||||
|
if len(m.lines) > 0 {
|
||||||
|
top := m.YOffset
|
||||||
|
bottom := max(len(m.lines)-1, 0)
|
||||||
|
lines = m.lines[top:bottom]
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines
|
||||||
|
}
|
||||||
|
|
||||||
|
// COMMANDS
|
||||||
|
|
||||||
|
// Sync tells the renderer where the viewport will be located and requests
|
||||||
|
// a render of the current state of the viewport. It should be called for the
|
||||||
|
// first render and after a window resize.
|
||||||
|
//
|
||||||
|
// For high performance rendering only.
|
||||||
|
func Sync(m Model) tea.Cmd {
|
||||||
|
if len(m.lines) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: we should probably use m.visibleLines() rather than these two
|
||||||
|
// expressions.
|
||||||
|
top := max(m.YOffset, 0)
|
||||||
|
bottom := clamp(m.YOffset+m.Height, 0, len(m.lines)-1)
|
||||||
|
|
||||||
|
return tea.SyncScrollArea(
|
||||||
|
m.lines[top:bottom],
|
||||||
|
m.YPosition,
|
||||||
|
m.YPosition+m.Height,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ViewDown is a high performance command that moves the viewport up by a given
|
||||||
|
// numer of lines. Use Model.ViewDown to get the lines that should be rendered.
|
||||||
|
// For example:
|
||||||
|
//
|
||||||
|
// lines := model.ViewDown(1)
|
||||||
|
// cmd := ViewDown(m, lines)
|
||||||
|
//
|
||||||
|
func ViewDown(m Model, lines []string) tea.Cmd {
|
||||||
|
if len(lines) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return tea.ScrollDown(lines, m.YPosition, m.YPosition+m.Height)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ViewUp is a high performance command the moves the viewport down by a given
|
||||||
|
// number of lines height. Use Model.ViewUp to get the lines that should be
|
||||||
|
// rendered.
|
||||||
|
func ViewUp(m Model, lines []string) tea.Cmd {
|
||||||
|
if len(lines) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return tea.ScrollUp(lines, m.YPosition, m.YPosition+m.Height)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UPDATE
|
// UPDATE
|
||||||
|
|
||||||
// Update runs the update loop with default keybindings. To define your own
|
// Update runs the update loop with default keybindings similar to popular
|
||||||
// keybindings use the methods on Model.
|
// pagers. To define your own keybindings use the methods on Model (i.e.
|
||||||
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
// Model.LineDown()) and define your own update function.
|
||||||
switch msg := msg.(type) {
|
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||||
|
var cmd tea.Cmd
|
||||||
|
|
||||||
|
switch msg := msg.(type) {
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
// Down one page
|
// Down one page
|
||||||
case "pgdown":
|
case "pgdown", spacebar, "f":
|
||||||
fallthrough
|
lines := m.ViewDown()
|
||||||
case " ": // spacebar
|
if m.HighPerformanceRendering {
|
||||||
fallthrough
|
cmd = ViewDown(m, lines)
|
||||||
case "f":
|
}
|
||||||
m.ViewDown()
|
|
||||||
return m, nil
|
|
||||||
|
|
||||||
// Up one page
|
// Up one page
|
||||||
case "pgup":
|
case "pgup", "b":
|
||||||
fallthrough
|
lines := m.ViewUp()
|
||||||
case "b":
|
if m.HighPerformanceRendering {
|
||||||
m.ViewUp()
|
cmd = ViewUp(m, lines)
|
||||||
return m, nil
|
}
|
||||||
|
|
||||||
// Down half page
|
// Down half page
|
||||||
case "d":
|
case "d", "ctrl+d":
|
||||||
m.HalfViewDown()
|
lines := m.HalfViewDown()
|
||||||
return m, nil
|
if m.HighPerformanceRendering {
|
||||||
|
cmd = ViewDown(m, lines)
|
||||||
|
}
|
||||||
|
|
||||||
// Up half page
|
// Up half page
|
||||||
case "u":
|
case "u", "ctrl+u":
|
||||||
m.HalfViewUp()
|
lines := m.HalfViewUp()
|
||||||
return m, nil
|
if m.HighPerformanceRendering {
|
||||||
|
cmd = ViewUp(m, lines)
|
||||||
|
}
|
||||||
|
|
||||||
// Down one line
|
// Down one line
|
||||||
case "down":
|
case "down", "j":
|
||||||
fallthrough
|
lines := m.LineDown(1)
|
||||||
case "j":
|
if m.HighPerformanceRendering {
|
||||||
m.LineDown(1)
|
cmd = ViewDown(m, lines)
|
||||||
return m, nil
|
}
|
||||||
|
|
||||||
// Up one line
|
// Up one line
|
||||||
case "up":
|
case "up", "k":
|
||||||
fallthrough
|
lines := m.LineUp(1)
|
||||||
case "k":
|
if m.HighPerformanceRendering {
|
||||||
m.LineUp(1)
|
cmd = ViewUp(m, lines)
|
||||||
return m, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return m, nil
|
case tea.MouseMsg:
|
||||||
|
switch msg.Type {
|
||||||
|
case tea.MouseWheelUp:
|
||||||
|
lines := m.LineUp(mouseWheelDelta)
|
||||||
|
if m.HighPerformanceRendering {
|
||||||
|
cmd = ViewUp(m, lines)
|
||||||
|
}
|
||||||
|
|
||||||
|
case tea.MouseWheelDown:
|
||||||
|
lines := m.LineDown(mouseWheelDelta)
|
||||||
|
if m.HighPerformanceRendering {
|
||||||
|
cmd = ViewDown(m, lines)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// VIEW
|
// VIEW
|
||||||
|
|
||||||
// View renders the viewport into a string.
|
// View renders the viewport into a string.
|
||||||
func View(m Model) string {
|
func (m Model) View() string {
|
||||||
if m.Err != nil {
|
if m.HighPerformanceRendering {
|
||||||
return m.Err.Error()
|
// Just send newlines since we're doing to be rendering the actual
|
||||||
|
// content seprately. We still need send something that equals the
|
||||||
|
// height of this view so that the Bubble Tea standard renderer can
|
||||||
|
// position anything below this view properly.
|
||||||
|
return strings.Repeat("\n", m.Height-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
var lines []string
|
lines := m.visibleLines()
|
||||||
|
|
||||||
if len(m.lines) > 0 {
|
|
||||||
top := max(0, m.Y)
|
|
||||||
bottom := min(len(m.lines), m.Y+m.Height)
|
|
||||||
lines = m.lines[top:bottom]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fill empty space with newlines
|
// Fill empty space with newlines
|
||||||
extraLines := ""
|
extraLines := ""
|
||||||
@@ -154,6 +380,10 @@ func View(m Model) string {
|
|||||||
|
|
||||||
// ETC
|
// ETC
|
||||||
|
|
||||||
|
func clamp(v, low, high int) int {
|
||||||
|
return min(high, max(low, v))
|
||||||
|
}
|
||||||
|
|
||||||
func min(a, b int) int {
|
func min(a, b int) int {
|
||||||
if a < b {
|
if a < b {
|
||||||
return a
|
return a
|
||||||
|
Reference in New Issue
Block a user