mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-10-17 16:15:58 +03:00
Compare commits
50 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
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 |
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
|
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
|
25
README.md
25
README.md
@@ -1,17 +1,17 @@
|
|||||||
Bubbles
|
Bubbles
|
||||||
=======
|
=======
|
||||||
|
|
||||||
<p>
|
[](https://github.com/charmbracelet/bubbles/releases)
|
||||||
<a href="https://pkg.go.dev/github.com/charmbracelet/bubbles?tab=doc"><img src="https://godoc.org/github.com/golang/gddo?status.svg" alt="GoDoc"></a>
|
[](https://pkg.go.dev/github.com/charmbracelet/bubbles)
|
||||||
<a href="https://github.com/charmbracelet/bubbles/actions"><img src="https://github.com/charmbracelet/glow/workflows/build/badge.svg" alt="Build Status"></a>
|
[](https://github.com/charmbracelet/bubbles/actions)
|
||||||
</p>
|
[](http://goreportcard.com/report/charmbracelet/bubbles)
|
||||||
|
|
||||||
Some components for [Bubble Tea](https://github.com/charmbraclet/bubbletea) applications.
|
Some components for [Bubble Tea](https://github.com/charmbracelet/bubbletea) applications.
|
||||||
|
|
||||||
These components are used in production in [Glow][glow] and [Charm][charm].
|
These components are used in production in [Glow][glow] and [Charm][charm].
|
||||||
|
|
||||||
[glow]: https://github.com/charmbraclet/glow
|
[glow]: https://github.com/charmbracelet/glow
|
||||||
[charm]: https://github.com/charmbraclet/charm
|
[charm]: https://github.com/charmbracelet/charm
|
||||||
|
|
||||||
|
|
||||||
## Spinner
|
## Spinner
|
||||||
@@ -33,7 +33,7 @@ pasting, in-place scrolling when the value exceeds the width of the element and
|
|||||||
the common, and many customization options.
|
the common, and many customization options.
|
||||||
|
|
||||||
* [Example code, one field](https://github.com/charmbracelet/tea/tree/master/examples/textinput/main.go)
|
* [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)
|
* [Example code, many fields](https://github.com/charmbracelet/tea/tree/master/examples/textinputs/main.go)
|
||||||
|
|
||||||
|
|
||||||
## Paginator
|
## Paginator
|
||||||
@@ -59,7 +59,7 @@ for applications which make use of the alterate screen buffer.
|
|||||||
|
|
||||||
* [Example code](https://github.com/charmbracelet/tea/tree/master/examples/pager/main.go)
|
* [Example code](https://github.com/charmbracelet/tea/tree/master/examples/pager/main.go)
|
||||||
|
|
||||||
This compoent is well complimented with [Reflow][reflow] for ANSI-aware
|
This component is well complimented with [Reflow][reflow] for ANSI-aware
|
||||||
indenting and text wrapping.
|
indenting and text wrapping.
|
||||||
|
|
||||||
[reflow]: https://github.com/muesli/reflow
|
[reflow]: https://github.com/muesli/reflow
|
||||||
@@ -72,8 +72,9 @@ indenting and text wrapping.
|
|||||||
|
|
||||||
***
|
***
|
||||||
|
|
||||||
A [Charm](https://charm.sh) project.
|
Part of [Charm](https://charm.sh).
|
||||||
|
|
||||||
<img alt="the Charm logo" src="https://stuff.charm.sh/charm-badge.jpg" width="400">
|
<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!
|
||||||
|
|
||||||
Charm热爱开源!
|
|
||||||
|
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.2
|
||||||
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.2 h1:y9Yo2Pv8tcm3mAJsWONGsmHhzrbNxJVxpVtemikxE9A=
|
||||||
|
github.com/charmbracelet/bubbletea v0.12.2/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,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
|
||||||
@@ -40,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 {
|
||||||
@@ -115,7 +115,7 @@ 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 {
|
if m.UsePgUpPgDownKeys {
|
||||||
@@ -164,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 {
|
||||||
@@ -189,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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -7,17 +7,36 @@ import (
|
|||||||
"github.com/muesli/termenv"
|
"github.com/muesli/termenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const defaultFPS = time.Second / 10
|
||||||
defaultFPS = time.Second / 10
|
|
||||||
)
|
|
||||||
|
|
||||||
// Spinner is a set of frames used in animating the spinner.
|
// Spinner is a set of frames used in animating the spinner.
|
||||||
type Spinner = []string
|
type Spinner = []string
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Some spinners to choose from. You could also make your own.
|
// Some spinners to choose from. You could also make your own.
|
||||||
Line = Spinner([]string{"|", "/", "-", "\\"})
|
Line = Spinner{"|", "/", "-", "\\"}
|
||||||
Dot = Spinner([]string{"⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "})
|
Dot = Spinner{"⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "}
|
||||||
|
Globe = Spinner{"🌍 ", "🌎 ", "🌏 "}
|
||||||
|
Moon = Spinner{"🌑 ", "🌒 ", "🌓 ", "🌔 ", "🌕 ", "🌖 ", "🌗 ", "🌘 "}
|
||||||
|
Monkey = Spinner{"🙈 ", "🙈 ", "🙉 ", "🙊 "}
|
||||||
|
Jump = Spinner{"⢄", "⢂", "⢁", "⡁", "⡈", "⡐", "⡠"}
|
||||||
|
Bit8 = Spinner{
|
||||||
|
"⠀", "⠁", "⠂", "⠃", "⠄", "⠅", "⠆", "⠇", "⡀", "⡁", "⡂", "⡃", "⡄", "⡅", "⡆", "⡇",
|
||||||
|
"⠈", "⠉", "⠊", "⠋", "⠌", "⠍", "⠎", "⠏", "⡈", "⡉", "⡊", "⡋", "⡌", "⡍", "⡎", "⡏",
|
||||||
|
"⠐", "⠑", "⠒", "⠓", "⠔", "⠕", "⠖", "⠗", "⡐", "⡑", "⡒", "⡓", "⡔", "⡕", "⡖", "⡗",
|
||||||
|
"⠘", "⠙", "⠚", "⠛", "⠜", "⠝", "⠞", "⠟", "⡘", "⡙", "⡚", "⡛", "⡜", "⡝", "⡞", "⡟",
|
||||||
|
"⠠", "⠡", "⠢", "⠣", "⠤", "⠥", "⠦", "⠧", "⡠", "⡡", "⡢", "⡣", "⡤", "⡥", "⡦", "⡧",
|
||||||
|
"⠨", "⠩", "⠪", "⠫", "⠬", "⠭", "⠮", "⠯", "⡨", "⡩", "⡪", "⡫", "⡬", "⡭", "⡮", "⡯",
|
||||||
|
"⠰", "⠱", "⠲", "⠳", "⠴", "⠵", "⠶", "⠷", "⡰", "⡱", "⡲", "⡳", "⡴", "⡵", "⡶", "⡷",
|
||||||
|
"⠸", "⠹", "⠺", "⠻", "⠼", "⠽", "⠾", "⠿", "⡸", "⡹", "⡺", "⡻", "⡼", "⡽", "⡾", "⡿",
|
||||||
|
"⢀", "⢁", "⢂", "⢃", "⢄", "⢅", "⢆", "⢇", "⣀", "⣁", "⣂", "⣃", "⣄", "⣅", "⣆", "⣇",
|
||||||
|
"⢈", "⢉", "⢊", "⢋", "⢌", "⢍", "⢎", "⢏", "⣈", "⣉", "⣊", "⣋", "⣌", "⣍", "⣎", "⣏",
|
||||||
|
"⢐", "⢑", "⢒", "⢓", "⢔", "⢕", "⢖", "⢗", "⣐", "⣑", "⣒", "⣓", "⣔", "⣕", "⣖", "⣗",
|
||||||
|
"⢘", "⢙", "⢚", "⢛", "⢜", "⢝", "⢞", "⢟", "⣘", "⣙", "⣚", "⣛", "⣜", "⣝", "⣞", "⣟",
|
||||||
|
"⢠", "⢡", "⢢", "⢣", "⢤", "⢥", "⢦", "⢧", "⣠", "⣡", "⣢", "⣣", "⣤", "⣥", "⣦", "⣧",
|
||||||
|
"⢨", "⢩", "⢪", "⢫", "⢬", "⢭", "⢮", "⢯", "⣨", "⣩", "⣪", "⣫", "⣬", "⣭", "⣮", "⣯",
|
||||||
|
"⢰", "⢱", "⢲", "⢳", "⢴", "⢵", "⢶", "⢷", "⣰", "⣱", "⣲", "⣳", "⣴", "⣵", "⣶", "⣷",
|
||||||
|
"⢸", "⢹", "⢺", "⢻", "⢼", "⢽", "⢾", "⢿", "⣸", "⣹", "⣺", "⣻", "⣼", "⣽", "⣾", "⣿"}
|
||||||
|
|
||||||
color = termenv.ColorProfile().Color
|
color = termenv.ColorProfile().Color
|
||||||
)
|
)
|
||||||
@@ -29,7 +48,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 +63,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,35 +144,43 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
type startTick struct{}
|
||||||
|
|
||||||
// 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) {
|
||||||
if _, ok := msg.(TickMsg); ok {
|
switch msg.(type) {
|
||||||
|
case startTick:
|
||||||
|
return m, m.tick()
|
||||||
|
case TickMsg:
|
||||||
m.frame++
|
m.frame++
|
||||||
if m.frame >= len(m.Frames) {
|
if m.frame >= len(m.Frames) {
|
||||||
m.frame = 0
|
m.frame = 0
|
||||||
}
|
}
|
||||||
return m, Tick(m)
|
return m, m.tick()
|
||||||
|
default:
|
||||||
|
return m, nil
|
||||||
}
|
}
|
||||||
return m, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// View renders the model's view.
|
// View renders the model's view.
|
||||||
func View(model Model) string {
|
func (m Model) View() string {
|
||||||
if model.frame >= len(model.Frames) {
|
if m.frame >= len(m.Frames) {
|
||||||
return "error"
|
return "error"
|
||||||
}
|
}
|
||||||
|
|
||||||
frame := model.Frames[model.frame]
|
frame := m.Frames[m.frame]
|
||||||
|
|
||||||
if model.ForegroundColor != "" || model.BackgroundColor != "" {
|
if m.ForegroundColor != "" || m.BackgroundColor != "" {
|
||||||
return termenv.
|
return termenv.
|
||||||
String(frame).
|
String(frame).
|
||||||
Foreground(color(model.ForegroundColor)).
|
Foreground(color(m.ForegroundColor)).
|
||||||
Background(color(model.BackgroundColor)).
|
Background(color(m.BackgroundColor)).
|
||||||
String()
|
String()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,8 +188,14 @@ 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() tea.Msg {
|
||||||
return tea.Tick(m.FPS, func(time.Time) tea.Msg {
|
return startTick{}
|
||||||
return TickMsg{}
|
}
|
||||||
|
|
||||||
|
func (m Model) tick() tea.Cmd {
|
||||||
|
return tea.Tick(m.FPS, func(t time.Time) tea.Msg {
|
||||||
|
return TickMsg{
|
||||||
|
Time: t,
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package textinput
|
package textinput
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
@@ -11,31 +12,66 @@ import (
|
|||||||
"github.com/muesli/termenv"
|
"github.com/muesli/termenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultBlinkSpeed = time.Millisecond * 530
|
||||||
|
|
||||||
|
// color is a helper for returning colors.
|
||||||
|
var color func(s string) termenv.Color = termenv.ColorProfile().Color
|
||||||
|
|
||||||
|
// blinkMsg and blinkCanceled are used to manage cursor blinking.
|
||||||
|
type blinkMsg struct{}
|
||||||
|
type blinkCanceled struct{}
|
||||||
|
|
||||||
|
// Messages for clipboard events.
|
||||||
|
type pasteMsg string
|
||||||
|
type pasteErrMsg struct{ error }
|
||||||
|
|
||||||
|
// EchoMode sets the input behavior of the text input field.
|
||||||
|
type EchoMode int
|
||||||
|
|
||||||
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
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
// blinkCtx manages cursor blinking.
|
||||||
// color is a helper for returning colors
|
type blinkCtx struct {
|
||||||
color func(s string) termenv.Color = termenv.ColorProfile().Color
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
type cursorMode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
cursorBlink = iota
|
||||||
|
cursorStatic
|
||||||
|
cursorHide
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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
|
||||||
|
|
||||||
|
// General settings
|
||||||
Prompt string
|
Prompt string
|
||||||
|
Placeholder string
|
||||||
Cursor string
|
Cursor string
|
||||||
BlinkSpeed time.Duration
|
BlinkSpeed time.Duration
|
||||||
Placeholder string
|
|
||||||
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.
|
||||||
@@ -46,23 +82,53 @@ 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
|
||||||
|
|
||||||
|
// Used to manage cursor blink
|
||||||
|
blinkCtx *blinkCtx
|
||||||
|
|
||||||
|
// cursorMode determines the behavior of the cursor
|
||||||
|
cursorMode cursorMode
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewModel creates a new model with default settings.
|
||||||
|
func NewModel() Model {
|
||||||
|
return Model{
|
||||||
|
Prompt: "> ",
|
||||||
|
Placeholder: "",
|
||||||
|
BlinkSpeed: defaultBlinkSpeed,
|
||||||
|
TextColor: "",
|
||||||
|
PlaceholderColor: "240",
|
||||||
|
CursorColor: "",
|
||||||
|
EchoCharacter: '*',
|
||||||
|
CharLimit: 0,
|
||||||
|
|
||||||
|
value: nil,
|
||||||
|
focus: false,
|
||||||
|
blink: true,
|
||||||
|
pos: 0,
|
||||||
|
cursorMode: cursorBlink,
|
||||||
|
|
||||||
|
blinkCtx: &blinkCtx{
|
||||||
|
ctx: context.Background(),
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetValue sets the value of the text input.
|
// SetValue sets the value of the text input.
|
||||||
@@ -74,7 +140,7 @@ func (m *Model) SetValue(s string) {
|
|||||||
m.value = runes
|
m.value = runes
|
||||||
}
|
}
|
||||||
if m.pos > len(m.value) {
|
if m.pos > len(m.value) {
|
||||||
m.pos = len(m.value)
|
m.SetCursor(len(m.value))
|
||||||
}
|
}
|
||||||
m.handleOverflow()
|
m.handleOverflow()
|
||||||
}
|
}
|
||||||
@@ -84,23 +150,30 @@ 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) {
|
// Returns whether or nor the cursor timer should be reset.
|
||||||
|
func (m *Model) SetCursor(pos int) bool {
|
||||||
m.pos = clamp(pos, 0, len(m.value))
|
m.pos = clamp(pos, 0, len(m.value))
|
||||||
m.handleOverflow()
|
m.handleOverflow()
|
||||||
|
|
||||||
|
// Show the cursor unless it's been explicitly hidden
|
||||||
|
m.blink = m.cursorMode == cursorHide
|
||||||
|
|
||||||
|
// Reset cursor blink if necessary
|
||||||
|
return m.cursorMode == cursorBlink
|
||||||
}
|
}
|
||||||
|
|
||||||
// CursorStart moves the cursor to the start of the field.
|
// CursorStart moves the cursor to the start of the field. Returns whether or
|
||||||
func (m *Model) CursorStart() {
|
// not the curosr blink should be reset.
|
||||||
m.pos = 0
|
func (m *Model) CursorStart() bool {
|
||||||
m.handleOverflow()
|
return m.SetCursor(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CursorEnd moves the cursor to the end of the field.
|
// CursorEnd moves the cursor to the end of the field. Returns whether or not
|
||||||
func (m *Model) CursorEnd() {
|
// the cursor blink should be reset.
|
||||||
m.pos = len(m.value)
|
func (m *Model) CursorEnd() bool {
|
||||||
m.handleOverflow()
|
return m.SetCursor(len(m.value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Focused returns the focus state on the model.
|
// Focused returns the focus state on the model.
|
||||||
@@ -111,7 +184,7 @@ func (m Model) Focused() bool {
|
|||||||
// Focus sets the focus state on the model.
|
// Focus sets the focus state on the model.
|
||||||
func (m *Model) Focus() {
|
func (m *Model) Focus() {
|
||||||
m.focus = true
|
m.focus = true
|
||||||
m.blink = false
|
m.blink = m.cursorMode == cursorHide // show the cursor unless we've explicitly hidden it
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blur removes the focus state on the model.
|
// Blur removes the focus state on the model.
|
||||||
@@ -120,22 +193,22 @@ func (m *Model) Blur() {
|
|||||||
m.blink = true
|
m.blink = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset sets the input to its default state with no input.
|
// Reset sets the input to its default state with no input. Returns whether
|
||||||
func (m *Model) Reset() {
|
// or not the cursor blink should reset.
|
||||||
|
func (m *Model) Reset() bool {
|
||||||
m.value = nil
|
m.value = nil
|
||||||
m.pos = 0
|
return m.SetCursor(0)
|
||||||
m.blink = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Paste pastes the contents of the clipboard into the text area (if supported).
|
// handle a clipboard paste event, if supported. Returns whether or not the
|
||||||
func (m *Model) Paste() {
|
// cursor blink should be reset.
|
||||||
pasteString, err := clipboard.ReadAll()
|
func (m *Model) handlePaste(v string) (blink bool) {
|
||||||
if err != nil {
|
paste := []rune(v)
|
||||||
m.Err = err
|
|
||||||
}
|
|
||||||
paste := []rune(pasteString)
|
|
||||||
|
|
||||||
availSpace := m.CharLimit - len(m.value)
|
var availSpace int
|
||||||
|
if m.CharLimit > 0 {
|
||||||
|
availSpace = m.CharLimit - len(m.value)
|
||||||
|
}
|
||||||
|
|
||||||
// If the char limit's been reached cancel
|
// If the char limit's been reached cancel
|
||||||
if m.CharLimit > 0 && availSpace <= 0 {
|
if m.CharLimit > 0 && availSpace <= 0 {
|
||||||
@@ -144,7 +217,7 @@ func (m *Model) Paste() {
|
|||||||
|
|
||||||
// If there's not enough space to paste the whole thing cut the pasted
|
// If there's not enough space to paste the whole thing cut the pasted
|
||||||
// runes down so they'll fit
|
// runes down so they'll fit
|
||||||
if availSpace < len(paste) {
|
if m.CharLimit > 0 && availSpace < len(paste) {
|
||||||
paste = paste[:len(paste)-availSpace]
|
paste = paste[:len(paste)-availSpace]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,15 +230,20 @@ func (m *Model) Paste() {
|
|||||||
// Insert pasted runes
|
// Insert pasted runes
|
||||||
for _, r := range paste {
|
for _, r := range paste {
|
||||||
head = append(head, r)
|
head = append(head, r)
|
||||||
availSpace--
|
|
||||||
m.pos++
|
m.pos++
|
||||||
if m.CharLimit > 0 && availSpace <= 0 {
|
if m.CharLimit > 0 {
|
||||||
break
|
availSpace--
|
||||||
|
if availSpace <= 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put it all back together
|
// Put it all back together
|
||||||
m.value = append(head, tail...)
|
m.value = append(head, tail...)
|
||||||
|
|
||||||
|
// Reset blink state if necessary and run overflow checks
|
||||||
|
return m.SetCursor(m.pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If a max width is defined, perform some logic to treat the visible area
|
// If a max width is defined, perform some logic to treat the visible area
|
||||||
@@ -181,7 +259,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 +273,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 +288,6 @@ func (m *Model) handleOverflow() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
m.offset = m.offsetRight - (len(runes) - 1 - i)
|
m.offset = m.offsetRight - (len(runes) - 1 - i)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +311,76 @@ func (m *Model) colorPlaceholder(s string) string {
|
|||||||
String()
|
String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) wordLeft() {
|
// deleteWordLeft deletes the word left to the cursor. Returns whether or not
|
||||||
|
// the cursor blink should be reset.
|
||||||
|
func (m *Model) deleteWordLeft() (blink bool) {
|
||||||
|
if m.pos == 0 || len(m.value) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
i := m.pos
|
||||||
|
blink = m.SetCursor(m.pos - 1)
|
||||||
|
for unicode.IsSpace(m.value[m.pos]) {
|
||||||
|
// ignore series of whitespace before cursor
|
||||||
|
blink = m.SetCursor(m.pos - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
for m.pos > 0 {
|
||||||
|
if !unicode.IsSpace(m.value[m.pos]) {
|
||||||
|
blink = m.SetCursor(m.pos - 1)
|
||||||
|
} else {
|
||||||
|
if m.pos > 0 {
|
||||||
|
// keep the previous space
|
||||||
|
blink = m.SetCursor(m.pos + 1)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if i > len(m.value) {
|
||||||
|
m.value = m.value[:m.pos]
|
||||||
|
} else {
|
||||||
|
m.value = append(m.value[:m.pos], m.value[i:]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// deleteWordRight deletes the word right to the cursor. Returns whether or not
|
||||||
|
// the cursor blink should be reset.
|
||||||
|
func (m *Model) deleteWordRight() (blink bool) {
|
||||||
|
if m.pos >= len(m.value) || len(m.value) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
i := m.pos
|
||||||
|
blink = m.SetCursor(m.pos + 1)
|
||||||
|
for unicode.IsSpace(m.value[m.pos]) {
|
||||||
|
// ignore series of whitespace after cursor
|
||||||
|
blink = m.SetCursor(m.pos + 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
for m.pos < len(m.value) {
|
||||||
|
if !unicode.IsSpace(m.value[m.pos]) {
|
||||||
|
blink = m.SetCursor(m.pos + 1)
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.pos > len(m.value) {
|
||||||
|
m.value = m.value[:i]
|
||||||
|
} else {
|
||||||
|
m.value = append(m.value[:i], m.value[m.pos:]...)
|
||||||
|
}
|
||||||
|
blink = m.SetCursor(i)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// wordLeft moves the cursor one word to the left. Returns whether or not the
|
||||||
|
// cursor blink should be reset.
|
||||||
|
func (m *Model) wordLeft() (blink bool) {
|
||||||
if m.pos == 0 || len(m.value) == 0 {
|
if m.pos == 0 || len(m.value) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -246,7 +389,7 @@ func (m *Model) wordLeft() {
|
|||||||
|
|
||||||
for i >= 0 {
|
for i >= 0 {
|
||||||
if unicode.IsSpace(m.value[i]) {
|
if unicode.IsSpace(m.value[i]) {
|
||||||
m.pos--
|
blink = m.SetCursor(m.pos - 1)
|
||||||
i--
|
i--
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
@@ -255,15 +398,19 @@ func (m *Model) wordLeft() {
|
|||||||
|
|
||||||
for i >= 0 {
|
for i >= 0 {
|
||||||
if !unicode.IsSpace(m.value[i]) {
|
if !unicode.IsSpace(m.value[i]) {
|
||||||
m.pos--
|
blink = m.SetCursor(m.pos - 1)
|
||||||
i--
|
i--
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) wordRight() {
|
// wordRight moves the cursor one word to the right. Returns whether or not the
|
||||||
|
// cursor blink should be reset.
|
||||||
|
func (m *Model) wordRight() (blink bool) {
|
||||||
if m.pos >= len(m.value) || len(m.value) == 0 {
|
if m.pos >= len(m.value) || len(m.value) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -271,8 +418,8 @@ 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++
|
blink = m.SetCursor(m.pos + 1)
|
||||||
i++
|
i++
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
@@ -280,147 +427,153 @@ 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++
|
blink = m.SetCursor(m.pos + 1)
|
||||||
i++
|
i++
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// BlinkMsg is sent when the cursor should alternate it's blinking state.
|
func (m Model) echoTransform(v string) string {
|
||||||
type BlinkMsg struct{}
|
switch m.EchoMode {
|
||||||
|
case EchoPassword:
|
||||||
|
return strings.Repeat(string(m.EchoCharacter), rw.StringWidth(v))
|
||||||
|
case EchoNone:
|
||||||
|
return ""
|
||||||
|
|
||||||
// NewModel creates a new model with default settings.
|
default:
|
||||||
func NewModel() Model {
|
return v
|
||||||
return Model{
|
|
||||||
Prompt: "> ",
|
|
||||||
BlinkSpeed: defaultBlinkSpeed,
|
|
||||||
Placeholder: "",
|
|
||||||
TextColor: "",
|
|
||||||
PlaceholderColor: "240",
|
|
||||||
CursorColor: "",
|
|
||||||
CharLimit: 0,
|
|
||||||
|
|
||||||
value: nil,
|
|
||||||
focus: false,
|
|
||||||
blink: true,
|
|
||||||
pos: 0,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is the Tea update loop.
|
// Update is the Bubble Tea update loop.
|
||||||
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||||
if !m.focus {
|
if !m.focus {
|
||||||
m.blink = true
|
m.blink = true
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var resetBlink bool
|
||||||
|
|
||||||
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
|
if msg.Alt {
|
||||||
case tea.KeyDelete:
|
resetBlink = m.deleteWordLeft()
|
||||||
if len(m.value) > 0 {
|
} else {
|
||||||
m.value = append(m.value[:max(0, m.pos-1)], m.value[m.pos:]...)
|
if len(m.value) > 0 {
|
||||||
if m.pos > 0 {
|
m.value = append(m.value[:max(0, m.pos-1)], m.value[m.pos:]...)
|
||||||
m.pos--
|
if m.pos > 0 {
|
||||||
|
resetBlink = m.SetCursor(m.pos - 1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case tea.KeyLeft:
|
case tea.KeyLeft, tea.KeyCtrlB:
|
||||||
if msg.Alt { // alt+left arrow, back one word
|
if msg.Alt { // alt+left arrow, back one word
|
||||||
m.wordLeft()
|
resetBlink = m.wordLeft()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if m.pos > 0 {
|
if m.pos > 0 { // left arrow, ^F, back one character
|
||||||
m.pos--
|
resetBlink = m.SetCursor(m.pos - 1)
|
||||||
}
|
}
|
||||||
case tea.KeyRight:
|
case tea.KeyRight, tea.KeyCtrlF:
|
||||||
if msg.Alt { // alt+right arrow, forward one word
|
if msg.Alt { // alt+right arrow, forward one word
|
||||||
m.wordRight()
|
resetBlink = m.wordRight()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if m.pos < len(m.value) {
|
if m.pos < len(m.value) { // right arrow, ^F, forward one word
|
||||||
m.pos++
|
resetBlink = m.SetCursor(m.pos + 1)
|
||||||
}
|
}
|
||||||
case tea.KeyCtrlF: // ^F, forward one character
|
case tea.KeyCtrlW: // ^W, delete word left of cursor
|
||||||
fallthrough
|
resetBlink = m.deleteWordLeft()
|
||||||
case tea.KeyCtrlB: // ^B, back one charcter
|
case tea.KeyHome, tea.KeyCtrlA: // ^A, go to beginning
|
||||||
fallthrough
|
resetBlink = m.CursorStart()
|
||||||
case tea.KeyCtrlA: // ^A, go to beginning
|
case tea.KeyDelete, tea.KeyCtrlD: // ^D, delete char under cursor
|
||||||
m.CursorStart()
|
|
||||||
case 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()
|
resetBlink = 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]
|
||||||
m.pos = len(m.value)
|
resetBlink = m.SetCursor(len(m.value))
|
||||||
case tea.KeyCtrlU: // ^U, kill text before cursor
|
case tea.KeyCtrlU: // ^U, kill text before cursor
|
||||||
m.value = m.value[m.pos:]
|
m.value = m.value[m.pos:]
|
||||||
m.pos = 0
|
resetBlink = m.SetCursor(0)
|
||||||
m.offset = 0
|
m.offset = 0
|
||||||
case tea.KeyCtrlV: // ^V paste
|
case tea.KeyCtrlV: // ^V paste
|
||||||
m.Paste()
|
return m, Paste
|
||||||
case tea.KeyRune: // input a regular character
|
case tea.KeyRunes: // input regular characters
|
||||||
|
if msg.Alt && len(msg.Runes) == 1 {
|
||||||
if msg.Alt {
|
if msg.Runes[0] == 'd' { // alt+d, delete word right of cursor
|
||||||
if msg.Rune == 'b' { // alt+b, back one word
|
resetBlink = m.deleteWordRight()
|
||||||
m.wordLeft()
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if msg.Rune == 'f' { // alt+f, forward one word
|
if msg.Runes[0] == 'b' { // alt+b, back one word
|
||||||
m.wordRight()
|
resetBlink = m.wordLeft()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if msg.Runes[0] == 'f' { // alt+f, forward one word
|
||||||
|
resetBlink = m.wordRight()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Input a regular character
|
// Input a regular character
|
||||||
if m.CharLimit <= 0 || len(m.value) < m.CharLimit {
|
if m.CharLimit <= 0 || len(m.value) < m.CharLimit {
|
||||||
m.value = append(m.value[:m.pos], append([]rune{msg.Rune}, m.value[m.pos:]...)...)
|
m.value = append(m.value[:m.pos], append(msg.Runes, m.value[m.pos:]...)...)
|
||||||
m.pos++
|
resetBlink = m.SetCursor(m.pos + len(msg.Runes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case ErrMsg:
|
case blinkMsg:
|
||||||
m.Err = msg
|
var cmd tea.Cmd
|
||||||
|
if m.cursorMode == cursorBlink {
|
||||||
|
m.blink = !m.blink
|
||||||
|
cmd = m.blinkCmd()
|
||||||
|
}
|
||||||
|
return m, cmd
|
||||||
|
|
||||||
case BlinkMsg:
|
case blinkCanceled: // no-op
|
||||||
m.blink = !m.blink
|
return m, nil
|
||||||
return m, Blink(m)
|
|
||||||
|
case pasteMsg:
|
||||||
|
resetBlink = m.handlePaste(string(msg))
|
||||||
|
|
||||||
|
case pasteErrMsg:
|
||||||
|
m.Err = msg
|
||||||
|
}
|
||||||
|
|
||||||
|
var cmd tea.Cmd
|
||||||
|
if resetBlink {
|
||||||
|
cmd = m.blinkCmd()
|
||||||
}
|
}
|
||||||
|
|
||||||
m.handleOverflow()
|
m.handleOverflow()
|
||||||
|
return m, cmd
|
||||||
return m, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// View renders the textinput in its current state.
|
// View renders the textinput in its current state.
|
||||||
func View(model tea.Model) string {
|
func (m Model) View() 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 m.placeholderView()
|
||||||
}
|
}
|
||||||
|
|
||||||
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 += m.cursorView(m.echoTransform(string(value[pos]))) // 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 += m.cursorView(" ")
|
||||||
}
|
}
|
||||||
|
|
||||||
// If a max width and background color were set fill the empty spaces with
|
// If a max width and background color were set fill the empty spaces with
|
||||||
@@ -440,8 +593,8 @@ func View(model tea.Model) string {
|
|||||||
return m.Prompt + v
|
return m.Prompt + v
|
||||||
}
|
}
|
||||||
|
|
||||||
// placeholderView
|
// placeholderView returns the prompt and placeholder view, if any.
|
||||||
func placeholderView(m Model) string {
|
func (m Model) placeholderView() string {
|
||||||
var (
|
var (
|
||||||
v string
|
v string
|
||||||
p = m.Placeholder
|
p = m.Placeholder
|
||||||
@@ -449,12 +602,9 @@ func placeholderView(m Model) string {
|
|||||||
|
|
||||||
// Cursor
|
// Cursor
|
||||||
if m.blink && m.PlaceholderColor != "" {
|
if m.blink && m.PlaceholderColor != "" {
|
||||||
v += cursorView(
|
v += m.cursorView(m.colorPlaceholder(p[:1]))
|
||||||
m.colorPlaceholder(p[:1]),
|
|
||||||
m,
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
v += cursorView(p[:1], m)
|
v += m.cursorView(p[:1])
|
||||||
}
|
}
|
||||||
|
|
||||||
// The rest of the placeholder text
|
// The rest of the placeholder text
|
||||||
@@ -464,29 +614,54 @@ func placeholderView(m Model) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// cursorView styles the cursor.
|
// cursorView styles the cursor.
|
||||||
func cursorView(s string, m Model) string {
|
func (m Model) cursorView(v string) string {
|
||||||
if m.blink {
|
if m.blink {
|
||||||
if m.TextColor != "" || m.BackgroundColor != "" {
|
if m.TextColor != "" || m.BackgroundColor != "" {
|
||||||
return termenv.String(s).
|
return termenv.String(v).
|
||||||
Foreground(color(m.TextColor)).
|
Foreground(color(m.TextColor)).
|
||||||
Background(color(m.BackgroundColor)).
|
Background(color(m.BackgroundColor)).
|
||||||
String()
|
String()
|
||||||
}
|
}
|
||||||
return s
|
return v
|
||||||
}
|
}
|
||||||
return termenv.String(s).
|
return termenv.String(v).
|
||||||
Foreground(color(m.CursorColor)).
|
Foreground(color(m.CursorColor)).
|
||||||
Background(color(m.BackgroundColor)).
|
Background(color(m.BackgroundColor)).
|
||||||
Reverse().
|
Reverse().
|
||||||
String()
|
String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blink is a command used to time the cursor blinking.
|
// blinkCmd is an internal command used to manage cursor blinking.
|
||||||
func Blink(model Model) tea.Cmd {
|
func (m Model) blinkCmd() tea.Cmd {
|
||||||
return func() tea.Msg {
|
if m.blinkCtx != nil && m.blinkCtx.cancel != nil {
|
||||||
time.Sleep(model.BlinkSpeed)
|
m.blinkCtx.cancel()
|
||||||
return BlinkMsg{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(m.blinkCtx.ctx, m.BlinkSpeed)
|
||||||
|
m.blinkCtx.cancel = cancel
|
||||||
|
|
||||||
|
return func() tea.Msg {
|
||||||
|
defer cancel()
|
||||||
|
<-ctx.Done()
|
||||||
|
if ctx.Err() == context.DeadlineExceeded {
|
||||||
|
return blinkMsg{}
|
||||||
|
}
|
||||||
|
return blinkCanceled{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Blink is a command used to initialize cursor blinking.
|
||||||
|
func Blink() tea.Msg {
|
||||||
|
return blinkMsg{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Paste is a command for pasting from the clipboard into the text input.
|
||||||
|
func Paste() tea.Msg {
|
||||||
|
str, err := clipboard.ReadAll()
|
||||||
|
if err != nil {
|
||||||
|
return pasteErrMsg{err}
|
||||||
|
}
|
||||||
|
return pasteMsg(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func clamp(v, low, high int) int {
|
func clamp(v, low, high int) int {
|
||||||
|
@@ -7,9 +7,7 @@ import (
|
|||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const spacebar = " "
|
||||||
spacebar = " "
|
|
||||||
)
|
|
||||||
|
|
||||||
// MODEL
|
// MODEL
|
||||||
|
|
||||||
@@ -73,11 +71,11 @@ 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)
|
||||||
bottom := min(len(m.lines), m.YOffset+m.Height)
|
bottom := clamp(m.YOffset+m.Height, top, len(m.lines))
|
||||||
lines = m.lines[top:bottom]
|
lines = m.lines[top:bottom]
|
||||||
}
|
}
|
||||||
return lines
|
return lines
|
||||||
@@ -125,7 +123,7 @@ func (m *Model) HalfViewDown() (lines []string) {
|
|||||||
|
|
||||||
if len(m.lines) > 0 {
|
if len(m.lines) > 0 {
|
||||||
top := max(m.YOffset+m.Height/2, 0)
|
top := max(m.YOffset+m.Height/2, 0)
|
||||||
bottom := min(m.YOffset+m.Height, len(m.lines)-1)
|
bottom := clamp(m.YOffset+m.Height, top, len(m.lines)-1)
|
||||||
lines = m.lines[top:bottom]
|
lines = m.lines[top:bottom]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,7 +143,7 @@ func (m *Model) HalfViewUp() (lines []string) {
|
|||||||
|
|
||||||
if len(m.lines) > 0 {
|
if len(m.lines) > 0 {
|
||||||
top := max(m.YOffset, 0)
|
top := max(m.YOffset, 0)
|
||||||
bottom := min(m.YOffset+m.Height/2, len(m.lines)-1)
|
bottom := clamp(m.YOffset+m.Height/2, top, len(m.lines)-1)
|
||||||
lines = m.lines[top:bottom]
|
lines = m.lines[top:bottom]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,7 +169,7 @@ func (m *Model) LineDown(n int) (lines []string) {
|
|||||||
|
|
||||||
if len(m.lines) > 0 {
|
if len(m.lines) > 0 {
|
||||||
top := max(m.YOffset+m.Height-n, 0)
|
top := max(m.YOffset+m.Height-n, 0)
|
||||||
bottom := min(m.YOffset+m.Height, len(m.lines)-1)
|
bottom := clamp(m.YOffset+m.Height, top, len(m.lines)-1)
|
||||||
lines = m.lines[top:bottom]
|
lines = m.lines[top:bottom]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,7 +191,7 @@ func (m *Model) LineUp(n int) (lines []string) {
|
|||||||
|
|
||||||
if len(m.lines) > 0 {
|
if len(m.lines) > 0 {
|
||||||
top := max(0, m.YOffset)
|
top := max(0, m.YOffset)
|
||||||
bottom := min(m.YOffset+n, len(m.lines)-1)
|
bottom := clamp(m.YOffset+n, top, len(m.lines)-1)
|
||||||
lines = m.lines[top:bottom]
|
lines = m.lines[top:bottom]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,7 +208,7 @@ func (m *Model) GotoTop() (lines []string) {
|
|||||||
|
|
||||||
if len(m.lines) > 0 {
|
if len(m.lines) > 0 {
|
||||||
top := m.YOffset
|
top := m.YOffset
|
||||||
bottom := min(m.YOffset+m.Height, len(m.lines)-1)
|
bottom := clamp(m.YOffset+m.Height, top, len(m.lines)-1)
|
||||||
lines = m.lines[top:bottom]
|
lines = m.lines[top:bottom]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -245,7 +243,7 @@ func Sync(m Model) tea.Cmd {
|
|||||||
// TODO: we should probably use m.visibleLines() rather than these two
|
// TODO: we should probably use m.visibleLines() rather than these two
|
||||||
// expressions.
|
// expressions.
|
||||||
top := max(m.YOffset, 0)
|
top := max(m.YOffset, 0)
|
||||||
bottom := min(m.YOffset+m.Height, len(m.lines)-1)
|
bottom := clamp(m.YOffset+m.Height, 0, len(m.lines)-1)
|
||||||
|
|
||||||
return tea.SyncScrollArea(
|
return tea.SyncScrollArea(
|
||||||
m.lines[top:bottom],
|
m.lines[top:bottom],
|
||||||
@@ -269,7 +267,7 @@ func ViewDown(m Model, lines []string) tea.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ViewUp is a high performance command the moves the viewport down by a given
|
// ViewUp is a high performance command the moves the viewport down by a given
|
||||||
// number of lines height. Use Model.ViewDown to get the lines that should be
|
// number of lines height. Use Model.ViewUp to get the lines that should be
|
||||||
// rendered.
|
// rendered.
|
||||||
func ViewUp(m Model, lines []string) tea.Cmd {
|
func ViewUp(m Model, lines []string) tea.Cmd {
|
||||||
if len(lines) == 0 {
|
if len(lines) == 0 {
|
||||||
@@ -283,11 +281,10 @@ func ViewUp(m Model, lines []string) tea.Cmd {
|
|||||||
// Update runs the update loop with default keybindings similar to popular
|
// Update runs the update loop with default keybindings similar to popular
|
||||||
// pagers. To define your own keybindings use the methods on Model (i.e.
|
// pagers. To define your own keybindings use the methods on Model (i.e.
|
||||||
// Model.LineDown()) and define your own update function.
|
// Model.LineDown()) and define your own update function.
|
||||||
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
func (m Model) Update(msg tea.Msg) (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
|
||||||
@@ -305,14 +302,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)
|
||||||
@@ -334,8 +331,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 {
|
||||||
@@ -356,8 +352,7 @@ func Update(msg tea.Msg, m Model) (Model, tea.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.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
|
||||||
@@ -379,6 +374,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
|
||||||
@@ -392,7 +391,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