mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-10-19 08:58:44 +03:00
Compare commits
13 Commits
feature/te
...
vim
Author | SHA1 | Date | |
---|---|---|---|
|
9eea57c1e8 | ||
|
e691e35a6d | ||
|
0f1a912f12 | ||
|
715108b08b | ||
|
3e1a14200c | ||
|
907f5e659d | ||
|
2cffee5f40 | ||
|
8c9798c7f6 | ||
|
fdfe776a07 | ||
|
fb44abddf8 | ||
|
447ff2da6a | ||
|
57a01e62e3 | ||
|
f5b74d002c |
22
.github/dependabot.yml
vendored
22
.github/dependabot.yml
vendored
@@ -1,22 +0,0 @@
|
|||||||
version: 2
|
|
||||||
updates:
|
|
||||||
- package-ecosystem: "gomod"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
||||||
time: "08:00"
|
|
||||||
labels:
|
|
||||||
- "dependencies"
|
|
||||||
commit-message:
|
|
||||||
prefix: "feat"
|
|
||||||
include: "scope"
|
|
||||||
- package-ecosystem: "github-actions"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
||||||
time: "08:00"
|
|
||||||
labels:
|
|
||||||
- "dependencies"
|
|
||||||
commit-message:
|
|
||||||
prefix: "chore"
|
|
||||||
include: "scope"
|
|
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@@ -11,12 +11,12 @@ jobs:
|
|||||||
GO111MODULE: "on"
|
GO111MODULE: "on"
|
||||||
steps:
|
steps:
|
||||||
- name: Install Go
|
- name: Install Go
|
||||||
uses: actions/setup-go@v3
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: ${{ matrix.go-version }}
|
go-version: ${{ matrix.go-version }}
|
||||||
|
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Download Go modules
|
- name: Download Go modules
|
||||||
run: go mod download
|
run: go mod download
|
||||||
|
4
.github/workflows/coverage.yml
vendored
4
.github/workflows/coverage.yml
vendored
@@ -12,12 +12,12 @@ jobs:
|
|||||||
GO111MODULE: "on"
|
GO111MODULE: "on"
|
||||||
steps:
|
steps:
|
||||||
- name: Install Go
|
- name: Install Go
|
||||||
uses: actions/setup-go@v3
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: ${{ matrix.go-version }}
|
go-version: ${{ matrix.go-version }}
|
||||||
|
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Coverage
|
- name: Coverage
|
||||||
env:
|
env:
|
||||||
|
28
.github/workflows/lint-soft.yml
vendored
28
.github/workflows/lint-soft.yml
vendored
@@ -1,28 +0,0 @@
|
|||||||
name: lint-soft
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
pull_request:
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
# Optional: allow read access to pull request. Use with `only-new-issues` option.
|
|
||||||
pull-requests: read
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
golangci:
|
|
||||||
name: lint-soft
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Install Go
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: ^1
|
|
||||||
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
- name: golangci-lint
|
|
||||||
uses: golangci/golangci-lint-action@v3
|
|
||||||
with:
|
|
||||||
# Optional: golangci-lint command line arguments.
|
|
||||||
args: --config .golangci-soft.yml --issues-exit-code=0
|
|
||||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
|
||||||
only-new-issues: true
|
|
22
.github/workflows/lint.yml
vendored
22
.github/workflows/lint.yml
vendored
@@ -1,28 +1,18 @@
|
|||||||
name: lint
|
name: lint
|
||||||
on:
|
on: [push, pull_request]
|
||||||
push:
|
|
||||||
pull_request:
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
# Optional: allow read access to pull request. Use with `only-new-issues` option.
|
|
||||||
pull-requests: read
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
golangci:
|
golangci:
|
||||||
name: lint
|
name: lint
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Install Go
|
- uses: actions/checkout@v2
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: ^1
|
|
||||||
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v3
|
uses: golangci/golangci-lint-action@v2
|
||||||
with:
|
with:
|
||||||
# Optional: golangci-lint command line arguments.
|
# Optional: golangci-lint command line arguments.
|
||||||
#args:
|
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`.
|
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||||
only-new-issues: true
|
only-new-issues: true
|
||||||
|
@@ -1,47 +0,0 @@
|
|||||||
run:
|
|
||||||
tests: false
|
|
||||||
|
|
||||||
issues:
|
|
||||||
include:
|
|
||||||
- EXC0001
|
|
||||||
- EXC0005
|
|
||||||
- EXC0011
|
|
||||||
- EXC0012
|
|
||||||
- EXC0013
|
|
||||||
|
|
||||||
max-issues-per-linter: 0
|
|
||||||
max-same-issues: 0
|
|
||||||
|
|
||||||
linters:
|
|
||||||
enable:
|
|
||||||
# - dupl
|
|
||||||
- exhaustive
|
|
||||||
# - exhaustivestruct
|
|
||||||
- goconst
|
|
||||||
- godot
|
|
||||||
- godox
|
|
||||||
- gomnd
|
|
||||||
- gomoddirectives
|
|
||||||
- goprintffuncname
|
|
||||||
- ifshort
|
|
||||||
# - lll
|
|
||||||
- misspell
|
|
||||||
- nakedret
|
|
||||||
- nestif
|
|
||||||
- noctx
|
|
||||||
- nolintlint
|
|
||||||
- prealloc
|
|
||||||
- wrapcheck
|
|
||||||
|
|
||||||
# disable default linters, they are already enabled in .golangci.yml
|
|
||||||
disable:
|
|
||||||
- deadcode
|
|
||||||
- errcheck
|
|
||||||
- gosimple
|
|
||||||
- govet
|
|
||||||
- ineffassign
|
|
||||||
- staticcheck
|
|
||||||
- structcheck
|
|
||||||
- typecheck
|
|
||||||
- unused
|
|
||||||
- varcheck
|
|
@@ -15,15 +15,20 @@ issues:
|
|||||||
linters:
|
linters:
|
||||||
enable:
|
enable:
|
||||||
- bodyclose
|
- bodyclose
|
||||||
|
- dupl
|
||||||
- exportloopref
|
- exportloopref
|
||||||
|
- goconst
|
||||||
|
- godot
|
||||||
|
- godox
|
||||||
- goimports
|
- goimports
|
||||||
|
- goprintffuncname
|
||||||
- gosec
|
- gosec
|
||||||
- nilerr
|
- ifshort
|
||||||
- predeclared
|
- misspell
|
||||||
|
- prealloc
|
||||||
- revive
|
- revive
|
||||||
- rowserrcheck
|
- rowserrcheck
|
||||||
- sqlclosecheck
|
- sqlclosecheck
|
||||||
- tparallel
|
|
||||||
- unconvert
|
- unconvert
|
||||||
- unparam
|
- unparam
|
||||||
- whitespace
|
- whitespace
|
||||||
|
64
README.md
64
README.md
@@ -26,8 +26,8 @@ applications. These components are used in production in [Glow][glow],
|
|||||||
A spinner, useful for indicating that some kind an operation is happening.
|
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.”
|
There are a couple default ones, but you can also pass your own ”frames.”
|
||||||
|
|
||||||
* [Example code, basic spinner](https://github.com/charmbracelet/bubbletea/tree/master/examples/spinner/main.go)
|
* [Example code, basic spinner](https://github.com/charmbracelet/tea/tree/master/examples/spinner/main.go)
|
||||||
* [Example code, various spinners](https://github.com/charmbracelet/bubbletea/tree/master/examples/spinners/main.go)
|
* [Example code, various spinners](https://github.com/charmbracelet/tea/tree/master/examples/spinners/main.go)
|
||||||
|
|
||||||
|
|
||||||
## Text Input
|
## Text Input
|
||||||
@@ -38,8 +38,8 @@ 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
|
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/bubbletea/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/bubbletea/tree/master/examples/textinputs/main.go)
|
* [Example code, many fields](https://github.com/charmbracelet/tea/tree/master/examples/textinputs/main.go)
|
||||||
|
|
||||||
## Text Area
|
## Text Area
|
||||||
|
|
||||||
@@ -50,21 +50,17 @@ spans multiple lines. Supports unicode, pasting, vertical scrolling when the
|
|||||||
value exceeds the width and height of the element, and many customization
|
value exceeds the width and height of the element, and many customization
|
||||||
options.
|
options.
|
||||||
|
|
||||||
* [Example code, chat input](https://github.com/charmbracelet/bubbletea/tree/master/examples/chat/main.go)
|
* [Example code, chat input](https://github.com/charmbracelet/tea/tree/master/examples/chat/main.go)
|
||||||
* [Example code, story time input](https://github.com/charmbracelet/bubbletea/tree/master/examples/textarea/main.go)
|
* [Example code, story time input](https://github.com/charmbracelet/tea/tree/master/examples/textarea/main.go)
|
||||||
|
|
||||||
## Table
|
|
||||||
|
|
||||||
<img src="https://stuff.charm.sh/bubbles-examples/table.gif" width="400" alt="Table Example">
|
|
||||||
|
|
||||||
A component for displaying and navigating tabular data (columns and rows).
|
|
||||||
Supports vertical scrolling and many customization options.
|
|
||||||
|
|
||||||
* [Example code, countries and populations](https://github.com/charmbracelet/bubbletea/tree/master/examples/table/main.go)
|
|
||||||
|
|
||||||
## Progress
|
## Progress
|
||||||
|
|
||||||
<img src="https://stuff.charm.sh/bubbles-examples/progress.gif" width="800" alt="Progressbar Example">
|
<picture>
|
||||||
|
<source media="(max-width: 800px)" srcset="https://stuff.charm.sh/bubbles-examples/progress.gif">
|
||||||
|
<source media="(min-width: 800px)" width="800" srcset="https://stuff.charm.sh/bubbles-examples/progress.gif">
|
||||||
|
<img src="https://stuff.charm.sh/bubbles-examples/progress.gif" alt="Progressbar Example">
|
||||||
|
</picture>
|
||||||
|
|
||||||
A simple, customizable progress meter, with optional animation via
|
A simple, customizable progress meter, with optional animation via
|
||||||
[Harmonica][harmonica]. Supports solid and gradient fills. The empty and filled
|
[Harmonica][harmonica]. Supports solid and gradient fills. The empty and filled
|
||||||
@@ -91,13 +87,17 @@ logic and visualize pagination however you like.
|
|||||||
|
|
||||||
## Viewport
|
## Viewport
|
||||||
|
|
||||||
<img src="https://stuff.charm.sh/bubbles-examples/viewport.gif" width="600" alt="Viewport Example">
|
<picture>
|
||||||
|
<source media="(max-width: 600px)" srcset="https://stuff.charm.sh/bubbles-examples/viewport.gif?0">
|
||||||
|
<source media="(min-width: 600px)" width="600" srcset="https://stuff.charm.sh/bubbles-examples/viewport.gif?0">
|
||||||
|
<img src="https://stuff.charm.sh/bubbles-examples/viewport.gif?0" alt="Viewport Example">
|
||||||
|
</picture>
|
||||||
|
|
||||||
A viewport for vertically scrolling content. Optionally includes standard
|
A viewport for vertically scrolling content. Optionally includes standard
|
||||||
pager keybindings and mouse wheel support. A high performance mode is available
|
pager keybindings and mouse wheel support. A high performance mode is available
|
||||||
for applications which make use of the alternate screen buffer.
|
for applications which make use of the alternate screen buffer.
|
||||||
|
|
||||||
* [Example code](https://github.com/charmbracelet/bubbletea/tree/master/examples/pager/main.go)
|
* [Example code](https://github.com/charmbracelet/tea/tree/master/examples/pager/main.go)
|
||||||
|
|
||||||
This component is well complemented with [Reflow][reflow] for ANSI-aware
|
This component is well complemented with [Reflow][reflow] for ANSI-aware
|
||||||
indenting and text wrapping.
|
indenting and text wrapping.
|
||||||
@@ -107,16 +107,20 @@ indenting and text wrapping.
|
|||||||
|
|
||||||
## List
|
## List
|
||||||
|
|
||||||
<img src="https://stuff.charm.sh/bubbles-examples/list.gif" width="600" alt="List Example">
|
<picture>
|
||||||
|
<source media="(max-width: 600px)" srcset="https://stuff.charm.sh/bubbles-examples/list.gif">
|
||||||
|
<source media="(min-width: 600px)" width="600" srcset="https://stuff.charm.sh/bubbles-examples/list.gif">
|
||||||
|
<img src="https://stuff.charm.sh/bubbles-examples/list.gif" alt="List Example">
|
||||||
|
</picture>
|
||||||
|
|
||||||
A customizable, batteries-included component for browsing a set of items.
|
A customizable, batteries-included component for browsing a set of items.
|
||||||
Features pagination, fuzzy filtering, auto-generated help, an activity spinner,
|
Features pagination, fuzzy filtering, auto-generated help, an activity spinner,
|
||||||
and status messages, all of which can be enabled and disabled as needed.
|
and status messages, all of which can be enabled and disabled as needed.
|
||||||
Extrapolated from [Glow][glow].
|
Extrapolated from [Glow][glow].
|
||||||
|
|
||||||
* [Example code, default list](https://github.com/charmbracelet/bubbletea/tree/master/examples/list-default/main.go)
|
* [Example code, default list](https://github.com/charmbracelet/tea/tree/master/examples/list-default/main.go)
|
||||||
* [Example code, simple list](https://github.com/charmbracelet/bubbletea/tree/master/examples/list-simple/main.go)
|
* [Example code, simple list](https://github.com/charmbracelet/tea/tree/master/examples/list-simple/main.go)
|
||||||
* [Example code, all features](https://github.com/charmbracelet/bubbletea/tree/master/examples/list-fancy/main.go)
|
* [Example code, all features](https://github.com/charmbracelet/tea/tree/master/examples/list-fancy/main.go)
|
||||||
|
|
||||||
|
|
||||||
## Timer
|
## Timer
|
||||||
@@ -141,7 +145,11 @@ can be customized as you see fit.
|
|||||||
|
|
||||||
## Help
|
## Help
|
||||||
|
|
||||||
<img src="https://stuff.charm.sh/bubbles-examples/help.gif" width="500" alt="Help Example">
|
<picture>
|
||||||
|
<source media="(max-width: 500px)" srcset="https://stuff.charm.sh/bubbles-examples/help.gif">
|
||||||
|
<source media="(min-width: 500px)" width="500" srcset="https://stuff.charm.sh/bubbles-examples/help.gif">
|
||||||
|
<img src="https://stuff.charm.sh/bubbles-examples/help.gif" alt="Help Example">
|
||||||
|
</picture>
|
||||||
|
|
||||||
A customizable horizontal mini help view that automatically generates itself
|
A customizable horizontal mini help view that automatically generates itself
|
||||||
from your keybindings. It features single and multi-line modes, which the user
|
from your keybindings. It features single and multi-line modes, which the user
|
||||||
@@ -217,17 +225,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
If you’ve built a Bubble you think should be listed here,
|
If you’ve built a Bubble you think should be listed here,
|
||||||
[let us know](mailto:vt100@charm.sh).
|
[let us know](mailto:vt100@charm.sh).
|
||||||
|
|
||||||
## Feedback
|
|
||||||
|
|
||||||
We’d love to hear your thoughts on this project. Feel free to drop us a note!
|
|
||||||
|
|
||||||
* [Twitter](https://twitter.com/charmcli)
|
|
||||||
* [The Fediverse](https://mastodon.social/@charmcli)
|
|
||||||
* [Discord](https://charm.sh/chat)
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
[MIT](https://github.com/charmbracelet/bubbletea/raw/master/LICENSE)
|
[MIT](https://github.com/charmbracelet/teaparty/raw/master/LICENSE)
|
||||||
|
|
||||||
|
|
||||||
***
|
***
|
||||||
|
|
||||||
@@ -236,3 +238,5 @@ Part of [Charm](https://charm.sh).
|
|||||||
<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge.jpg" width="400"></a>
|
<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热爱开源 • Charm loves open source
|
||||||
|
|
||||||
|
[charm]: https://charm.sh/
|
||||||
|
@@ -70,8 +70,8 @@ type Model struct {
|
|||||||
blinkCtx *blinkCtx
|
blinkCtx *blinkCtx
|
||||||
// The ID of the blink message we're expecting to receive.
|
// The ID of the blink message we're expecting to receive.
|
||||||
blinkTag int
|
blinkTag int
|
||||||
// mode determines the behavior of the cursor
|
// cursorMode determines the behavior of the cursor
|
||||||
mode Mode
|
cursorMode Mode
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new model with default settings.
|
// New creates a new model with default settings.
|
||||||
@@ -80,7 +80,7 @@ func New() Model {
|
|||||||
BlinkSpeed: defaultBlinkSpeed,
|
BlinkSpeed: defaultBlinkSpeed,
|
||||||
|
|
||||||
Blink: true,
|
Blink: true,
|
||||||
mode: CursorBlink,
|
cursorMode: CursorBlink,
|
||||||
|
|
||||||
blinkCtx: &blinkCtx{
|
blinkCtx: &blinkCtx{
|
||||||
ctx: context.Background(),
|
ctx: context.Background(),
|
||||||
@@ -94,7 +94,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|||||||
case initialBlinkMsg:
|
case initialBlinkMsg:
|
||||||
// We accept all initialBlinkMsgs generated by the Blink command.
|
// We accept all initialBlinkMsgs generated by the Blink command.
|
||||||
|
|
||||||
if m.mode != CursorBlink || !m.focus {
|
if m.cursorMode != CursorBlink || !m.focus {
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|||||||
// only exactly when it should.
|
// only exactly when it should.
|
||||||
|
|
||||||
// Is this model blink-able?
|
// Is this model blink-able?
|
||||||
if m.mode != CursorBlink || !m.focus {
|
if m.cursorMode != CursorBlink || !m.focus {
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,7 +116,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var cmd tea.Cmd
|
var cmd tea.Cmd
|
||||||
if m.mode == CursorBlink {
|
if m.cursorMode == CursorBlink {
|
||||||
m.Blink = !m.Blink
|
m.Blink = !m.Blink
|
||||||
cmd = m.BlinkCmd()
|
cmd = m.BlinkCmd()
|
||||||
}
|
}
|
||||||
@@ -128,18 +128,18 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mode returns the model's cursor mode. For available cursor modes, see
|
// CursorMode returns the model's cursor mode. For available cursor modes, see
|
||||||
// type Mode.
|
// type CursorMode.
|
||||||
func (m Model) Mode() Mode {
|
func (m Model) CursorMode() Mode {
|
||||||
return m.mode
|
return m.cursorMode
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetMode sets the model's cursor mode. This method returns a command.
|
// SetCursorMode sets the model's cursor mode. This method returns a command.
|
||||||
//
|
//
|
||||||
// For available cursor modes, see type CursorMode.
|
// For available cursor modes, see type CursorMode.
|
||||||
func (m *Model) SetMode(mode Mode) tea.Cmd {
|
func (m *Model) SetCursorMode(mode Mode) tea.Cmd {
|
||||||
m.mode = mode
|
m.cursorMode = mode
|
||||||
m.Blink = m.mode == CursorHide || !m.focus
|
m.Blink = m.cursorMode == CursorHide || !m.focus
|
||||||
if mode == CursorBlink {
|
if mode == CursorBlink {
|
||||||
return Blink
|
return Blink
|
||||||
}
|
}
|
||||||
@@ -148,7 +148,7 @@ func (m *Model) SetMode(mode Mode) tea.Cmd {
|
|||||||
|
|
||||||
// BlinkCmd is an command used to manage cursor blinking.
|
// BlinkCmd is an command used to manage cursor blinking.
|
||||||
func (m *Model) BlinkCmd() tea.Cmd {
|
func (m *Model) BlinkCmd() tea.Cmd {
|
||||||
if m.mode != CursorBlink {
|
if m.cursorMode != CursorBlink {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,9 +179,9 @@ func Blink() tea.Msg {
|
|||||||
// Focus focuses the cursor to allow it to blink if desired.
|
// Focus focuses the cursor to allow it to blink if desired.
|
||||||
func (m *Model) Focus() tea.Cmd {
|
func (m *Model) Focus() tea.Cmd {
|
||||||
m.focus = true
|
m.focus = true
|
||||||
m.Blink = m.mode == CursorHide // show the cursor unless we've explicitly hidden it
|
m.Blink = m.cursorMode == CursorHide // show the cursor unless we've explicitly hidden it
|
||||||
|
|
||||||
if m.mode == CursorBlink && m.focus {
|
if m.cursorMode == CursorBlink && m.focus {
|
||||||
return m.BlinkCmd()
|
return m.BlinkCmd()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -201,7 +201,7 @@ func (m *Model) SetChar(char string) {
|
|||||||
// View displays the cursor.
|
// View displays the cursor.
|
||||||
func (m Model) View() string {
|
func (m Model) View() string {
|
||||||
if m.Blink {
|
if m.Blink {
|
||||||
return m.TextStyle.Inline(true).Render(m.char)
|
return m.TextStyle.Render(m.char)
|
||||||
}
|
}
|
||||||
return m.Style.Inline(true).Reverse(true).Render(m.char)
|
return m.Style.Inline(true).Reverse(true).Render(m.char)
|
||||||
}
|
}
|
||||||
|
8
go.mod
8
go.mod
@@ -1,15 +1,15 @@
|
|||||||
module github.com/Maks1mS/bubbles
|
module github.com/charmbracelet/bubbles
|
||||||
|
|
||||||
go 1.13
|
go 1.13
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/atotto/clipboard v0.1.4
|
github.com/atotto/clipboard v0.1.4
|
||||||
github.com/charmbracelet/bubbletea v0.22.1
|
github.com/charmbracelet/bubbletea v0.21.0
|
||||||
github.com/charmbracelet/harmonica v0.2.0
|
github.com/charmbracelet/harmonica v0.2.0
|
||||||
github.com/charmbracelet/lipgloss v0.6.0
|
github.com/charmbracelet/lipgloss v0.5.0
|
||||||
github.com/kylelemons/godebug v1.1.0 // indirect
|
github.com/kylelemons/godebug v1.1.0 // indirect
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0
|
github.com/lucasb-eyer/go-colorful v1.2.0
|
||||||
github.com/mattn/go-runewidth v0.0.14
|
github.com/mattn/go-runewidth v0.0.13
|
||||||
github.com/muesli/reflow v0.3.0
|
github.com/muesli/reflow v0.3.0
|
||||||
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739
|
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739
|
||||||
github.com/sahilm/fuzzy v0.1.0
|
github.com/sahilm/fuzzy v0.1.0
|
||||||
|
30
go.sum
30
go.sum
@@ -1,31 +1,27 @@
|
|||||||
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
|
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
|
||||||
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
||||||
github.com/charmbracelet/bubbletea v0.22.1 h1:z66q0LWdJNOWEH9zadiAIXp2GN1AWrwNXU8obVY9X24=
|
github.com/charmbracelet/bubbletea v0.21.0 h1:f3y+kanzgev5PA916qxmDybSHU3N804uOnKnhRPXTcI=
|
||||||
github.com/charmbracelet/bubbletea v0.22.1/go.mod h1:8/7hVvbPN6ZZPkczLiB8YpLkLJ0n7DMho5Wvfd2X1C0=
|
github.com/charmbracelet/bubbletea v0.21.0/go.mod h1:GgmJMec61d08zXsOhqRC/AiOx4K4pmz+VIcRIm1FKr4=
|
||||||
github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=
|
github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=
|
||||||
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
|
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
|
||||||
github.com/charmbracelet/lipgloss v0.6.0 h1:1StyZB9vBSOyuZxQUcUwGr17JmojPNm87inij9N3wJY=
|
github.com/charmbracelet/lipgloss v0.5.0 h1:lulQHuVeodSgDez+3rGiuxlPVXSnhth442DATR2/8t8=
|
||||||
github.com/charmbracelet/lipgloss v0.6.0/go.mod h1:tHh2wr34xcHjC2HCXIlGSG1jaDF0S0atAUvBMP6Ppuk=
|
github.com/charmbracelet/lipgloss v0.5.0/go.mod h1:EZLha/HbzEt7cYqdFPovlqy5FZPj0xFhg5SaqxScmgs=
|
||||||
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
|
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
|
||||||
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
|
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
|
||||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
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/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
|
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||||
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
|
|
||||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
|
||||||
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
|
|
||||||
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
|
|
||||||
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||||
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||||
|
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
|
||||||
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||||
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
|
|
||||||
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
|
||||||
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
|
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
|
||||||
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
|
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
|
||||||
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
github.com/muesli/cancelreader v0.2.0 h1:SOpr+CfyVNce341kKqvbhhzQhBPyJRXQaCtn03Pae1Q=
|
||||||
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
github.com/muesli/cancelreader v0.2.0/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
||||||
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
|
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
|
||||||
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
|
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
|
||||||
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
|
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
|
||||||
@@ -33,7 +29,10 @@ github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ
|
|||||||
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 h1:QANkGiGr39l1EESqrE0gZw0/AJNYzIvoGLhIoVYtluI=
|
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 h1:QANkGiGr39l1EESqrE0gZw0/AJNYzIvoGLhIoVYtluI=
|
||||||
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
|
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
|
||||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
|
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 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||||
|
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||||
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
|
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
|
||||||
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
|
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
|
||||||
@@ -41,10 +40,7 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
|
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
|
||||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
|
||||||
|
@@ -3,7 +3,7 @@ package help
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Maks1mS/bubbles/key"
|
"github.com/charmbracelet/bubbles/key"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
)
|
)
|
||||||
@@ -203,7 +203,7 @@ func (m Model) FullHelpView(groups [][]key.Binding) string {
|
|||||||
|
|
||||||
// Column
|
// Column
|
||||||
totalWidth += lipgloss.Width(col)
|
totalWidth += lipgloss.Width(col)
|
||||||
if m.Width > 0 && totalWidth > m.Width {
|
if totalWidth > m.Width {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,7 +212,7 @@ func (m Model) FullHelpView(groups [][]key.Binding) string {
|
|||||||
// Separator
|
// Separator
|
||||||
if i < len(group)-1 {
|
if i < len(group)-1 {
|
||||||
totalWidth += sepWidth
|
totalWidth += sepWidth
|
||||||
if m.Width > 0 && totalWidth > m.Width {
|
if totalWidth > m.Width {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5,7 +5,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Maks1mS/bubbles/key"
|
"github.com/charmbracelet/bubbles/key"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/muesli/reflow/truncate"
|
"github.com/muesli/reflow/truncate"
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
package list
|
package list
|
||||||
|
|
||||||
import "github.com/Maks1mS/bubbles/key"
|
import "github.com/charmbracelet/bubbles/key"
|
||||||
|
|
||||||
// KeyMap defines keybindings. It satisfies to the help.KeyMap interface, which
|
// KeyMap defines keybindings. It satisfies to the help.KeyMap interface, which
|
||||||
// is used to render the menu menu.
|
// is used to render the menu menu.
|
||||||
|
19
list/list.go
19
list/list.go
@@ -10,11 +10,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Maks1mS/bubbles/help"
|
"github.com/charmbracelet/bubbles/help"
|
||||||
"github.com/Maks1mS/bubbles/key"
|
"github.com/charmbracelet/bubbles/key"
|
||||||
"github.com/Maks1mS/bubbles/paginator"
|
"github.com/charmbracelet/bubbles/paginator"
|
||||||
"github.com/Maks1mS/bubbles/spinner"
|
"github.com/charmbracelet/bubbles/spinner"
|
||||||
"github.com/Maks1mS/bubbles/textinput"
|
"github.com/charmbracelet/bubbles/textinput"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/muesli/reflow/ansi"
|
"github.com/muesli/reflow/ansi"
|
||||||
@@ -183,18 +183,18 @@ type Model struct {
|
|||||||
func New(items []Item, delegate ItemDelegate, width, height int) Model {
|
func New(items []Item, delegate ItemDelegate, width, height int) Model {
|
||||||
styles := DefaultStyles()
|
styles := DefaultStyles()
|
||||||
|
|
||||||
sp := spinner.New()
|
sp := spinner.NewModel()
|
||||||
sp.Spinner = spinner.Line
|
sp.Spinner = spinner.Line
|
||||||
sp.Style = styles.Spinner
|
sp.Style = styles.Spinner
|
||||||
|
|
||||||
filterInput := textinput.New()
|
filterInput := textinput.NewModel()
|
||||||
filterInput.Prompt = "Filter: "
|
filterInput.Prompt = "Filter: "
|
||||||
filterInput.PromptStyle = styles.FilterPrompt
|
filterInput.PromptStyle = styles.FilterPrompt
|
||||||
filterInput.CursorStyle = styles.FilterCursor
|
filterInput.CursorStyle = styles.FilterCursor
|
||||||
filterInput.CharLimit = 64
|
filterInput.CharLimit = 64
|
||||||
filterInput.Focus()
|
filterInput.Focus()
|
||||||
|
|
||||||
p := paginator.New()
|
p := paginator.NewModel()
|
||||||
p.Type = paginator.Dots
|
p.Type = paginator.Dots
|
||||||
p.ActiveDot = styles.ActivePaginationDot.String()
|
p.ActiveDot = styles.ActivePaginationDot.String()
|
||||||
p.InactiveDot = styles.InactivePaginationDot.String()
|
p.InactiveDot = styles.InactivePaginationDot.String()
|
||||||
@@ -221,7 +221,7 @@ func New(items []Item, delegate ItemDelegate, width, height int) Model {
|
|||||||
items: items,
|
items: items,
|
||||||
Paginator: p,
|
Paginator: p,
|
||||||
spinner: sp,
|
spinner: sp,
|
||||||
Help: help.New(),
|
Help: help.NewModel(),
|
||||||
}
|
}
|
||||||
|
|
||||||
m.updatePagination()
|
m.updatePagination()
|
||||||
@@ -538,6 +538,7 @@ func (m Model) SettingFilter() bool {
|
|||||||
// It's purely a convenience method for the following:
|
// It's purely a convenience method for the following:
|
||||||
//
|
//
|
||||||
// m.FilterState() == FilterApplied
|
// m.FilterState() == FilterApplied
|
||||||
|
//
|
||||||
func (m Model) IsFiltered() bool {
|
func (m Model) IsFiltered() bool {
|
||||||
return m.filterState == FilterApplied
|
return m.filterState == FilterApplied
|
||||||
}
|
}
|
||||||
|
@@ -7,7 +7,6 @@ package paginator
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/Maks1mS/bubbles/key"
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,48 +19,19 @@ const (
|
|||||||
Dots
|
Dots
|
||||||
)
|
)
|
||||||
|
|
||||||
// KeyMap is the key bindings for different actions within the paginator.
|
|
||||||
type KeyMap struct {
|
|
||||||
PrevPage key.Binding
|
|
||||||
NextPage key.Binding
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultKeyMap is the default set of key bindings for navigating and acting
|
|
||||||
// upon the paginator.
|
|
||||||
var DefaultKeyMap = KeyMap{
|
|
||||||
PrevPage: key.NewBinding(key.WithKeys("pgup", "left", "h")),
|
|
||||||
NextPage: key.NewBinding(key.WithKeys("pgdown", "right", "l")),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Model is the Bubble Tea model for this user interface.
|
// Model is the Bubble Tea model for this user interface.
|
||||||
type Model struct {
|
type Model struct {
|
||||||
// Type configures how the pagination is rendered (Arabic, Dots).
|
|
||||||
Type Type
|
Type Type
|
||||||
// Page is the current page number.
|
|
||||||
Page int
|
Page int
|
||||||
// PerPage is the number of items per page.
|
|
||||||
PerPage int
|
PerPage int
|
||||||
// TotalPages is the total number of pages.
|
|
||||||
TotalPages int
|
TotalPages int
|
||||||
// ActiveDot is used to mark the current page under the Dots display type.
|
|
||||||
ActiveDot string
|
ActiveDot string
|
||||||
// InactiveDot is used to mark inactive pages under the Dots display type.
|
|
||||||
InactiveDot string
|
InactiveDot string
|
||||||
// ArabicFormat is the printf-style format to use for the Arabic display type.
|
|
||||||
ArabicFormat string
|
ArabicFormat string
|
||||||
|
|
||||||
// KeyMap encodes the keybindings recognized by the widget.
|
|
||||||
KeyMap KeyMap
|
|
||||||
|
|
||||||
// Deprecated: customize KeyMap instead.
|
|
||||||
UsePgUpPgDownKeys bool
|
UsePgUpPgDownKeys bool
|
||||||
// Deprecated: customize KeyMap instead.
|
|
||||||
UseLeftRightKeys bool
|
UseLeftRightKeys bool
|
||||||
// Deprecated: customize KeyMap instead.
|
|
||||||
UseUpDownKeys bool
|
UseUpDownKeys bool
|
||||||
// Deprecated: customize KeyMap instead.
|
|
||||||
UseHLKeys bool
|
UseHLKeys bool
|
||||||
// Deprecated: customize KeyMap instead.
|
|
||||||
UseJKKeys bool
|
UseJKKeys bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,6 +68,7 @@ func (m Model) ItemsOnPage(totalItems int) int {
|
|||||||
// bunchOfStuff := []stuff{...}
|
// bunchOfStuff := []stuff{...}
|
||||||
// start, end := model.GetSliceBounds(len(bunchOfStuff))
|
// start, end := model.GetSliceBounds(len(bunchOfStuff))
|
||||||
// sliceToRender := bunchOfStuff[start:end]
|
// sliceToRender := bunchOfStuff[start:end]
|
||||||
|
//
|
||||||
func (m *Model) GetSliceBounds(length int) (start int, end int) {
|
func (m *Model) GetSliceBounds(length int) (start int, end int) {
|
||||||
start = m.Page * m.PerPage
|
start = m.Page * m.PerPage
|
||||||
end = min(m.Page*m.PerPage+m.PerPage, length)
|
end = min(m.Page*m.PerPage+m.PerPage, length)
|
||||||
@@ -132,10 +103,14 @@ func New() Model {
|
|||||||
Page: 0,
|
Page: 0,
|
||||||
PerPage: 1,
|
PerPage: 1,
|
||||||
TotalPages: 1,
|
TotalPages: 1,
|
||||||
KeyMap: DefaultKeyMap,
|
|
||||||
ActiveDot: "•",
|
ActiveDot: "•",
|
||||||
InactiveDot: "○",
|
InactiveDot: "○",
|
||||||
ArabicFormat: "%d/%d",
|
ArabicFormat: "%d/%d",
|
||||||
|
UsePgUpPgDownKeys: true,
|
||||||
|
UseLeftRightKeys: true,
|
||||||
|
UseUpDownKeys: false,
|
||||||
|
UseHLKeys: true,
|
||||||
|
UseJKKeys: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,11 +123,45 @@ var NewModel = New
|
|||||||
func (m Model) Update(msg tea.Msg) (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:
|
||||||
switch {
|
if m.UsePgUpPgDownKeys {
|
||||||
case key.Matches(msg, m.KeyMap.NextPage):
|
switch msg.String() {
|
||||||
m.NextPage()
|
case "pgup":
|
||||||
case key.Matches(msg, m.KeyMap.PrevPage):
|
|
||||||
m.PrevPage()
|
m.PrevPage()
|
||||||
|
case "pgdown":
|
||||||
|
m.NextPage()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if m.UseLeftRightKeys {
|
||||||
|
switch msg.String() {
|
||||||
|
case "left":
|
||||||
|
m.PrevPage()
|
||||||
|
case "right":
|
||||||
|
m.NextPage()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if m.UseUpDownKeys {
|
||||||
|
switch msg.String() {
|
||||||
|
case "up":
|
||||||
|
m.PrevPage()
|
||||||
|
case "down":
|
||||||
|
m.NextPage()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if m.UseHLKeys {
|
||||||
|
switch msg.String() {
|
||||||
|
case "h":
|
||||||
|
m.PrevPage()
|
||||||
|
case "l":
|
||||||
|
m.NextPage()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if m.UseJKKeys {
|
||||||
|
switch msg.String() {
|
||||||
|
case "j":
|
||||||
|
m.PrevPage()
|
||||||
|
case "k":
|
||||||
|
m.NextPage()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -37,12 +37,13 @@ const (
|
|||||||
defaultDamping = 1.0
|
defaultDamping = 1.0
|
||||||
)
|
)
|
||||||
|
|
||||||
// Option is used to set options in New. For example:
|
// Option is used to set options in NewModel. For example:
|
||||||
//
|
//
|
||||||
// progress := New(
|
// progress := NewModel(
|
||||||
// WithRamp("#ff0000", "#0000ff"),
|
// WithRamp("#ff0000", "#0000ff"),
|
||||||
// WithoutPercentage(),
|
// WithoutPercentage(),
|
||||||
// )
|
// )
|
||||||
|
//
|
||||||
type Option func(*Model)
|
type Option func(*Model)
|
||||||
|
|
||||||
// WithDefaultGradient sets a gradient fill with default colors.
|
// WithDefaultGradient sets a gradient fill with default colors.
|
||||||
|
@@ -85,7 +85,7 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Model contains the state for the spinner. Use New 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 {
|
||||||
// Spinner settings to use. See type Spinner.
|
// Spinner settings to use. See type Spinner.
|
||||||
@@ -209,6 +209,7 @@ func Tick() tea.Msg {
|
|||||||
// Option is used to set options in New. For example:
|
// Option is used to set options in New. For example:
|
||||||
//
|
//
|
||||||
// spinner := New(WithSpinner(Dot))
|
// spinner := New(WithSpinner(Dot))
|
||||||
|
//
|
||||||
type Option func(*Model)
|
type Option func(*Model)
|
||||||
|
|
||||||
// WithSpinner is an option to set the spinner.
|
// WithSpinner is an option to set the spinner.
|
||||||
|
@@ -3,7 +3,7 @@ package spinner_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/Maks1mS/bubbles/spinner"
|
"github.com/charmbracelet/bubbles/spinner"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSpinnerNew(t *testing.T) {
|
func TestSpinnerNew(t *testing.T) {
|
||||||
|
@@ -3,8 +3,8 @@ package table
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Maks1mS/bubbles/key"
|
"github.com/charmbracelet/bubbles/key"
|
||||||
"github.com/Maks1mS/bubbles/viewport"
|
"github.com/charmbracelet/bubbles/viewport"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/mattn/go-runewidth"
|
"github.com/mattn/go-runewidth"
|
||||||
@@ -59,11 +59,11 @@ func DefaultKeyMap() KeyMap {
|
|||||||
),
|
),
|
||||||
PageUp: key.NewBinding(
|
PageUp: key.NewBinding(
|
||||||
key.WithKeys("b", "pgup"),
|
key.WithKeys("b", "pgup"),
|
||||||
key.WithHelp("b/pgup", "page up"),
|
key.WithHelp("b", "page up"),
|
||||||
),
|
),
|
||||||
PageDown: key.NewBinding(
|
PageDown: key.NewBinding(
|
||||||
key.WithKeys("f", "pgdown", spacebar),
|
key.WithKeys("f", "pgup", spacebar),
|
||||||
key.WithHelp("f/pgdn", "page down"),
|
key.WithHelp("f", "page down"),
|
||||||
),
|
),
|
||||||
HalfPageUp: key.NewBinding(
|
HalfPageUp: key.NewBinding(
|
||||||
key.WithKeys("u", "ctrl+u"),
|
key.WithKeys("u", "ctrl+u"),
|
||||||
@@ -110,6 +110,7 @@ func (m *Model) SetStyles(s Styles) {
|
|||||||
// Option is used to set options in New. For example:
|
// Option is used to set options in New. For example:
|
||||||
//
|
//
|
||||||
// table := New(WithColumns([]Column{{Title: "ID", Width: 10}}))
|
// table := New(WithColumns([]Column{{Title: "ID", Width: 10}}))
|
||||||
|
//
|
||||||
type Option func(*Model)
|
type Option func(*Model)
|
||||||
|
|
||||||
// New creates a new model for the table widget.
|
// New creates a new model for the table widget.
|
||||||
@@ -290,12 +291,6 @@ func (m Model) Cursor() int {
|
|||||||
return m.cursor
|
return m.cursor
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetCursor sets the cursor position in the table.
|
|
||||||
func (m *Model) SetCursor(n int) {
|
|
||||||
m.cursor = clamp(n, 0, len(m.rows)-1)
|
|
||||||
m.UpdateViewport()
|
|
||||||
}
|
|
||||||
|
|
||||||
// MoveUp moves the selection up by any number of row.
|
// MoveUp moves the selection up by any number of row.
|
||||||
// It can not go above the first row.
|
// It can not go above the first row.
|
||||||
func (m *Model) MoveUp(n int) {
|
func (m *Model) MoveUp(n int) {
|
||||||
@@ -345,7 +340,7 @@ func (m *Model) FromValues(value, separator string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m Model) headersView() string {
|
func (m Model) headersView() string {
|
||||||
var s = make([]string, 0, len(m.cols))
|
var s = make([]string, len(m.cols))
|
||||||
for _, col := range m.cols {
|
for _, col := range m.cols {
|
||||||
style := lipgloss.NewStyle().Width(col.Width).MaxWidth(col.Width).Inline(true)
|
style := lipgloss.NewStyle().Width(col.Width).MaxWidth(col.Width).Inline(true)
|
||||||
renderedCell := style.Render(runewidth.Truncate(col.Title, col.Width, "…"))
|
renderedCell := style.Render(runewidth.Truncate(col.Title, col.Width, "…"))
|
||||||
@@ -355,7 +350,7 @@ func (m Model) headersView() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) renderRow(rowID int) string {
|
func (m *Model) renderRow(rowID int) string {
|
||||||
var s = make([]string, 0, len(m.cols))
|
var s = make([]string, len(m.cols))
|
||||||
for i, value := range m.rows[rowID] {
|
for i, value := range m.rows[rowID] {
|
||||||
style := lipgloss.NewStyle().Width(m.cols[i].Width).MaxWidth(m.cols[i].Width).Inline(true)
|
style := lipgloss.NewStyle().Width(m.cols[i].Width).MaxWidth(m.cols[i].Width).Inline(true)
|
||||||
renderedCell := m.styles.Cell.Render(style.Render(runewidth.Truncate(value, m.cols[i].Width, "…")))
|
renderedCell := m.styles.Cell.Render(style.Render(runewidth.Truncate(value, m.cols[i].Width, "…")))
|
||||||
|
791
textarea/modal.go
Normal file
791
textarea/modal.go
Normal file
@@ -0,0 +1,791 @@
|
|||||||
|
package textarea
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/bubbles/cursor"
|
||||||
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
rw "github.com/mattn/go-runewidth"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Mode is the possible modes of the textarea for modal editing.
|
||||||
|
type Mode string
|
||||||
|
|
||||||
|
const (
|
||||||
|
// ModeNormal is the normal mode for navigating around text.
|
||||||
|
ModeNormal Mode = "normal"
|
||||||
|
// ModeInsert is the insert mode for inserting text.
|
||||||
|
ModeInsert Mode = "insert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SetMode sets the mode of the textarea.
|
||||||
|
func (m *Model) SetMode(mode Mode) tea.Cmd {
|
||||||
|
switch mode {
|
||||||
|
case ModeInsert:
|
||||||
|
m.mode = ModeInsert
|
||||||
|
return m.Cursor.SetCursorMode(cursor.CursorBlink)
|
||||||
|
case ModeNormal:
|
||||||
|
m.mode = ModeNormal
|
||||||
|
m.col = clamp(m.col-1, 0, len(m.value[m.row]))
|
||||||
|
return m.Cursor.SetCursorMode(cursor.CursorStatic)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Action is the type of action that will be performed when the user completes
|
||||||
|
// a key combination.
|
||||||
|
type Action int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// ActionMove moves the cursor.
|
||||||
|
ActionMove Action = iota
|
||||||
|
// ActionReplace replaces text.
|
||||||
|
ActionReplace
|
||||||
|
// ActionDelete deletes text.
|
||||||
|
ActionDelete
|
||||||
|
// ActionYank yanks text.
|
||||||
|
ActionYank
|
||||||
|
// ActionChange deletes text and enters insert mode.
|
||||||
|
ActionChange
|
||||||
|
)
|
||||||
|
|
||||||
|
// Position is a (row, column) pair representing a position of the cursor or
|
||||||
|
// any character.
|
||||||
|
type Position struct {
|
||||||
|
Row int
|
||||||
|
Col int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Range is a range of characters in the text area.
|
||||||
|
type Range struct {
|
||||||
|
Start Position
|
||||||
|
End Position
|
||||||
|
}
|
||||||
|
|
||||||
|
// NormalCommand is a helper for keeping track of the various relevant information
|
||||||
|
// when performing vim motions in the textarea.
|
||||||
|
type NormalCommand struct {
|
||||||
|
// Buffer is the buffer of keys that have been press for the current
|
||||||
|
// command.
|
||||||
|
Buffer string
|
||||||
|
// Count is the number of times to replay the action. This is usually
|
||||||
|
// optional and defaults to 1.
|
||||||
|
Count int
|
||||||
|
// Action is the action to be performed.
|
||||||
|
Action Action
|
||||||
|
// Range is the range of characters to perform the action on.
|
||||||
|
Range Range
|
||||||
|
// Seeking is the type of seeking that is in progress.
|
||||||
|
Seeking SeekType
|
||||||
|
}
|
||||||
|
|
||||||
|
// SeekType are the possible types of seeking that can be in progress.
|
||||||
|
type SeekType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// SeekNone is the default seeking action.
|
||||||
|
SeekNone SeekType = iota
|
||||||
|
// SeekForwardTo is the seeking action for f.
|
||||||
|
SeekForwardTo // f
|
||||||
|
// SeekBackwardTo is the seeking action for F.
|
||||||
|
SeekBackwardTo // F
|
||||||
|
// SeekForwardUntil is the seeking action for t.
|
||||||
|
SeekForwardUntil // t
|
||||||
|
// SeekBackwardUntil is the seeking action for T.
|
||||||
|
SeekBackwardUntil // T
|
||||||
|
// SeekAround is the seeking action for a.
|
||||||
|
SeekAround // a
|
||||||
|
// SeekInner is the seeking action for i.
|
||||||
|
SeekInner // i
|
||||||
|
)
|
||||||
|
|
||||||
|
// IsSeeking returns whether the command is in the middle of seeking a range.
|
||||||
|
func (n *NormalCommand) IsSeeking() bool {
|
||||||
|
return n.Seeking != SeekNone
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSeekingForward returns whether the seeking action is in the forward direction (f, t).
|
||||||
|
func (n *NormalCommand) IsSeekingForward() bool {
|
||||||
|
return n.Seeking == SeekForwardTo || n.Seeking == SeekForwardUntil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSeekingBackward returns whether the seeking action is in the backward direction (F, T).
|
||||||
|
func (n *NormalCommand) IsSeekingBackward() bool {
|
||||||
|
return n.Seeking == SeekBackwardTo || n.Seeking == SeekBackwardUntil
|
||||||
|
}
|
||||||
|
|
||||||
|
// executeMsg executes a command.
|
||||||
|
type executeMsg NormalCommand
|
||||||
|
|
||||||
|
// executeCmd implements tea.Cmd for an executeMsg.
|
||||||
|
func executeCmd(n NormalCommand) tea.Cmd {
|
||||||
|
return func() tea.Msg {
|
||||||
|
return executeMsg(n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Model) insertUpdate(msg tea.Msg) tea.Cmd {
|
||||||
|
switch msg := msg.(type) {
|
||||||
|
case tea.KeyMsg:
|
||||||
|
switch msg.String() {
|
||||||
|
case "esc":
|
||||||
|
return m.SetMode(ModeNormal)
|
||||||
|
case "ctrl+k":
|
||||||
|
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
||||||
|
if m.col >= len(m.value[m.row]) {
|
||||||
|
m.mergeLineBelow(m.row)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
m.deleteAfterCursor()
|
||||||
|
case "ctrl+u":
|
||||||
|
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
||||||
|
if m.col <= 0 {
|
||||||
|
m.mergeLineAbove(m.row)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
m.deleteBeforeCursor()
|
||||||
|
case "backspace", "ctrl+h":
|
||||||
|
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
||||||
|
if m.col <= 0 {
|
||||||
|
m.mergeLineAbove(m.row)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if len(m.value[m.row]) > 0 {
|
||||||
|
m.value[m.row] = append(m.value[m.row][:max(0, m.col-1)], m.value[m.row][m.col:]...)
|
||||||
|
if m.col > 0 {
|
||||||
|
m.SetCursor(m.col - 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "delete", "ctrl+d":
|
||||||
|
if len(m.value[m.row]) > 0 && m.col < len(m.value[m.row]) {
|
||||||
|
m.value[m.row] = append(m.value[m.row][:m.col], m.value[m.row][m.col+1:]...)
|
||||||
|
}
|
||||||
|
if m.col >= len(m.value[m.row]) {
|
||||||
|
m.mergeLineBelow(m.row)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case "alt+backspace", "ctrl+w":
|
||||||
|
if m.col <= 0 {
|
||||||
|
m.mergeLineAbove(m.row)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
m.deleteWordLeft()
|
||||||
|
case "alt+delete", "alt+d":
|
||||||
|
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
||||||
|
if m.col >= len(m.value[m.row]) {
|
||||||
|
m.mergeLineBelow(m.row)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
m.deleteWordRight()
|
||||||
|
case "enter", "ctrl+m":
|
||||||
|
if len(m.value) >= maxHeight {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
||||||
|
m.splitLine(m.row, m.col)
|
||||||
|
case "end", "ctrl+e":
|
||||||
|
m.CursorEnd()
|
||||||
|
case "home", "ctrl+a":
|
||||||
|
m.CursorStart()
|
||||||
|
case "right", "ctrl+f":
|
||||||
|
if m.col < len(m.value[m.row]) {
|
||||||
|
m.SetCursor(m.col + 1)
|
||||||
|
} else {
|
||||||
|
if m.row < len(m.value)-1 {
|
||||||
|
m.row++
|
||||||
|
m.CursorStart()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "down", "ctrl+n":
|
||||||
|
m.CursorDown()
|
||||||
|
case "alt+right", "alt+f":
|
||||||
|
m.wordRight()
|
||||||
|
case "ctrl+v":
|
||||||
|
return Paste
|
||||||
|
case "left", "ctrl+b":
|
||||||
|
if m.col == 0 && m.row != 0 {
|
||||||
|
m.row--
|
||||||
|
m.CursorEnd()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if m.col > 0 {
|
||||||
|
m.SetCursor(m.col - 1)
|
||||||
|
}
|
||||||
|
case "up", "ctrl+p":
|
||||||
|
m.CursorUp()
|
||||||
|
case "alt+left", "alt+b":
|
||||||
|
m.wordLeft()
|
||||||
|
default:
|
||||||
|
if m.CharLimit > 0 && rw.StringWidth(m.Value()) >= m.CharLimit {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
m.col = min(m.col, len(m.value[m.row]))
|
||||||
|
m.value[m.row] = append(m.value[m.row][:m.col], append(msg.Runes, m.value[m.row][m.col:]...)...)
|
||||||
|
m.SetCursor(m.col + len(msg.Runes))
|
||||||
|
}
|
||||||
|
|
||||||
|
case pasteMsg:
|
||||||
|
m.handlePaste(string(msg))
|
||||||
|
|
||||||
|
case pasteErrMsg:
|
||||||
|
m.Err = msg
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Model) normalUpdate(msg tea.Msg) tea.Cmd {
|
||||||
|
switch msg := msg.(type) {
|
||||||
|
case tea.KeyMsg:
|
||||||
|
if m.command.IsSeeking() {
|
||||||
|
return m.seekingUpdate(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.command.Action == ActionReplace {
|
||||||
|
for i := m.col; i < m.col+max(m.command.Count, 1); i++ {
|
||||||
|
if i >= len(m.value[m.row]) || len(msg.Runes) <= 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
m.value[m.row][i] = msg.Runes[0]
|
||||||
|
}
|
||||||
|
m.command = &NormalCommand{}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.ContainsAny(msg.String(), "jk") {
|
||||||
|
m.lastCharOffset = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
switch msg.String() {
|
||||||
|
case "esc":
|
||||||
|
m.command = &NormalCommand{}
|
||||||
|
return m.SetMode(ModeNormal)
|
||||||
|
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
|
||||||
|
if m.command.Count == 0 && msg.String() == "0" {
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{Row: m.row, Col: m.col},
|
||||||
|
End: Position{Row: m.row, Col: 0},
|
||||||
|
}
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
}
|
||||||
|
|
||||||
|
v := m.command.Buffer + msg.String()
|
||||||
|
count, err := strconv.Atoi(v)
|
||||||
|
if err != nil {
|
||||||
|
count, _ = strconv.Atoi(msg.String())
|
||||||
|
m.command.Buffer = msg.String()
|
||||||
|
m.command.Count = count
|
||||||
|
} else {
|
||||||
|
m.command.Buffer = v
|
||||||
|
m.command.Count = count
|
||||||
|
}
|
||||||
|
case "o":
|
||||||
|
m.CursorEnd()
|
||||||
|
m.splitLine(m.row, m.col)
|
||||||
|
return m.SetMode(ModeInsert)
|
||||||
|
case "O":
|
||||||
|
m.CursorStart()
|
||||||
|
m.splitLine(m.row, m.col)
|
||||||
|
m.CursorUp()
|
||||||
|
return m.SetMode(ModeInsert)
|
||||||
|
case "G":
|
||||||
|
var row int
|
||||||
|
if m.command.Count > 0 {
|
||||||
|
row = m.command.Count - 1
|
||||||
|
} else {
|
||||||
|
row = len(m.value) - 1
|
||||||
|
}
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{Row: row, Col: m.col},
|
||||||
|
End: Position{Row: clamp(row, 0, len(m.value)-1), Col: m.col},
|
||||||
|
}
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
case "g":
|
||||||
|
if m.command.Buffer == "g" {
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{Row: 0, Col: m.col},
|
||||||
|
End: Position{Row: 0, Col: clamp(m.col, 0, len(m.value[0]))},
|
||||||
|
}
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
}
|
||||||
|
m.command.Buffer = "g"
|
||||||
|
case "x":
|
||||||
|
m.command.Action = ActionDelete
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{Row: m.row, Col: m.col},
|
||||||
|
End: Position{Row: m.row, Col: m.col + max(m.command.Count, 1)},
|
||||||
|
}
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
case "X":
|
||||||
|
m.command.Action = ActionDelete
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{Row: m.row, Col: m.col},
|
||||||
|
End: Position{Row: m.row, Col: m.col - max(m.command.Count, 1)},
|
||||||
|
}
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
case "c":
|
||||||
|
if m.command.Action == ActionChange {
|
||||||
|
m.CursorStart()
|
||||||
|
m.deleteAfterCursor()
|
||||||
|
m.command = &NormalCommand{}
|
||||||
|
return m.SetMode(ModeInsert)
|
||||||
|
}
|
||||||
|
m.command.Action = ActionChange
|
||||||
|
case "d":
|
||||||
|
if m.command.Action == ActionDelete {
|
||||||
|
for i := 0; i < max(m.command.Count, 1); i++ {
|
||||||
|
m.value[m.row] = []rune{}
|
||||||
|
if m.row < len(m.value)-1 {
|
||||||
|
m.mergeLineBelow(m.row)
|
||||||
|
} else {
|
||||||
|
m.mergeLineAbove(m.row)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m.command = &NormalCommand{}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
m.command.Action = ActionDelete
|
||||||
|
case "y":
|
||||||
|
m.command.Action = ActionYank
|
||||||
|
case "t", "T", "f", "F":
|
||||||
|
m.command.Buffer = msg.String()
|
||||||
|
switch msg.String() {
|
||||||
|
case "t":
|
||||||
|
m.command.Seeking = SeekForwardUntil
|
||||||
|
case "T":
|
||||||
|
m.command.Seeking = SeekBackwardUntil
|
||||||
|
case "f":
|
||||||
|
m.command.Seeking = SeekForwardTo
|
||||||
|
case "F":
|
||||||
|
m.command.Seeking = SeekBackwardTo
|
||||||
|
}
|
||||||
|
case "r":
|
||||||
|
m.command.Action = ActionReplace
|
||||||
|
case "i":
|
||||||
|
if m.command.Action != ActionMove {
|
||||||
|
m.command.Buffer = "i"
|
||||||
|
m.command.Seeking = SeekInner
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{Row: m.row, Col: m.col},
|
||||||
|
End: Position{Row: m.row, Col: m.col},
|
||||||
|
}
|
||||||
|
return tea.Sequentially(executeCmd(*m.command), m.SetMode(ModeInsert))
|
||||||
|
case "I":
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{Row: m.row, Col: m.col},
|
||||||
|
End: Position{Row: m.row, Col: 0},
|
||||||
|
}
|
||||||
|
return tea.Sequentially(executeCmd(*m.command), m.SetMode(ModeInsert))
|
||||||
|
case "a":
|
||||||
|
if m.command.Action != ActionMove {
|
||||||
|
m.command.Buffer = "a"
|
||||||
|
m.command.Seeking = SeekAround
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{Row: m.row, Col: m.col},
|
||||||
|
End: Position{Row: m.row, Col: m.col + 1},
|
||||||
|
}
|
||||||
|
return tea.Sequentially(executeCmd(*m.command), m.SetMode(ModeInsert))
|
||||||
|
case "A":
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{Row: m.row, Col: m.col},
|
||||||
|
End: Position{Row: m.row, Col: len(m.value[m.row]) + 1},
|
||||||
|
}
|
||||||
|
return tea.Sequentially(executeCmd(*m.command), m.SetMode(ModeInsert))
|
||||||
|
case "^":
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{m.row, m.col},
|
||||||
|
End: Position{m.row, 0},
|
||||||
|
}
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
case "$":
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{m.row, m.col},
|
||||||
|
End: Position{m.row, len(m.value[m.row])},
|
||||||
|
}
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
case "e", "E":
|
||||||
|
end := m.findWordEndRight(max(m.command.Count, 1), msg.String() == "E")
|
||||||
|
if m.command.Action == ActionDelete || m.command.Action == ActionChange {
|
||||||
|
end.Col = min(end.Col+1, len(m.value[end.Row]))
|
||||||
|
}
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{m.row, m.col},
|
||||||
|
End: end,
|
||||||
|
}
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
case "w", "W":
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{m.row, m.col},
|
||||||
|
End: m.findWordStartRight(max(m.command.Count, 1), msg.String() == "W"),
|
||||||
|
}
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
case "b", "B":
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{m.row, m.col},
|
||||||
|
End: m.findWordLeft(max(m.command.Count, 1), msg.String() == "B"),
|
||||||
|
}
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
case "h", "l":
|
||||||
|
direction := 1
|
||||||
|
if msg.String() == "h" {
|
||||||
|
direction = -1
|
||||||
|
}
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{m.row, m.col},
|
||||||
|
End: Position{m.row, clamp(m.col+(direction*max(m.command.Count, 1)), 0, len(m.value[m.row]))},
|
||||||
|
}
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
case "j", "k":
|
||||||
|
direction := 1
|
||||||
|
if msg.String() == "k" {
|
||||||
|
direction = -1
|
||||||
|
}
|
||||||
|
row := clamp(m.row+(direction*max(m.command.Count, 1)), 0, len(m.value)-1)
|
||||||
|
li := m.LineInfo()
|
||||||
|
charOffset := max(m.lastCharOffset, li.CharOffset)
|
||||||
|
m.lastCharOffset = charOffset
|
||||||
|
|
||||||
|
rowContent := m.value[row]
|
||||||
|
charWidth := rw.StringWidth(string(rowContent))
|
||||||
|
|
||||||
|
col := 0
|
||||||
|
offset := 0
|
||||||
|
|
||||||
|
for offset < charOffset {
|
||||||
|
if col > len(m.value[row]) || offset >= charWidth-1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
offset += rw.RuneWidth(m.value[row][col])
|
||||||
|
col++
|
||||||
|
}
|
||||||
|
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{m.row, m.col},
|
||||||
|
End: Position{row, col},
|
||||||
|
}
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
case "C":
|
||||||
|
m.deleteAfterCursor()
|
||||||
|
m.command = &NormalCommand{}
|
||||||
|
return m.SetMode(ModeInsert)
|
||||||
|
case "D":
|
||||||
|
m.deleteAfterCursor()
|
||||||
|
m.command = &NormalCommand{}
|
||||||
|
return nil
|
||||||
|
case "J":
|
||||||
|
m.CursorEnd()
|
||||||
|
m.mergeLineBelow(m.row)
|
||||||
|
m.command = &NormalCommand{}
|
||||||
|
return nil
|
||||||
|
case "p":
|
||||||
|
return Paste
|
||||||
|
}
|
||||||
|
case executeMsg:
|
||||||
|
switch m.command.Action {
|
||||||
|
case ActionDelete:
|
||||||
|
m.deleteRange(m.command.Range)
|
||||||
|
case ActionChange:
|
||||||
|
m.deleteRange(m.command.Range)
|
||||||
|
return m.SetMode(ModeInsert)
|
||||||
|
case ActionMove:
|
||||||
|
row := clamp(m.command.Range.End.Row, 0, len(m.value)-1)
|
||||||
|
m.row = row
|
||||||
|
m.col = clamp(m.command.Range.End.Col, 0, len(m.value[row]))
|
||||||
|
}
|
||||||
|
m.command = &NormalCommand{}
|
||||||
|
|
||||||
|
case pasteMsg:
|
||||||
|
m.handlePaste(string(msg))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Model) seekingUpdate(msg tea.KeyMsg) tea.Cmd {
|
||||||
|
if len(msg.Runes) <= 0 {
|
||||||
|
m.command = &NormalCommand{}
|
||||||
|
}
|
||||||
|
if msg.Runes[0] == 'w' || msg.Runes[0] == 'W' {
|
||||||
|
m.command.Range = m.findPairRange(msg.Runes[0])
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
}
|
||||||
|
switch m.command.Buffer {
|
||||||
|
case "a":
|
||||||
|
m.command.Range = m.findPairRange(msg.Runes[0])
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
case "i":
|
||||||
|
pr := m.findPairRange(msg.Runes[0])
|
||||||
|
|
||||||
|
pr.Start.Col++
|
||||||
|
pr.End.Col--
|
||||||
|
|
||||||
|
m.command.Range = pr
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
case "f":
|
||||||
|
end := m.findCharRight(msg.Runes[0])
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{m.row, m.col},
|
||||||
|
End: end,
|
||||||
|
}
|
||||||
|
case "F":
|
||||||
|
start := m.findCharLeft(msg.Runes[0])
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: start,
|
||||||
|
End: Position{m.row, m.col},
|
||||||
|
}
|
||||||
|
case "t":
|
||||||
|
end := m.findCharRight(msg.Runes[0])
|
||||||
|
end.Col--
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: Position{m.row, m.col},
|
||||||
|
End: end,
|
||||||
|
}
|
||||||
|
case "T":
|
||||||
|
start := m.findCharLeft(msg.Runes[0])
|
||||||
|
start.Col++
|
||||||
|
m.command.Range = Range{
|
||||||
|
Start: start,
|
||||||
|
End: Position{m.row, m.col},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return executeCmd(*m.command)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Model) findCharLeft(r rune) Position {
|
||||||
|
col := m.col
|
||||||
|
|
||||||
|
for col > 0 {
|
||||||
|
col--
|
||||||
|
if m.value[m.row][col] == r {
|
||||||
|
return Position{m.row, col}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Position{m.row, m.col}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Model) findCharRight(r rune) Position {
|
||||||
|
col := m.col
|
||||||
|
|
||||||
|
for col < len(m.value[m.row]) {
|
||||||
|
if m.value[m.row][col] == r {
|
||||||
|
return Position{m.row, col}
|
||||||
|
}
|
||||||
|
col++
|
||||||
|
}
|
||||||
|
return Position{m.row, m.col}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Model) findPairRange(r rune) Range {
|
||||||
|
var startRune, endRune rune
|
||||||
|
|
||||||
|
switch r {
|
||||||
|
case 'w', 'W':
|
||||||
|
return Range{
|
||||||
|
Start: m.findWordLeft(0, r == 'W'),
|
||||||
|
End: m.findWordEndRight(1, true),
|
||||||
|
}
|
||||||
|
case '(', ')':
|
||||||
|
startRune, endRune = '(', ')'
|
||||||
|
case '{', '}':
|
||||||
|
startRune, endRune = '{', '}'
|
||||||
|
case '[', ']':
|
||||||
|
startRune, endRune = '[', ']'
|
||||||
|
case '<', '>':
|
||||||
|
startRune, endRune = '<', '>'
|
||||||
|
case '"', '\'':
|
||||||
|
startRune, endRune = r, r
|
||||||
|
default:
|
||||||
|
m.command = &NormalCommand{}
|
||||||
|
return Range{
|
||||||
|
Start: Position{m.row, m.col},
|
||||||
|
End: Position{m.row, m.col},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Range{
|
||||||
|
Start: m.findCharLeft(startRune),
|
||||||
|
End: m.findCharRight(endRune),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// findWordLeft locates the start of the word on the left of the current word.
|
||||||
|
// It takes whether or not to break words on spaces or any non-alpha-numeric
|
||||||
|
// character as an argument.
|
||||||
|
func (m *Model) findWordLeft(count int, ignorePunctuation bool) Position {
|
||||||
|
wordBreak := isSoftWordBreak
|
||||||
|
if ignorePunctuation {
|
||||||
|
wordBreak = isWordBreak
|
||||||
|
}
|
||||||
|
|
||||||
|
if count == 0 {
|
||||||
|
if wordBreak(m.value[m.row][m.col-1]) {
|
||||||
|
return Position{m.row, m.col}
|
||||||
|
}
|
||||||
|
count = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
row, col := m.row, m.col
|
||||||
|
|
||||||
|
for count > 0 {
|
||||||
|
if col <= 0 && row > 0 {
|
||||||
|
row--
|
||||||
|
col = len(m.value[row]) - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip all spaces (and punctuation) to the left of the cursor.
|
||||||
|
for col > 0 && wordBreak(m.value[row][col-1]) {
|
||||||
|
col--
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then skip all non-spaces to the left of the cursor.
|
||||||
|
for col > 0 && !wordBreak(m.value[row][col-1]) {
|
||||||
|
col--
|
||||||
|
}
|
||||||
|
|
||||||
|
count--
|
||||||
|
|
||||||
|
if row <= 0 && col <= 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Position{Row: row, Col: col}
|
||||||
|
}
|
||||||
|
|
||||||
|
// findWordStartRight locates the start of the next word. It takes whether or not to
|
||||||
|
// break words on spaces or any non-alpha-numeric character as an argument.
|
||||||
|
func (m *Model) findWordStartRight(count int, ignorePunctuation bool) Position {
|
||||||
|
wordBreak := isSoftWordBreak
|
||||||
|
if ignorePunctuation {
|
||||||
|
wordBreak = isWordBreak
|
||||||
|
}
|
||||||
|
|
||||||
|
row, col := m.row, m.col
|
||||||
|
|
||||||
|
for count > 0 {
|
||||||
|
if col >= len(m.value[row])-1 && row < len(m.value)-1 {
|
||||||
|
row++
|
||||||
|
col = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip until the start of a word is found.
|
||||||
|
for col < len(m.value[row]) && !wordBreak(m.value[row][col]) {
|
||||||
|
col++
|
||||||
|
}
|
||||||
|
// Skip all spaces to the right of the cursor.
|
||||||
|
for col < len(m.value[row])-1 && wordBreak(m.value[row][col]) {
|
||||||
|
col++
|
||||||
|
}
|
||||||
|
count--
|
||||||
|
|
||||||
|
if row >= len(m.value)-1 && col >= len(m.value[row])-1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Position{Row: row, Col: col}
|
||||||
|
}
|
||||||
|
|
||||||
|
// findWordEndRight locates the start of the next word. It takes whether or not to
|
||||||
|
// break words on spaces or any non-alpha-numeric character as an argument.
|
||||||
|
func (m *Model) findWordEndRight(count int, ignorePunctuation bool) Position {
|
||||||
|
wordBreak := isSoftWordBreak
|
||||||
|
if ignorePunctuation {
|
||||||
|
wordBreak = isWordBreak
|
||||||
|
}
|
||||||
|
|
||||||
|
row, col := m.row, m.col
|
||||||
|
|
||||||
|
for count > 0 {
|
||||||
|
if col > len(m.value[row]) && row < len(m.value)-1 {
|
||||||
|
row++
|
||||||
|
col = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip all spaces to the right of the cursor.
|
||||||
|
for col < len(m.value[row])-1 && wordBreak(m.value[row][col+1]) {
|
||||||
|
col++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then skip all non-spaces to the right of the cursor.
|
||||||
|
for col < len(m.value[row])-1 && !wordBreak(m.value[row][col+1]) {
|
||||||
|
col++
|
||||||
|
}
|
||||||
|
|
||||||
|
count--
|
||||||
|
|
||||||
|
if row <= 0 && col <= 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Position{Row: row, Col: col}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isWordBreak(char rune) bool {
|
||||||
|
return unicode.IsSpace(char)
|
||||||
|
}
|
||||||
|
|
||||||
|
func isSoftWordBreak(char rune) bool {
|
||||||
|
return unicode.IsSpace(char) || unicode.IsPunct(char)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Model) deleteRange(r Range) {
|
||||||
|
if r.Start.Row == r.End.Row && r.Start.Col == r.End.Col {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
minCol, maxCol := min(r.Start.Col, r.End.Col), max(r.Start.Col, r.End.Col)
|
||||||
|
|
||||||
|
minCol = clamp(minCol, 0, len(m.value[r.Start.Row]))
|
||||||
|
maxCol = clamp(maxCol, 0, len(m.value[r.Start.Row]))
|
||||||
|
|
||||||
|
if r.Start.Row == r.End.Row {
|
||||||
|
// If the action is delete and from a seek action, we need to delete
|
||||||
|
// the range inclusively.
|
||||||
|
if m.command.IsSeeking() {
|
||||||
|
if m.command.IsSeekingForward() {
|
||||||
|
maxCol = min(maxCol+1, len(m.value[r.Start.Row]))
|
||||||
|
} else if m.command.IsSeekingBackward() {
|
||||||
|
minCol = max(minCol-1, 0)
|
||||||
|
} else if m.command.Seeking == SeekAround || m.command.Seeking == SeekInner {
|
||||||
|
maxCol = min(maxCol+1, len(m.value[r.Start.Row]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m.value[r.Start.Row] = append(m.value[r.Start.Row][:minCol], m.value[r.Start.Row][maxCol:]...)
|
||||||
|
m.SetCursor(minCol)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
minRow, maxRow := min(r.Start.Row, r.End.Row), max(r.Start.Row, r.End.Row)
|
||||||
|
|
||||||
|
if maxRow-minRow == 1 {
|
||||||
|
if r.Start.Row < r.End.Row {
|
||||||
|
m.value[r.Start.Row] = append([]rune{}, m.value[r.Start.Row][:r.Start.Col]...)
|
||||||
|
m.value[r.End.Row] = append([]rune{}, m.value[r.End.Row][r.End.Col:]...)
|
||||||
|
m.mergeLineBelow(minRow)
|
||||||
|
} else {
|
||||||
|
m.value[r.Start.Row] = append([]rune{}, m.value[r.Start.Row][r.Start.Col:]...)
|
||||||
|
m.value[r.End.Row] = append([]rune{}, m.value[r.End.Row][:r.End.Col]...)
|
||||||
|
m.mergeLineAbove(maxRow)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := max(minRow, 0); i <= min(maxRow, len(m.value)-1); i++ {
|
||||||
|
m.value[i] = []rune{}
|
||||||
|
}
|
||||||
|
|
||||||
|
m.value = append(m.value[:minRow], m.value[maxRow:]...)
|
||||||
|
|
||||||
|
m.row = clamp(0, minRow, len(m.value))
|
||||||
|
}
|
@@ -5,10 +5,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/Maks1mS/bubbles/cursor"
|
|
||||||
"github.com/Maks1mS/bubbles/key"
|
|
||||||
"github.com/Maks1mS/bubbles/viewport"
|
|
||||||
"github.com/atotto/clipboard"
|
"github.com/atotto/clipboard"
|
||||||
|
"github.com/charmbracelet/bubbles/cursor"
|
||||||
|
"github.com/charmbracelet/bubbles/viewport"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
rw "github.com/mattn/go-runewidth"
|
rw "github.com/mattn/go-runewidth"
|
||||||
@@ -28,63 +27,6 @@ const (
|
|||||||
type pasteMsg string
|
type pasteMsg string
|
||||||
type pasteErrMsg struct{ error }
|
type pasteErrMsg struct{ error }
|
||||||
|
|
||||||
// KeyMap is the key bindings for different actions within the textarea.
|
|
||||||
type KeyMap struct {
|
|
||||||
CharacterBackward key.Binding
|
|
||||||
CharacterForward key.Binding
|
|
||||||
DeleteAfterCursor key.Binding
|
|
||||||
DeleteBeforeCursor key.Binding
|
|
||||||
DeleteCharacterBackward key.Binding
|
|
||||||
DeleteCharacterForward key.Binding
|
|
||||||
DeleteWordBackward key.Binding
|
|
||||||
DeleteWordForward key.Binding
|
|
||||||
InsertNewline key.Binding
|
|
||||||
LineEnd key.Binding
|
|
||||||
LineNext key.Binding
|
|
||||||
LinePrevious key.Binding
|
|
||||||
LineStart key.Binding
|
|
||||||
Paste key.Binding
|
|
||||||
WordBackward key.Binding
|
|
||||||
WordForward key.Binding
|
|
||||||
InputBegin key.Binding
|
|
||||||
InputEnd key.Binding
|
|
||||||
|
|
||||||
UppercaseWordForward key.Binding
|
|
||||||
LowercaseWordForward key.Binding
|
|
||||||
CapitalizeWordForward key.Binding
|
|
||||||
|
|
||||||
TransposeCharacterBackward key.Binding
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultKeyMap is the default set of key bindings for navigating and acting
|
|
||||||
// upon the textarea.
|
|
||||||
var DefaultKeyMap = KeyMap{
|
|
||||||
CharacterForward: key.NewBinding(key.WithKeys("right", "ctrl+f")),
|
|
||||||
CharacterBackward: key.NewBinding(key.WithKeys("left", "ctrl+b")),
|
|
||||||
WordForward: key.NewBinding(key.WithKeys("alt+right", "alt+f")),
|
|
||||||
WordBackward: key.NewBinding(key.WithKeys("alt+left", "alt+b")),
|
|
||||||
LineNext: key.NewBinding(key.WithKeys("down", "ctrl+n")),
|
|
||||||
LinePrevious: key.NewBinding(key.WithKeys("up", "ctrl+p")),
|
|
||||||
DeleteWordBackward: key.NewBinding(key.WithKeys("alt+backspace", "ctrl+w")),
|
|
||||||
DeleteWordForward: key.NewBinding(key.WithKeys("alt+delete", "alt+d")),
|
|
||||||
DeleteAfterCursor: key.NewBinding(key.WithKeys("ctrl+k")),
|
|
||||||
DeleteBeforeCursor: key.NewBinding(key.WithKeys("ctrl+u")),
|
|
||||||
InsertNewline: key.NewBinding(key.WithKeys("enter", "ctrl+m")),
|
|
||||||
DeleteCharacterBackward: key.NewBinding(key.WithKeys("backspace", "ctrl+h")),
|
|
||||||
DeleteCharacterForward: key.NewBinding(key.WithKeys("delete", "ctrl+d")),
|
|
||||||
LineStart: key.NewBinding(key.WithKeys("home", "ctrl+a")),
|
|
||||||
LineEnd: key.NewBinding(key.WithKeys("end", "ctrl+e")),
|
|
||||||
Paste: key.NewBinding(key.WithKeys("ctrl+v")),
|
|
||||||
InputBegin: key.NewBinding(key.WithKeys("alt+<", "ctrl+home")),
|
|
||||||
InputEnd: key.NewBinding(key.WithKeys("alt+>", "ctrl+end")),
|
|
||||||
|
|
||||||
CapitalizeWordForward: key.NewBinding(key.WithKeys("alt+c")),
|
|
||||||
LowercaseWordForward: key.NewBinding(key.WithKeys("alt+l")),
|
|
||||||
UppercaseWordForward: key.NewBinding(key.WithKeys("alt+u")),
|
|
||||||
|
|
||||||
TransposeCharacterBackward: key.NewBinding(key.WithKeys("ctrl+t")),
|
|
||||||
}
|
|
||||||
|
|
||||||
// LineInfo is a helper for keeping track of line information regarding
|
// LineInfo is a helper for keeping track of line information regarding
|
||||||
// soft-wrapped lines.
|
// soft-wrapped lines.
|
||||||
type LineInfo struct {
|
type LineInfo struct {
|
||||||
@@ -133,29 +75,11 @@ type Model struct {
|
|||||||
Err error
|
Err error
|
||||||
|
|
||||||
// General settings.
|
// General settings.
|
||||||
|
|
||||||
// Prompt is printed at the beginning of each line.
|
|
||||||
//
|
|
||||||
// When changing the value of Prompt after the model has been
|
|
||||||
// initialized, ensure that SetWidth() gets called afterwards.
|
|
||||||
//
|
|
||||||
// See also SetPromptFunc().
|
|
||||||
Prompt string
|
Prompt string
|
||||||
|
|
||||||
// Placeholder is the text displayed when the user
|
|
||||||
// hasn't entered anything yet.
|
|
||||||
Placeholder string
|
Placeholder string
|
||||||
|
|
||||||
// ShowLineNumbers, if enabled, causes line numbers to be printed
|
|
||||||
// after the prompt.
|
|
||||||
ShowLineNumbers bool
|
ShowLineNumbers bool
|
||||||
|
|
||||||
// EndOfBufferCharacter is displayed at the end of the input.
|
|
||||||
EndOfBufferCharacter rune
|
EndOfBufferCharacter rune
|
||||||
|
|
||||||
// KeyMap encodes the keybindings recognized by the widget.
|
|
||||||
KeyMap KeyMap
|
|
||||||
|
|
||||||
// Styling. FocusedStyle and BlurredStyle are used to style the textarea in
|
// Styling. FocusedStyle and BlurredStyle are used to style the textarea in
|
||||||
// focused and blurred states.
|
// focused and blurred states.
|
||||||
FocusedStyle Style
|
FocusedStyle Style
|
||||||
@@ -173,13 +97,6 @@ type Model struct {
|
|||||||
// accept. If 0 or less, there's no limit.
|
// accept. If 0 or less, there's no limit.
|
||||||
CharLimit int
|
CharLimit int
|
||||||
|
|
||||||
// If promptFunc is set, it replaces Prompt as a generator for
|
|
||||||
// prompt strings at the beginning of each line.
|
|
||||||
promptFunc func(line int) string
|
|
||||||
|
|
||||||
// promptWidth is the width of the prompt.
|
|
||||||
promptWidth int
|
|
||||||
|
|
||||||
// width is the maximum number of characters that can be displayed at once.
|
// width is the maximum number of characters that can be displayed at once.
|
||||||
// If 0 or less this setting is ignored.
|
// If 0 or less this setting is ignored.
|
||||||
width int
|
width int
|
||||||
@@ -202,6 +119,12 @@ type Model struct {
|
|||||||
// Cursor row.
|
// Cursor row.
|
||||||
row int
|
row int
|
||||||
|
|
||||||
|
// mode is the current mode of the textarea.
|
||||||
|
mode Mode
|
||||||
|
|
||||||
|
// command is the normal command of the textarea.
|
||||||
|
command *NormalCommand
|
||||||
|
|
||||||
// Last character offset, used to maintain state when the cursor is moved
|
// Last character offset, used to maintain state when the cursor is moved
|
||||||
// vertically such that we can maintain the same navigating position.
|
// vertically such that we can maintain the same navigating position.
|
||||||
lastCharOffset int
|
lastCharOffset int
|
||||||
@@ -212,8 +135,6 @@ type Model struct {
|
|||||||
// viewport is the vertically-scrollable viewport of the multi-line text
|
// viewport is the vertically-scrollable viewport of the multi-line text
|
||||||
// input.
|
// input.
|
||||||
viewport *viewport.Model
|
viewport *viewport.Model
|
||||||
|
|
||||||
isChanged bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new model with default settings.
|
// New creates a new model with default settings.
|
||||||
@@ -233,13 +154,14 @@ func New() Model {
|
|||||||
EndOfBufferCharacter: '~',
|
EndOfBufferCharacter: '~',
|
||||||
ShowLineNumbers: true,
|
ShowLineNumbers: true,
|
||||||
Cursor: cur,
|
Cursor: cur,
|
||||||
KeyMap: DefaultKeyMap,
|
|
||||||
|
|
||||||
value: make([][]rune, minHeight, maxHeight),
|
value: make([][]rune, minHeight, maxHeight),
|
||||||
|
mode: ModeInsert,
|
||||||
focus: false,
|
focus: false,
|
||||||
col: 0,
|
col: 0,
|
||||||
row: 0,
|
row: 0,
|
||||||
lineNumberFormat: "%2v ",
|
lineNumberFormat: "%2v ",
|
||||||
|
command: &NormalCommand{},
|
||||||
|
|
||||||
viewport: &vp,
|
viewport: &vp,
|
||||||
}
|
}
|
||||||
@@ -283,12 +205,6 @@ func (m *Model) SetValue(s string) {
|
|||||||
m.InsertString(s)
|
m.InsertString(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) SetValueAndReset(s string) {
|
|
||||||
m.Reset()
|
|
||||||
m.InsertString(s)
|
|
||||||
m.ResetWithoutValue()
|
|
||||||
}
|
|
||||||
|
|
||||||
// InsertString inserts a string at the cursor position.
|
// InsertString inserts a string at the cursor position.
|
||||||
func (m *Model) InsertString(s string) {
|
func (m *Model) InsertString(s string) {
|
||||||
lines := strings.Split(s, "\n")
|
lines := strings.Split(s, "\n")
|
||||||
@@ -328,10 +244,6 @@ func (m Model) Value() string {
|
|||||||
return strings.TrimSuffix(v.String(), "\n")
|
return strings.TrimSuffix(v.String(), "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Model) IsChanged() bool {
|
|
||||||
return m.isChanged
|
|
||||||
}
|
|
||||||
|
|
||||||
// Length returns the number of characters currently in the text input.
|
// Length returns the number of characters currently in the text input.
|
||||||
func (m *Model) Length() int {
|
func (m *Model) Length() int {
|
||||||
var l int
|
var l int
|
||||||
@@ -351,6 +263,11 @@ func (m Model) Line() int {
|
|||||||
return m.row
|
return m.row
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NormalCommand returns the normal command.
|
||||||
|
func (m *Model) NormalCommand() *NormalCommand {
|
||||||
|
return m.command
|
||||||
|
}
|
||||||
|
|
||||||
// CursorDown moves the cursor down by one line.
|
// CursorDown moves the cursor down by one line.
|
||||||
// Returns whether or not the cursor blink should be reset.
|
// Returns whether or not the cursor blink should be reset.
|
||||||
func (m *Model) CursorDown() {
|
func (m *Model) CursorDown() {
|
||||||
@@ -462,25 +379,12 @@ func (m *Model) Blur() {
|
|||||||
// Reset sets the input to its default state with no input.
|
// Reset sets the input to its default state with no input.
|
||||||
func (m *Model) Reset() {
|
func (m *Model) Reset() {
|
||||||
m.value = make([][]rune, minHeight, maxHeight)
|
m.value = make([][]rune, minHeight, maxHeight)
|
||||||
m.ResetWithoutValue()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Model) ResetWithoutValue() {
|
|
||||||
m.ResetView()
|
|
||||||
m.ResetChanged()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Model) ResetView() {
|
|
||||||
m.col = 0
|
m.col = 0
|
||||||
m.row = 0
|
m.row = 0
|
||||||
m.viewport.GotoTop()
|
m.viewport.GotoTop()
|
||||||
m.SetCursor(0)
|
m.SetCursor(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) ResetChanged() {
|
|
||||||
m.isChanged = false
|
|
||||||
}
|
|
||||||
|
|
||||||
// handle a clipboard paste event, if supported.
|
// handle a clipboard paste event, if supported.
|
||||||
func (m *Model) handlePaste(v string) {
|
func (m *Model) handlePaste(v string) {
|
||||||
paste := []rune(v)
|
paste := []rune(v)
|
||||||
@@ -519,11 +423,6 @@ func (m *Model) handlePaste(v string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put it all back together
|
|
||||||
value := append(head, tail...)
|
|
||||||
m.SetValue(string(value))
|
|
||||||
m.isChanged = true
|
|
||||||
|
|
||||||
// Reset blink state if necessary and run overflow checks
|
// Reset blink state if necessary and run overflow checks
|
||||||
m.SetCursor(m.col + len(paste))
|
m.SetCursor(m.col + len(paste))
|
||||||
}
|
}
|
||||||
@@ -532,7 +431,6 @@ func (m *Model) handlePaste(v string) {
|
|||||||
// not the cursor blink should be reset.
|
// not the cursor blink should be reset.
|
||||||
func (m *Model) deleteBeforeCursor() {
|
func (m *Model) deleteBeforeCursor() {
|
||||||
m.value[m.row] = m.value[m.row][m.col:]
|
m.value[m.row] = m.value[m.row][m.col:]
|
||||||
m.isChanged = true
|
|
||||||
m.SetCursor(0)
|
m.SetCursor(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -541,29 +439,9 @@ func (m *Model) deleteBeforeCursor() {
|
|||||||
// the cursor so as not to reveal word breaks in the masked input.
|
// the cursor so as not to reveal word breaks in the masked input.
|
||||||
func (m *Model) deleteAfterCursor() {
|
func (m *Model) deleteAfterCursor() {
|
||||||
m.value[m.row] = m.value[m.row][:m.col]
|
m.value[m.row] = m.value[m.row][:m.col]
|
||||||
m.isChanged = true
|
|
||||||
m.SetCursor(len(m.value[m.row]))
|
m.SetCursor(len(m.value[m.row]))
|
||||||
}
|
}
|
||||||
|
|
||||||
// transposeLeft exchanges the runes at the cursor and immediately
|
|
||||||
// before. No-op if the cursor is at the beginning of the line. If
|
|
||||||
// the cursor is not at the end of the line yet, moves the cursor to
|
|
||||||
// the right.
|
|
||||||
func (m *Model) transposeLeft() {
|
|
||||||
if m.col == 0 || len(m.value[m.row]) < 2 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if m.col >= len(m.value[m.row]) {
|
|
||||||
m.SetCursor(m.col - 1)
|
|
||||||
}
|
|
||||||
m.value[m.row][m.col-1], m.value[m.row][m.col] =
|
|
||||||
m.value[m.row][m.col], m.value[m.row][m.col-1]
|
|
||||||
if m.col < len(m.value[m.row]) {
|
|
||||||
m.SetCursor(m.col + 1)
|
|
||||||
}
|
|
||||||
m.isChanged = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// deleteWordLeft deletes the word left to the cursor. Returns whether or not
|
// deleteWordLeft deletes the word left to the cursor. Returns whether or not
|
||||||
// the cursor blink should be reset.
|
// the cursor blink should be reset.
|
||||||
func (m *Model) deleteWordLeft() {
|
func (m *Model) deleteWordLeft() {
|
||||||
@@ -602,8 +480,6 @@ func (m *Model) deleteWordLeft() {
|
|||||||
} else {
|
} else {
|
||||||
m.value[m.row] = append(m.value[m.row][:m.col], m.value[m.row][oldCol:]...)
|
m.value[m.row] = append(m.value[m.row][:m.col], m.value[m.row][oldCol:]...)
|
||||||
}
|
}
|
||||||
|
|
||||||
m.isChanged = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// deleteWordRight deletes the word right to the cursor.
|
// deleteWordRight deletes the word right to the cursor.
|
||||||
@@ -638,53 +514,33 @@ func (m *Model) deleteWordRight() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
m.SetCursor(oldCol)
|
m.SetCursor(oldCol)
|
||||||
m.isChanged = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// characterRight moves the cursor one character to the right.
|
|
||||||
func (m *Model) characterRight() {
|
|
||||||
if m.col < len(m.value[m.row]) {
|
|
||||||
m.SetCursor(m.col + 1)
|
|
||||||
} else {
|
|
||||||
if m.row < len(m.value)-1 {
|
|
||||||
m.row++
|
|
||||||
m.CursorStart()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// characterLeft moves the cursor one character to the left.
|
|
||||||
// If insideLine is set, the cursor is moved to the last
|
|
||||||
// character in the previous line, instead of one past that.
|
|
||||||
func (m *Model) characterLeft(insideLine bool) {
|
|
||||||
if m.col == 0 && m.row != 0 {
|
|
||||||
m.row--
|
|
||||||
m.CursorEnd()
|
|
||||||
if !insideLine {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if m.col > 0 {
|
|
||||||
m.SetCursor(m.col - 1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// wordLeft moves the cursor one word to the left. Returns whether or not the
|
// wordLeft moves the cursor one word to the left. Returns whether or not the
|
||||||
// cursor blink should be reset. If input is masked, move input to the start
|
// cursor blink should be reset. If input is masked, move input to the start
|
||||||
// so as not to reveal word breaks in the masked input.
|
// so as not to reveal word breaks in the masked input.
|
||||||
func (m *Model) wordLeft() {
|
func (m *Model) wordLeft() {
|
||||||
for {
|
if m.col == 0 || len(m.value[m.row]) == 0 {
|
||||||
m.characterLeft(true /* insideLine */)
|
return
|
||||||
if m.col < len(m.value[m.row]) && !unicode.IsSpace(m.value[m.row][m.col]) {
|
}
|
||||||
|
|
||||||
|
i := m.col - 1
|
||||||
|
for i >= 0 {
|
||||||
|
if unicode.IsSpace(m.value[m.row][min(i, len(m.value[m.row])-1)]) {
|
||||||
|
m.SetCursor(m.col - 1)
|
||||||
|
i--
|
||||||
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for m.col > 0 {
|
for i >= 0 {
|
||||||
if unicode.IsSpace(m.value[m.row][m.col-1]) {
|
if !unicode.IsSpace(m.value[m.row][min(i, len(m.value[m.row])-1)]) {
|
||||||
|
m.SetCursor(m.col - 1)
|
||||||
|
i--
|
||||||
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
m.SetCursor(m.col - 1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -692,57 +548,28 @@ func (m *Model) wordLeft() {
|
|||||||
// cursor blink should be reset. If the input is masked, move input to the end
|
// cursor blink should be reset. If the input is masked, move input to the end
|
||||||
// so as not to reveal word breaks in the masked input.
|
// so as not to reveal word breaks in the masked input.
|
||||||
func (m *Model) wordRight() {
|
func (m *Model) wordRight() {
|
||||||
m.doWordRight(func(int, int) { /* nothing */ })
|
if m.col >= len(m.value[m.row]) || len(m.value[m.row]) == 0 {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) doWordRight(fn func(charIdx int, pos int)) {
|
i := m.col
|
||||||
// Skip spaces forward.
|
for i < len(m.value[m.row]) {
|
||||||
for {
|
if unicode.IsSpace(m.value[m.row][i]) {
|
||||||
if m.col < len(m.value[m.row]) && !unicode.IsSpace(m.value[m.row][m.col]) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if m.row == len(m.value)-1 && m.col == len(m.value[m.row]) {
|
|
||||||
// End of text.
|
|
||||||
break
|
|
||||||
}
|
|
||||||
m.characterRight()
|
|
||||||
}
|
|
||||||
|
|
||||||
charIdx := 0
|
|
||||||
for m.col < len(m.value[m.row]) {
|
|
||||||
if unicode.IsSpace(m.value[m.row][m.col]) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
fn(charIdx, m.col)
|
|
||||||
m.SetCursor(m.col + 1)
|
m.SetCursor(m.col + 1)
|
||||||
charIdx++
|
i++
|
||||||
|
} else {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// uppercaseRight changes the word to the right to uppercase.
|
for i < len(m.value[m.row]) {
|
||||||
func (m *Model) uppercaseRight() {
|
if !unicode.IsSpace(m.value[m.row][i]) {
|
||||||
m.doWordRight(func(_ int, i int) {
|
m.SetCursor(m.col + 1)
|
||||||
m.value[m.row][i] = unicode.ToUpper(m.value[m.row][i])
|
i++
|
||||||
})
|
} else {
|
||||||
m.isChanged = true
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// lowercaseRight changes the word to the right to lowercase.
|
|
||||||
func (m *Model) lowercaseRight() {
|
|
||||||
m.doWordRight(func(_ int, i int) {
|
|
||||||
m.value[m.row][i] = unicode.ToLower(m.value[m.row][i])
|
|
||||||
})
|
|
||||||
m.isChanged = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// capitalizeRight changes the word to the right to title case.
|
|
||||||
func (m *Model) capitalizeRight() {
|
|
||||||
m.doWordRight(func(charIdx int, i int) {
|
|
||||||
if charIdx == 0 {
|
|
||||||
m.value[m.row][i] = unicode.ToTitle(m.value[m.row][i])
|
|
||||||
}
|
|
||||||
})
|
|
||||||
m.isChanged = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LineInfo returns the number of characters from the start of the
|
// LineInfo returns the number of characters from the start of the
|
||||||
@@ -804,18 +631,6 @@ func (m Model) Width() int {
|
|||||||
return m.width
|
return m.width
|
||||||
}
|
}
|
||||||
|
|
||||||
// moveToBegin moves the cursor to the beginning of the input.
|
|
||||||
func (m *Model) moveToBegin() {
|
|
||||||
m.row = 0
|
|
||||||
m.SetCursor(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// moveToEnd moves the cursor to the end of the input.
|
|
||||||
func (m *Model) moveToEnd() {
|
|
||||||
m.row = len(m.value) - 1
|
|
||||||
m.SetCursor(len(m.value[m.row]))
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetWidth sets the width of the textarea to fit exactly within the given width.
|
// SetWidth sets the width of the textarea to fit exactly within the given width.
|
||||||
// This means that the textarea will account for the width of the prompt and
|
// This means that the textarea will account for the width of the prompt and
|
||||||
// whether or not line numbers are being shown.
|
// whether or not line numbers are being shown.
|
||||||
@@ -836,26 +651,10 @@ func (m *Model) SetWidth(w int) {
|
|||||||
// Account for base style borders and padding.
|
// Account for base style borders and padding.
|
||||||
inputWidth -= m.style.Base.GetHorizontalFrameSize()
|
inputWidth -= m.style.Base.GetHorizontalFrameSize()
|
||||||
|
|
||||||
if m.promptFunc == nil {
|
inputWidth -= rw.StringWidth(m.Prompt)
|
||||||
m.promptWidth = rw.StringWidth(m.Prompt)
|
|
||||||
}
|
|
||||||
|
|
||||||
inputWidth -= m.promptWidth
|
|
||||||
m.width = clamp(inputWidth, minWidth, maxWidth)
|
m.width = clamp(inputWidth, minWidth, maxWidth)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetPromptFunc supersedes the Prompt field and sets a dynamic prompt
|
|
||||||
// instead.
|
|
||||||
// If the function returns a prompt that is shorter than the
|
|
||||||
// specified promptWidth, it will be padded to the left.
|
|
||||||
// If it returns a prompt that is longer, display artifacts
|
|
||||||
// may occur; the caller is responsible for computing an adequate
|
|
||||||
// promptWidth.
|
|
||||||
func (m *Model) SetPromptFunc(promptWidth int, fn func(lineIdx int) string) {
|
|
||||||
m.promptFunc = fn
|
|
||||||
m.promptWidth = promptWidth
|
|
||||||
}
|
|
||||||
|
|
||||||
// Height returns the current height of the textarea.
|
// Height returns the current height of the textarea.
|
||||||
func (m Model) Height() int {
|
func (m Model) Height() int {
|
||||||
return m.height
|
return m.height
|
||||||
@@ -883,111 +682,21 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|||||||
m.value[m.row] = make([]rune, 0)
|
m.value[m.row] = make([]rune, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch msg := msg.(type) {
|
var cmd tea.Cmd
|
||||||
case tea.KeyMsg:
|
|
||||||
switch {
|
|
||||||
case key.Matches(msg, m.KeyMap.DeleteAfterCursor):
|
|
||||||
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
|
||||||
if m.col >= len(m.value[m.row]) {
|
|
||||||
m.mergeLineBelow(m.row)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
m.deleteAfterCursor()
|
|
||||||
case key.Matches(msg, m.KeyMap.DeleteBeforeCursor):
|
|
||||||
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
|
||||||
if m.col <= 0 {
|
|
||||||
m.mergeLineAbove(m.row)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
m.deleteBeforeCursor()
|
|
||||||
case key.Matches(msg, m.KeyMap.DeleteCharacterBackward):
|
|
||||||
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
|
||||||
if m.col <= 0 {
|
|
||||||
m.mergeLineAbove(m.row)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if len(m.value[m.row]) > 0 {
|
|
||||||
m.value[m.row] = append(m.value[m.row][:max(0, m.col-1)], m.value[m.row][m.col:]...)
|
|
||||||
m.isChanged = true
|
|
||||||
if m.col > 0 {
|
|
||||||
m.SetCursor(m.col - 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case key.Matches(msg, m.KeyMap.DeleteCharacterForward):
|
|
||||||
if len(m.value[m.row]) > 0 && m.col < len(m.value[m.row]) {
|
|
||||||
m.value[m.row] = append(m.value[m.row][:m.col], m.value[m.row][m.col+1:]...)
|
|
||||||
m.isChanged = true
|
|
||||||
}
|
|
||||||
if m.col >= len(m.value[m.row]) {
|
|
||||||
m.mergeLineBelow(m.row)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
case key.Matches(msg, m.KeyMap.DeleteWordBackward):
|
|
||||||
if m.col <= 0 {
|
|
||||||
m.mergeLineAbove(m.row)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
m.deleteWordLeft()
|
|
||||||
case key.Matches(msg, m.KeyMap.DeleteWordForward):
|
|
||||||
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
|
||||||
if m.col >= len(m.value[m.row]) {
|
|
||||||
m.mergeLineBelow(m.row)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
m.deleteWordRight()
|
|
||||||
case key.Matches(msg, m.KeyMap.InsertNewline):
|
|
||||||
if len(m.value) >= maxHeight {
|
|
||||||
return m, nil
|
|
||||||
}
|
|
||||||
m.col = clamp(m.col, 0, len(m.value[m.row]))
|
|
||||||
m.splitLine(m.row, m.col)
|
|
||||||
case key.Matches(msg, m.KeyMap.LineEnd):
|
|
||||||
m.CursorEnd()
|
|
||||||
case key.Matches(msg, m.KeyMap.LineStart):
|
|
||||||
m.CursorStart()
|
|
||||||
case key.Matches(msg, m.KeyMap.CharacterForward):
|
|
||||||
m.characterRight()
|
|
||||||
case key.Matches(msg, m.KeyMap.LineNext):
|
|
||||||
m.CursorDown()
|
|
||||||
case key.Matches(msg, m.KeyMap.WordForward):
|
|
||||||
m.wordRight()
|
|
||||||
case key.Matches(msg, m.KeyMap.Paste):
|
|
||||||
return m, Paste
|
|
||||||
case key.Matches(msg, m.KeyMap.CharacterBackward):
|
|
||||||
m.characterLeft(false /* insideLine */)
|
|
||||||
case key.Matches(msg, m.KeyMap.LinePrevious):
|
|
||||||
m.CursorUp()
|
|
||||||
case key.Matches(msg, m.KeyMap.WordBackward):
|
|
||||||
m.wordLeft()
|
|
||||||
case key.Matches(msg, m.KeyMap.InputBegin):
|
|
||||||
m.moveToBegin()
|
|
||||||
case key.Matches(msg, m.KeyMap.InputEnd):
|
|
||||||
m.moveToEnd()
|
|
||||||
case key.Matches(msg, m.KeyMap.LowercaseWordForward):
|
|
||||||
m.lowercaseRight()
|
|
||||||
case key.Matches(msg, m.KeyMap.UppercaseWordForward):
|
|
||||||
m.uppercaseRight()
|
|
||||||
case key.Matches(msg, m.KeyMap.CapitalizeWordForward):
|
|
||||||
m.capitalizeRight()
|
|
||||||
case key.Matches(msg, m.KeyMap.TransposeCharacterBackward):
|
|
||||||
m.transposeLeft()
|
|
||||||
|
|
||||||
default:
|
switch m.mode {
|
||||||
if m.CharLimit > 0 && rw.StringWidth(m.Value()) >= m.CharLimit {
|
case ModeInsert:
|
||||||
break
|
cmd = m.insertUpdate(msg)
|
||||||
|
cmds = append(cmds, cmd)
|
||||||
|
// Sometimes, we need to enter insert mode after executing a command.
|
||||||
|
switch msg.(type) {
|
||||||
|
case executeMsg:
|
||||||
|
cmd = m.normalUpdate(msg)
|
||||||
|
cmds = append(cmds, cmd)
|
||||||
}
|
}
|
||||||
|
case ModeNormal:
|
||||||
m.col = min(m.col, len(m.value[m.row]))
|
cmd = m.normalUpdate(msg)
|
||||||
m.value[m.row] = append(m.value[m.row][:m.col], append(msg.Runes, m.value[m.row][m.col:]...)...)
|
cmds = append(cmds, cmd)
|
||||||
m.isChanged = true
|
|
||||||
m.SetCursor(m.col + len(msg.Runes))
|
|
||||||
}
|
|
||||||
|
|
||||||
case pasteMsg:
|
|
||||||
m.handlePaste(string(msg))
|
|
||||||
|
|
||||||
case pasteErrMsg:
|
|
||||||
m.Err = msg
|
|
||||||
}
|
}
|
||||||
|
|
||||||
vp, cmd := m.viewport.Update(msg)
|
vp, cmd := m.viewport.Update(msg)
|
||||||
@@ -996,7 +705,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|||||||
|
|
||||||
newRow, newCol := m.cursorLineNumber(), m.col
|
newRow, newCol := m.cursorLineNumber(), m.col
|
||||||
m.Cursor, cmd = m.Cursor.Update(msg)
|
m.Cursor, cmd = m.Cursor.Update(msg)
|
||||||
if newRow != oldRow || newCol != oldCol {
|
if m.mode == ModeInsert && (newRow != oldRow || newCol != oldCol) {
|
||||||
m.Cursor.Blink = false
|
m.Cursor.Blink = false
|
||||||
cmd = m.Cursor.BlinkCmd()
|
cmd = m.Cursor.BlinkCmd()
|
||||||
}
|
}
|
||||||
@@ -1020,7 +729,6 @@ func (m Model) View() string {
|
|||||||
|
|
||||||
var newLines int
|
var newLines int
|
||||||
|
|
||||||
displayLine := 0
|
|
||||||
for l, line := range m.value {
|
for l, line := range m.value {
|
||||||
wrappedLines := wrap(line, m.width)
|
wrappedLines := wrap(line, m.width)
|
||||||
|
|
||||||
@@ -1031,10 +739,7 @@ func (m Model) View() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for wl, wrappedLine := range wrappedLines {
|
for wl, wrappedLine := range wrappedLines {
|
||||||
prompt := m.getPromptString(displayLine)
|
s.WriteString(style.Render(m.style.Prompt.Render(m.Prompt)))
|
||||||
prompt = m.style.Prompt.Render(prompt)
|
|
||||||
s.WriteString(style.Render(prompt))
|
|
||||||
displayLine++
|
|
||||||
|
|
||||||
if m.ShowLineNumbers {
|
if m.ShowLineNumbers {
|
||||||
if wl == 0 {
|
if wl == 0 {
|
||||||
@@ -1083,10 +788,7 @@ func (m Model) View() string {
|
|||||||
// Always show at least `m.Height` lines at all times.
|
// Always show at least `m.Height` lines at all times.
|
||||||
// To do this we can simply pad out a few extra new lines in the view.
|
// To do this we can simply pad out a few extra new lines in the view.
|
||||||
for i := 0; i < m.height; i++ {
|
for i := 0; i < m.height; i++ {
|
||||||
prompt := m.getPromptString(displayLine)
|
s.WriteString(m.style.Prompt.Render(m.Prompt))
|
||||||
prompt = m.style.Prompt.Render(prompt)
|
|
||||||
s.WriteString(prompt)
|
|
||||||
displayLine++
|
|
||||||
|
|
||||||
if m.ShowLineNumbers {
|
if m.ShowLineNumbers {
|
||||||
lineNumber := m.style.EndOfBuffer.Render((fmt.Sprintf(m.lineNumberFormat, string(m.EndOfBufferCharacter))))
|
lineNumber := m.style.EndOfBuffer.Render((fmt.Sprintf(m.lineNumberFormat, string(m.EndOfBufferCharacter))))
|
||||||
@@ -1099,19 +801,6 @@ func (m Model) View() string {
|
|||||||
return m.style.Base.Render(m.viewport.View())
|
return m.style.Base.Render(m.viewport.View())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Model) getPromptString(displayLine int) (prompt string) {
|
|
||||||
prompt = m.Prompt
|
|
||||||
if m.promptFunc == nil {
|
|
||||||
return prompt
|
|
||||||
}
|
|
||||||
prompt = m.promptFunc(displayLine)
|
|
||||||
pl := rw.StringWidth(prompt)
|
|
||||||
if pl < m.promptWidth {
|
|
||||||
prompt = fmt.Sprintf("%*s%s", m.promptWidth-pl, "", prompt)
|
|
||||||
}
|
|
||||||
return prompt
|
|
||||||
}
|
|
||||||
|
|
||||||
// placeholderView returns the prompt and placeholder view, if any.
|
// placeholderView returns the prompt and placeholder view, if any.
|
||||||
func (m Model) placeholderView() string {
|
func (m Model) placeholderView() string {
|
||||||
var (
|
var (
|
||||||
@@ -1120,8 +809,7 @@ func (m Model) placeholderView() string {
|
|||||||
style = m.style.Placeholder.Inline(true)
|
style = m.style.Placeholder.Inline(true)
|
||||||
)
|
)
|
||||||
|
|
||||||
prompt := m.getPromptString(0)
|
prompt := m.style.Prompt.Render(m.Prompt)
|
||||||
prompt = m.style.Prompt.Render(prompt)
|
|
||||||
s.WriteString(m.style.CursorLine.Render(prompt))
|
s.WriteString(m.style.CursorLine.Render(prompt))
|
||||||
|
|
||||||
if m.ShowLineNumbers {
|
if m.ShowLineNumbers {
|
||||||
@@ -1138,8 +826,6 @@ func (m Model) placeholderView() string {
|
|||||||
// The rest of the new lines
|
// The rest of the new lines
|
||||||
for i := 1; i < m.height; i++ {
|
for i := 1; i < m.height; i++ {
|
||||||
s.WriteRune('\n')
|
s.WriteRune('\n')
|
||||||
prompt := m.getPromptString(i)
|
|
||||||
prompt = m.style.Prompt.Render(prompt)
|
|
||||||
s.WriteString(prompt)
|
s.WriteString(prompt)
|
||||||
|
|
||||||
if m.ShowLineNumbers {
|
if m.ShowLineNumbers {
|
||||||
@@ -1188,8 +874,6 @@ func (m *Model) mergeLineBelow(row int) {
|
|||||||
if len(m.value) > 0 {
|
if len(m.value) > 0 {
|
||||||
m.value = m.value[:len(m.value)-1]
|
m.value = m.value[:len(m.value)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
m.isChanged = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// mergeLineAbove merges the current line the cursor is on with the line above.
|
// mergeLineAbove merges the current line the cursor is on with the line above.
|
||||||
@@ -1213,8 +897,6 @@ func (m *Model) mergeLineAbove(row int) {
|
|||||||
if len(m.value) > 0 {
|
if len(m.value) > 0 {
|
||||||
m.value = m.value[:len(m.value)-1]
|
m.value = m.value[:len(m.value)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
m.isChanged = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) splitLine(row, col int) {
|
func (m *Model) splitLine(row, col int) {
|
||||||
@@ -1232,8 +914,6 @@ func (m *Model) splitLine(row, col int) {
|
|||||||
|
|
||||||
m.col = 0
|
m.col = 0
|
||||||
m.row++
|
m.row++
|
||||||
|
|
||||||
m.isChanged = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Paste is a command for pasting from the clipboard into the text input.
|
// Paste is a command for pasting from the clipboard into the text input.
|
||||||
|
@@ -1,18 +1,48 @@
|
|||||||
package textinput
|
package textinput
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/Maks1mS/bubbles/cursor"
|
|
||||||
"github.com/Maks1mS/bubbles/key"
|
|
||||||
"github.com/atotto/clipboard"
|
"github.com/atotto/clipboard"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
rw "github.com/mattn/go-runewidth"
|
rw "github.com/mattn/go-runewidth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultBlinkSpeed = time.Millisecond * 530
|
||||||
|
|
||||||
|
// Internal ID management for text inputs. Necessary for blink integrity when
|
||||||
|
// multiple text inputs are involved.
|
||||||
|
var (
|
||||||
|
lastID int
|
||||||
|
idMtx sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
|
// Return the next ID we should use on the Model.
|
||||||
|
func nextID() int {
|
||||||
|
idMtx.Lock()
|
||||||
|
defer idMtx.Unlock()
|
||||||
|
lastID++
|
||||||
|
return lastID
|
||||||
|
}
|
||||||
|
|
||||||
|
// initialBlinkMsg initializes cursor blinking.
|
||||||
|
type initialBlinkMsg struct{}
|
||||||
|
|
||||||
|
// blinkMsg signals that the cursor should blink. It contains metadata that
|
||||||
|
// allows us to tell if the blink message is the one we're expecting.
|
||||||
|
type blinkMsg struct {
|
||||||
|
id int
|
||||||
|
tag int
|
||||||
|
}
|
||||||
|
|
||||||
|
// blinkCanceled is sent when a blink operation is canceled.
|
||||||
|
type blinkCanceled struct{}
|
||||||
|
|
||||||
// Internal messages for clipboard operations.
|
// Internal messages for clipboard operations.
|
||||||
type pasteMsg string
|
type pasteMsg string
|
||||||
type pasteErrMsg struct{ error }
|
type pasteErrMsg struct{ error }
|
||||||
@@ -35,44 +65,35 @@ const (
|
|||||||
// EchoOnEdit.
|
// EchoOnEdit.
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// blinkCtx manages cursor blinking.
|
||||||
|
type blinkCtx struct {
|
||||||
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
// CursorMode describes the behavior of the cursor.
|
||||||
|
type CursorMode int
|
||||||
|
|
||||||
|
// Available cursor modes.
|
||||||
|
const (
|
||||||
|
CursorBlink CursorMode = iota
|
||||||
|
CursorStatic
|
||||||
|
CursorHide
|
||||||
|
)
|
||||||
|
|
||||||
|
// String returns a the cursor mode in a human-readable format. This method is
|
||||||
|
// provisional and for informational purposes only.
|
||||||
|
func (c CursorMode) String() string {
|
||||||
|
return [...]string{
|
||||||
|
"blink",
|
||||||
|
"static",
|
||||||
|
"hidden",
|
||||||
|
}[c]
|
||||||
|
}
|
||||||
|
|
||||||
// ValidateFunc is a function that returns an error if the input is invalid.
|
// ValidateFunc is a function that returns an error if the input is invalid.
|
||||||
type ValidateFunc func(string) error
|
type ValidateFunc func(string) error
|
||||||
|
|
||||||
// KeyMap is the key bindings for different actions within the textinput.
|
|
||||||
type KeyMap struct {
|
|
||||||
CharacterForward key.Binding
|
|
||||||
CharacterBackward key.Binding
|
|
||||||
WordForward key.Binding
|
|
||||||
WordBackward key.Binding
|
|
||||||
DeleteWordBackward key.Binding
|
|
||||||
DeleteWordForward key.Binding
|
|
||||||
DeleteAfterCursor key.Binding
|
|
||||||
DeleteBeforeCursor key.Binding
|
|
||||||
DeleteCharacterBackward key.Binding
|
|
||||||
DeleteCharacterForward key.Binding
|
|
||||||
LineStart key.Binding
|
|
||||||
LineEnd key.Binding
|
|
||||||
Paste key.Binding
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultKeyMap is the default set of key bindings for navigating and acting
|
|
||||||
// upon the textinput.
|
|
||||||
var DefaultKeyMap = KeyMap{
|
|
||||||
CharacterForward: key.NewBinding(key.WithKeys("right", "ctrl+f")),
|
|
||||||
CharacterBackward: key.NewBinding(key.WithKeys("left", "ctrl+b")),
|
|
||||||
WordForward: key.NewBinding(key.WithKeys("alt+right", "alt+f")),
|
|
||||||
WordBackward: key.NewBinding(key.WithKeys("alt+left", "alt+b")),
|
|
||||||
DeleteWordBackward: key.NewBinding(key.WithKeys("alt+backspace", "ctrl+w")),
|
|
||||||
DeleteWordForward: key.NewBinding(key.WithKeys("alte+delete", "alt+d")),
|
|
||||||
DeleteAfterCursor: key.NewBinding(key.WithKeys("ctrl+k")),
|
|
||||||
DeleteBeforeCursor: key.NewBinding(key.WithKeys("ctrl+u")),
|
|
||||||
DeleteCharacterBackward: key.NewBinding(key.WithKeys("backspace", "ctrl+h")),
|
|
||||||
DeleteCharacterForward: key.NewBinding(key.WithKeys("delete", "ctrl+d")),
|
|
||||||
LineStart: key.NewBinding(key.WithKeys("home", "ctrl+a")),
|
|
||||||
LineEnd: key.NewBinding(key.WithKeys("end", "ctrl+e")),
|
|
||||||
Paste: key.NewBinding(key.WithKeys("ctrl+v")),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Model is the Bubble Tea model for this text input element.
|
// Model is the Bubble Tea model for this text input element.
|
||||||
type Model struct {
|
type Model struct {
|
||||||
Err error
|
Err error
|
||||||
@@ -80,13 +101,9 @@ type Model struct {
|
|||||||
// General settings.
|
// General settings.
|
||||||
Prompt string
|
Prompt string
|
||||||
Placeholder string
|
Placeholder string
|
||||||
|
BlinkSpeed time.Duration
|
||||||
EchoMode EchoMode
|
EchoMode EchoMode
|
||||||
EchoCharacter rune
|
EchoCharacter rune
|
||||||
Cursor cursor.Model
|
|
||||||
|
|
||||||
// Deprecated: use cursor.BlinkSpeed instead.
|
|
||||||
// This is unused and will be removed in the future.
|
|
||||||
BlinkSpeed time.Duration
|
|
||||||
|
|
||||||
// Styles. These will be applied as inline styles.
|
// Styles. These will be applied as inline styles.
|
||||||
//
|
//
|
||||||
@@ -107,8 +124,11 @@ 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
|
||||||
|
|
||||||
// KeyMap encodes the keybindings recognized by the widget.
|
// The ID of this Model as it relates to other textinput Models.
|
||||||
KeyMap KeyMap
|
id int
|
||||||
|
|
||||||
|
// The ID of the blink message we're expecting to receive.
|
||||||
|
blinkTag int
|
||||||
|
|
||||||
// Underlying text value.
|
// Underlying text value.
|
||||||
value []rune
|
value []rune
|
||||||
@@ -117,6 +137,9 @@ type Model struct {
|
|||||||
// component. When false, ignore keyboard input and hide the cursor.
|
// component. When false, ignore keyboard input and hide the cursor.
|
||||||
focus bool
|
focus bool
|
||||||
|
|
||||||
|
// Cursor blink state.
|
||||||
|
blink bool
|
||||||
|
|
||||||
// Cursor position.
|
// Cursor position.
|
||||||
pos int
|
pos int
|
||||||
|
|
||||||
@@ -125,6 +148,12 @@ type Model struct {
|
|||||||
offset int
|
offset int
|
||||||
offsetRight int
|
offsetRight int
|
||||||
|
|
||||||
|
// Used to manage cursor blink
|
||||||
|
blinkCtx *blinkCtx
|
||||||
|
|
||||||
|
// cursorMode determines the behavior of the cursor
|
||||||
|
cursorMode CursorMode
|
||||||
|
|
||||||
// Validate is a function that checks whether or not the text within the
|
// Validate is a function that checks whether or not the text within the
|
||||||
// input is valid. If it is not valid, the `Err` field will be set to the
|
// input is valid. If it is not valid, the `Err` field will be set to the
|
||||||
// error returned by the function. If the function is not defined, all
|
// error returned by the function. If the function is not defined, all
|
||||||
@@ -136,15 +165,21 @@ type Model struct {
|
|||||||
func New() Model {
|
func New() Model {
|
||||||
return Model{
|
return Model{
|
||||||
Prompt: "> ",
|
Prompt: "> ",
|
||||||
|
BlinkSpeed: defaultBlinkSpeed,
|
||||||
EchoCharacter: '*',
|
EchoCharacter: '*',
|
||||||
CharLimit: 0,
|
CharLimit: 0,
|
||||||
PlaceholderStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("240")),
|
PlaceholderStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("240")),
|
||||||
Cursor: cursor.New(),
|
|
||||||
KeyMap: DefaultKeyMap,
|
|
||||||
|
|
||||||
|
id: nextID(),
|
||||||
value: nil,
|
value: nil,
|
||||||
focus: false,
|
focus: false,
|
||||||
|
blink: true,
|
||||||
pos: 0,
|
pos: 0,
|
||||||
|
cursorMode: CursorBlink,
|
||||||
|
|
||||||
|
blinkCtx: &blinkCtx{
|
||||||
|
ctx: context.Background(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,7 +197,6 @@ func (m *Model) SetValue(s string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
empty := len(m.value) == 0
|
|
||||||
m.Err = nil
|
m.Err = nil
|
||||||
|
|
||||||
runes := []rune(s)
|
runes := []rune(s)
|
||||||
@@ -171,8 +205,8 @@ func (m *Model) SetValue(s string) {
|
|||||||
} else {
|
} else {
|
||||||
m.value = runes
|
m.value = runes
|
||||||
}
|
}
|
||||||
if (m.pos == 0 && empty) || m.pos > len(m.value) {
|
if m.pos == 0 || m.pos > len(m.value) {
|
||||||
m.SetCursor(len(m.value))
|
m.setCursor(len(m.value))
|
||||||
}
|
}
|
||||||
m.handleOverflow()
|
m.handleOverflow()
|
||||||
}
|
}
|
||||||
@@ -182,26 +216,74 @@ func (m Model) Value() string {
|
|||||||
return string(m.value)
|
return string(m.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Position returns the cursor position.
|
// Cursor returns the cursor position.
|
||||||
func (m Model) Position() int {
|
func (m Model) Cursor() int {
|
||||||
return m.pos
|
return m.pos
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Blink returns whether or not to draw the cursor.
|
||||||
|
func (m Model) Blink() bool {
|
||||||
|
return m.blink
|
||||||
|
}
|
||||||
|
|
||||||
// SetCursor moves the cursor to the given position. If the position is
|
// SetCursor moves the cursor to the given position. If the position is
|
||||||
// out of bounds the cursor will be moved to the start or end accordingly.
|
// out of bounds the cursor will be moved to the start or end accordingly.
|
||||||
func (m *Model) SetCursor(pos int) {
|
func (m *Model) SetCursor(pos int) {
|
||||||
|
m.setCursor(pos)
|
||||||
|
}
|
||||||
|
|
||||||
|
// setCursor moves the cursor to the given position and returns whether or not
|
||||||
|
// the cursor blink should be reset. If the position is out of bounds the
|
||||||
|
// cursor will be moved to the start or end accordingly.
|
||||||
|
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 input field.
|
// CursorStart moves the cursor to the start of the input field.
|
||||||
func (m *Model) CursorStart() {
|
func (m *Model) CursorStart() {
|
||||||
m.SetCursor(0)
|
m.cursorStart()
|
||||||
|
}
|
||||||
|
|
||||||
|
// cursorStart moves the cursor to the start of the input field and returns
|
||||||
|
// whether or not the curosr blink should be reset.
|
||||||
|
func (m *Model) cursorStart() bool {
|
||||||
|
return m.setCursor(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CursorEnd moves the cursor to the end of the input field.
|
// CursorEnd moves the cursor to the end of the input field.
|
||||||
func (m *Model) CursorEnd() {
|
func (m *Model) CursorEnd() {
|
||||||
m.SetCursor(len(m.value))
|
m.cursorEnd()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CursorMode returns the model's cursor mode. For available cursor modes, see
|
||||||
|
// type CursorMode.
|
||||||
|
func (m Model) CursorMode() CursorMode {
|
||||||
|
return m.cursorMode
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCursorMode sets the model's cursor mode. This method returns a command.
|
||||||
|
//
|
||||||
|
// For available cursor modes, see type CursorMode.
|
||||||
|
func (m *Model) SetCursorMode(mode CursorMode) tea.Cmd {
|
||||||
|
m.cursorMode = mode
|
||||||
|
m.blink = m.cursorMode == CursorHide || !m.focus
|
||||||
|
if mode == CursorBlink {
|
||||||
|
return Blink
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// cursorEnd moves the cursor to the end of the input field and returns whether
|
||||||
|
// the cursor should blink should reset.
|
||||||
|
func (m *Model) cursorEnd() bool {
|
||||||
|
return m.setCursor(len(m.value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Focused returns the focus state on the model.
|
// Focused returns the focus state on the model.
|
||||||
@@ -210,27 +292,34 @@ func (m Model) Focused() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Focus sets the focus state on the model. When the model is in focus it can
|
// Focus sets the focus state on the model. When the model is in focus it can
|
||||||
// receive keyboard input and the cursor will be shown.
|
// receive keyboard input and the cursor will be hidden.
|
||||||
func (m *Model) Focus() tea.Cmd {
|
func (m *Model) Focus() tea.Cmd {
|
||||||
m.focus = true
|
m.focus = true
|
||||||
return m.Cursor.Focus()
|
m.blink = m.cursorMode == CursorHide // show the cursor unless we've explicitly hidden it
|
||||||
|
|
||||||
|
if m.cursorMode == CursorBlink && m.focus {
|
||||||
|
return m.blinkCmd()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blur removes the focus state on the model. When the model is blurred it can
|
// Blur removes the focus state on the model. When the model is blurred it can
|
||||||
// not receive keyboard input and the cursor will be hidden.
|
// not receive keyboard input and the cursor will be hidden.
|
||||||
func (m *Model) Blur() {
|
func (m *Model) Blur() {
|
||||||
m.focus = false
|
m.focus = false
|
||||||
m.Cursor.Blur()
|
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.SetCursor(0)
|
return m.setCursor(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle a clipboard paste event, if supported.
|
// handle a clipboard paste event, if supported. Returns whether or not the
|
||||||
func (m *Model) handlePaste(v string) {
|
// cursor blink should reset.
|
||||||
|
func (m *Model) handlePaste(v string) bool {
|
||||||
paste := []rune(v)
|
paste := []rune(v)
|
||||||
|
|
||||||
var availSpace int
|
var availSpace int
|
||||||
@@ -240,7 +329,7 @@ func (m *Model) handlePaste(v string) {
|
|||||||
|
|
||||||
// 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 {
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@@ -277,7 +366,8 @@ func (m *Model) handlePaste(v string) {
|
|||||||
m.pos = oldPos
|
m.pos = oldPos
|
||||||
}
|
}
|
||||||
|
|
||||||
m.SetCursor(m.pos)
|
// 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
|
||||||
@@ -325,30 +415,31 @@ func (m *Model) handleOverflow() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// deleteBeforeCursor deletes all text before the cursor.
|
// deleteBeforeCursor deletes all text before the cursor. Returns whether or
|
||||||
func (m *Model) deleteBeforeCursor() {
|
// not the cursor blink should be reset.
|
||||||
|
func (m *Model) deleteBeforeCursor() bool {
|
||||||
m.value = m.value[m.pos:]
|
m.value = m.value[m.pos:]
|
||||||
m.offset = 0
|
m.offset = 0
|
||||||
m.SetCursor(0)
|
return m.setCursor(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// deleteAfterCursor deletes all text after the cursor. If input is masked
|
// deleteAfterCursor deletes all text after the cursor. Returns whether or not
|
||||||
// delete everything after the cursor so as not to reveal word breaks in the
|
// the cursor blink should be reset. If input is masked delete everything after
|
||||||
// masked input.
|
// the cursor so as not to reveal word breaks in the masked input.
|
||||||
func (m *Model) deleteAfterCursor() {
|
func (m *Model) deleteAfterCursor() bool {
|
||||||
m.value = m.value[:m.pos]
|
m.value = m.value[:m.pos]
|
||||||
m.SetCursor(len(m.value))
|
return m.setCursor(len(m.value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// deleteWordBackward deletes the word left to the cursor.
|
// deleteWordLeft deletes the word left to the cursor. Returns whether or not
|
||||||
func (m *Model) deleteWordBackward() {
|
// the cursor blink should be reset.
|
||||||
|
func (m *Model) deleteWordLeft() bool {
|
||||||
if m.pos == 0 || len(m.value) == 0 {
|
if m.pos == 0 || len(m.value) == 0 {
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.EchoMode != EchoNormal {
|
if m.EchoMode != EchoNormal {
|
||||||
m.deleteBeforeCursor()
|
return m.deleteBeforeCursor()
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Linter note: it's critical that we acquire the initial cursor position
|
// Linter note: it's critical that we acquire the initial cursor position
|
||||||
@@ -356,22 +447,22 @@ func (m *Model) deleteWordBackward() {
|
|||||||
// call into the corresponding if clause does not apply here.
|
// call into the corresponding if clause does not apply here.
|
||||||
oldPos := m.pos //nolint:ifshort
|
oldPos := m.pos //nolint:ifshort
|
||||||
|
|
||||||
m.SetCursor(m.pos - 1)
|
blink := m.setCursor(m.pos - 1)
|
||||||
for unicode.IsSpace(m.value[m.pos]) {
|
for unicode.IsSpace(m.value[m.pos]) {
|
||||||
if m.pos <= 0 {
|
if m.pos <= 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// ignore series of whitespace before cursor
|
// ignore series of whitespace before cursor
|
||||||
m.SetCursor(m.pos - 1)
|
blink = m.setCursor(m.pos - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
for m.pos > 0 {
|
for m.pos > 0 {
|
||||||
if !unicode.IsSpace(m.value[m.pos]) {
|
if !unicode.IsSpace(m.value[m.pos]) {
|
||||||
m.SetCursor(m.pos - 1)
|
blink = m.setCursor(m.pos - 1)
|
||||||
} else {
|
} else {
|
||||||
if m.pos > 0 {
|
if m.pos > 0 {
|
||||||
// keep the previous space
|
// keep the previous space
|
||||||
m.SetCursor(m.pos + 1)
|
blink = m.setCursor(m.pos + 1)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -382,26 +473,27 @@ func (m *Model) deleteWordBackward() {
|
|||||||
} else {
|
} else {
|
||||||
m.value = append(m.value[:m.pos], m.value[oldPos:]...)
|
m.value = append(m.value[:m.pos], m.value[oldPos:]...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return blink
|
||||||
}
|
}
|
||||||
|
|
||||||
// deleteWordForward deletes the word right to the cursor If input is masked
|
// deleteWordRight deletes the word right to the cursor. Returns whether or not
|
||||||
// delete everything after the cursor so as not to reveal word breaks in the
|
// the cursor blink should be reset. If input is masked delete everything after
|
||||||
// masked input.
|
// the cursor so as not to reveal word breaks in the masked input.
|
||||||
func (m *Model) deleteWordForward() {
|
func (m *Model) deleteWordRight() bool {
|
||||||
if m.pos >= len(m.value) || len(m.value) == 0 {
|
if m.pos >= len(m.value) || len(m.value) == 0 {
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.EchoMode != EchoNormal {
|
if m.EchoMode != EchoNormal {
|
||||||
m.deleteAfterCursor()
|
return m.deleteAfterCursor()
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
oldPos := m.pos
|
oldPos := m.pos
|
||||||
m.SetCursor(m.pos + 1)
|
m.setCursor(m.pos + 1)
|
||||||
for unicode.IsSpace(m.value[m.pos]) {
|
for unicode.IsSpace(m.value[m.pos]) {
|
||||||
// ignore series of whitespace after cursor
|
// ignore series of whitespace after cursor
|
||||||
m.SetCursor(m.pos + 1)
|
m.setCursor(m.pos + 1)
|
||||||
|
|
||||||
if m.pos >= len(m.value) {
|
if m.pos >= len(m.value) {
|
||||||
break
|
break
|
||||||
@@ -410,7 +502,7 @@ func (m *Model) deleteWordForward() {
|
|||||||
|
|
||||||
for m.pos < len(m.value) {
|
for m.pos < len(m.value) {
|
||||||
if !unicode.IsSpace(m.value[m.pos]) {
|
if !unicode.IsSpace(m.value[m.pos]) {
|
||||||
m.SetCursor(m.pos + 1)
|
m.setCursor(m.pos + 1)
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -422,25 +514,26 @@ func (m *Model) deleteWordForward() {
|
|||||||
m.value = append(m.value[:oldPos], m.value[m.pos:]...)
|
m.value = append(m.value[:oldPos], m.value[m.pos:]...)
|
||||||
}
|
}
|
||||||
|
|
||||||
m.SetCursor(oldPos)
|
return m.setCursor(oldPos)
|
||||||
}
|
}
|
||||||
|
|
||||||
// wordBackward moves the cursor one word to the left. If input is masked, move
|
// wordLeft moves the cursor one word to the left. Returns whether or not the
|
||||||
// input to the start so as not to reveal word breaks in the masked input.
|
// cursor blink should be reset. If input is masked, move input to the start
|
||||||
func (m *Model) wordBackward() {
|
// so as not to reveal word breaks in the masked input.
|
||||||
|
func (m *Model) wordLeft() bool {
|
||||||
if m.pos == 0 || len(m.value) == 0 {
|
if m.pos == 0 || len(m.value) == 0 {
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.EchoMode != EchoNormal {
|
if m.EchoMode != EchoNormal {
|
||||||
m.CursorStart()
|
return m.cursorStart()
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
blink := false
|
||||||
i := m.pos - 1
|
i := m.pos - 1
|
||||||
for i >= 0 {
|
for i >= 0 {
|
||||||
if unicode.IsSpace(m.value[i]) {
|
if unicode.IsSpace(m.value[i]) {
|
||||||
m.SetCursor(m.pos - 1)
|
blink = m.setCursor(m.pos - 1)
|
||||||
i--
|
i--
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
@@ -449,30 +542,33 @@ func (m *Model) wordBackward() {
|
|||||||
|
|
||||||
for i >= 0 {
|
for i >= 0 {
|
||||||
if !unicode.IsSpace(m.value[i]) {
|
if !unicode.IsSpace(m.value[i]) {
|
||||||
m.SetCursor(m.pos - 1)
|
blink = m.setCursor(m.pos - 1)
|
||||||
i--
|
i--
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return blink
|
||||||
}
|
}
|
||||||
|
|
||||||
// wordForward moves the cursor one word to the right. If the input is masked,
|
// wordRight moves the cursor one word to the right. Returns whether or not the
|
||||||
// move input to the end so as not to reveal word breaks in the masked input.
|
// cursor blink should be reset. If the input is masked, move input to the end
|
||||||
func (m *Model) wordForward() {
|
// so as not to reveal word breaks in the masked input.
|
||||||
|
func (m *Model) wordRight() bool {
|
||||||
if m.pos >= len(m.value) || len(m.value) == 0 {
|
if m.pos >= len(m.value) || len(m.value) == 0 {
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.EchoMode != EchoNormal {
|
if m.EchoMode != EchoNormal {
|
||||||
m.CursorEnd()
|
return m.cursorEnd()
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
blink := false
|
||||||
i := m.pos
|
i := m.pos
|
||||||
for i < len(m.value) {
|
for i < len(m.value) {
|
||||||
if unicode.IsSpace(m.value[i]) {
|
if unicode.IsSpace(m.value[i]) {
|
||||||
m.SetCursor(m.pos + 1)
|
blink = m.setCursor(m.pos + 1)
|
||||||
i++
|
i++
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
@@ -481,12 +577,14 @@ func (m *Model) wordForward() {
|
|||||||
|
|
||||||
for i < len(m.value) {
|
for i < len(m.value) {
|
||||||
if !unicode.IsSpace(m.value[i]) {
|
if !unicode.IsSpace(m.value[i]) {
|
||||||
m.SetCursor(m.pos + 1)
|
blink = m.setCursor(m.pos + 1)
|
||||||
i++
|
i++
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return blink
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Model) echoTransform(v string) string {
|
func (m Model) echoTransform(v string) string {
|
||||||
@@ -504,58 +602,76 @@ func (m Model) echoTransform(v string) string {
|
|||||||
// Update is the Bubble Tea update loop.
|
// Update is the Bubble Tea update loop.
|
||||||
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||||
if !m.focus {
|
if !m.focus {
|
||||||
|
m.blink = true
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Let's remember where the position of the cursor currently is so that if
|
var resetBlink bool
|
||||||
// the cursor position changes, we can reset the blink.
|
|
||||||
oldPos := m.pos //nolint
|
|
||||||
|
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch {
|
switch msg.Type {
|
||||||
case key.Matches(msg, m.KeyMap.DeleteWordBackward):
|
case tea.KeyBackspace, tea.KeyCtrlH: // delete character before cursor
|
||||||
m.Err = nil
|
|
||||||
m.deleteWordBackward()
|
|
||||||
case key.Matches(msg, m.KeyMap.DeleteCharacterBackward):
|
|
||||||
m.Err = nil
|
m.Err = nil
|
||||||
|
|
||||||
|
if msg.Alt {
|
||||||
|
resetBlink = m.deleteWordLeft()
|
||||||
|
} else {
|
||||||
if len(m.value) > 0 {
|
if len(m.value) > 0 {
|
||||||
m.value = append(m.value[:max(0, m.pos-1)], m.value[m.pos:]...)
|
m.value = append(m.value[:max(0, m.pos-1)], m.value[m.pos:]...)
|
||||||
if m.pos > 0 {
|
if m.pos > 0 {
|
||||||
m.SetCursor(m.pos - 1)
|
resetBlink = m.setCursor(m.pos - 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case key.Matches(msg, m.KeyMap.WordBackward):
|
|
||||||
m.wordBackward()
|
|
||||||
case key.Matches(msg, m.KeyMap.CharacterBackward):
|
|
||||||
if m.pos > 0 {
|
|
||||||
m.SetCursor(m.pos - 1)
|
|
||||||
}
|
}
|
||||||
case key.Matches(msg, m.KeyMap.WordForward):
|
case tea.KeyLeft, tea.KeyCtrlB:
|
||||||
m.wordForward()
|
if msg.Alt { // alt+left arrow, back one word
|
||||||
case key.Matches(msg, m.KeyMap.CharacterForward):
|
resetBlink = m.wordLeft()
|
||||||
if m.pos < len(m.value) {
|
break
|
||||||
m.SetCursor(m.pos + 1)
|
|
||||||
}
|
}
|
||||||
case key.Matches(msg, m.KeyMap.DeleteWordBackward):
|
if m.pos > 0 { // left arrow, ^F, back one character
|
||||||
m.deleteWordBackward()
|
resetBlink = m.setCursor(m.pos - 1)
|
||||||
case key.Matches(msg, m.KeyMap.LineStart):
|
}
|
||||||
m.CursorStart()
|
case tea.KeyRight, tea.KeyCtrlF:
|
||||||
case key.Matches(msg, m.KeyMap.DeleteCharacterForward):
|
if msg.Alt { // alt+right arrow, forward one word
|
||||||
|
resetBlink = m.wordRight()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if m.pos < len(m.value) { // right arrow, ^F, forward one character
|
||||||
|
resetBlink = m.setCursor(m.pos + 1)
|
||||||
|
}
|
||||||
|
case tea.KeyCtrlW: // ^W, delete word left of cursor
|
||||||
|
resetBlink = m.deleteWordLeft()
|
||||||
|
case tea.KeyHome, tea.KeyCtrlA: // ^A, go to beginning
|
||||||
|
resetBlink = m.cursorStart()
|
||||||
|
case tea.KeyDelete, tea.KeyCtrlD: // ^D, delete char under cursor
|
||||||
if len(m.value) > 0 && m.pos < len(m.value) {
|
if len(m.value) > 0 && m.pos < len(m.value) {
|
||||||
m.value = append(m.value[:m.pos], m.value[m.pos+1:]...)
|
m.value = append(m.value[:m.pos], m.value[m.pos+1:]...)
|
||||||
}
|
}
|
||||||
case key.Matches(msg, m.KeyMap.LineEnd):
|
case tea.KeyCtrlE, tea.KeyEnd: // ^E, go to end
|
||||||
m.CursorEnd()
|
resetBlink = m.cursorEnd()
|
||||||
case key.Matches(msg, m.KeyMap.DeleteAfterCursor):
|
case tea.KeyCtrlK: // ^K, kill text after cursor
|
||||||
m.deleteAfterCursor()
|
resetBlink = m.deleteAfterCursor()
|
||||||
case key.Matches(msg, m.KeyMap.DeleteBeforeCursor):
|
case tea.KeyCtrlU: // ^U, kill text before cursor
|
||||||
m.deleteBeforeCursor()
|
resetBlink = m.deleteBeforeCursor()
|
||||||
case key.Matches(msg, m.KeyMap.Paste):
|
case tea.KeyCtrlV: // ^V paste
|
||||||
return m, Paste
|
return m, Paste
|
||||||
case key.Matches(msg, m.KeyMap.DeleteWordForward):
|
case tea.KeyRunes, tea.KeySpace: // input regular characters
|
||||||
m.deleteWordForward()
|
if msg.Alt && len(msg.Runes) == 1 {
|
||||||
default:
|
if msg.Runes[0] == 'd' { // alt+d, delete word right of cursor
|
||||||
|
resetBlink = m.deleteWordRight()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if msg.Runes[0] == 'b' { // alt+b, back one word
|
||||||
|
resetBlink = m.wordLeft()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if msg.Runes[0] == 'f' { // alt+f, forward one word
|
||||||
|
resetBlink = m.wordRight()
|
||||||
|
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 {
|
||||||
runes := msg.Runes
|
runes := msg.Runes
|
||||||
@@ -565,31 +681,59 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|||||||
value = append(value[:m.pos], append(runes, value[m.pos:]...)...)
|
value = append(value[:m.pos], append(runes, value[m.pos:]...)...)
|
||||||
m.SetValue(string(value))
|
m.SetValue(string(value))
|
||||||
if m.Err == nil {
|
if m.Err == nil {
|
||||||
m.SetCursor(m.pos + len(runes))
|
resetBlink = m.setCursor(m.pos + len(runes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case initialBlinkMsg:
|
||||||
|
// We accept all initialBlinkMsgs genrated by the Blink command.
|
||||||
|
|
||||||
|
if m.cursorMode != CursorBlink || !m.focus {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := m.blinkCmd()
|
||||||
|
return m, cmd
|
||||||
|
|
||||||
|
case blinkMsg:
|
||||||
|
// We're choosy about whether to accept blinkMsgs so that our cursor
|
||||||
|
// only exactly when it should.
|
||||||
|
|
||||||
|
// Is this model blinkable?
|
||||||
|
if m.cursorMode != CursorBlink || !m.focus {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Were we expecting this blink message?
|
||||||
|
if msg.id != m.id || msg.tag != m.blinkTag {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var cmd tea.Cmd
|
||||||
|
if m.cursorMode == CursorBlink {
|
||||||
|
m.blink = !m.blink
|
||||||
|
cmd = m.blinkCmd()
|
||||||
|
}
|
||||||
|
return m, cmd
|
||||||
|
|
||||||
|
case blinkCanceled: // no-op
|
||||||
|
return m, nil
|
||||||
|
|
||||||
case pasteMsg:
|
case pasteMsg:
|
||||||
m.handlePaste(string(msg))
|
resetBlink = m.handlePaste(string(msg))
|
||||||
|
|
||||||
case pasteErrMsg:
|
case pasteErrMsg:
|
||||||
m.Err = msg
|
m.Err = msg
|
||||||
}
|
}
|
||||||
|
|
||||||
var cmds []tea.Cmd
|
|
||||||
var cmd tea.Cmd
|
var cmd tea.Cmd
|
||||||
|
if resetBlink {
|
||||||
m.Cursor, cmd = m.Cursor.Update(msg)
|
cmd = m.blinkCmd()
|
||||||
cmds = append(cmds, cmd)
|
|
||||||
|
|
||||||
if oldPos != m.pos {
|
|
||||||
m.Cursor.Blink = false
|
|
||||||
cmds = append(cmds, m.Cursor.BlinkCmd())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.handleOverflow()
|
m.handleOverflow()
|
||||||
return m, tea.Batch(cmds...)
|
return m, cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// View renders the textinput in its current state.
|
// View renders the textinput in its current state.
|
||||||
@@ -606,13 +750,10 @@ func (m Model) View() string {
|
|||||||
v := styleText(m.echoTransform(string(value[:pos])))
|
v := styleText(m.echoTransform(string(value[:pos])))
|
||||||
|
|
||||||
if pos < len(value) {
|
if pos < len(value) {
|
||||||
char := m.echoTransform(string(value[pos]))
|
v += m.cursorView(m.echoTransform(string(value[pos]))) // cursor and text under it
|
||||||
m.Cursor.SetChar(char)
|
|
||||||
v += m.Cursor.View() // cursor and text under it
|
|
||||||
v += styleText(m.echoTransform(string(value[pos+1:]))) // text after cursor
|
v += styleText(m.echoTransform(string(value[pos+1:]))) // text after cursor
|
||||||
} else {
|
} else {
|
||||||
m.Cursor.SetChar(" ")
|
v += m.cursorView(" ")
|
||||||
v += m.Cursor.View()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@@ -637,9 +778,12 @@ func (m Model) placeholderView() string {
|
|||||||
style = m.PlaceholderStyle.Inline(true).Render
|
style = m.PlaceholderStyle.Inline(true).Render
|
||||||
)
|
)
|
||||||
|
|
||||||
m.Cursor.TextStyle = m.PlaceholderStyle
|
// Cursor
|
||||||
m.Cursor.SetChar(p[:1])
|
if m.blink {
|
||||||
v += m.Cursor.View()
|
v += m.cursorView(style(p[:1]))
|
||||||
|
} else {
|
||||||
|
v += m.cursorView(p[:1])
|
||||||
|
}
|
||||||
|
|
||||||
// The rest of the placeholder text
|
// The rest of the placeholder text
|
||||||
v += style(p[1:])
|
v += style(p[1:])
|
||||||
@@ -647,9 +791,42 @@ func (m Model) placeholderView() string {
|
|||||||
return m.PromptStyle.Render(m.Prompt) + v
|
return m.PromptStyle.Render(m.Prompt) + v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cursorView styles the cursor.
|
||||||
|
func (m Model) cursorView(v string) string {
|
||||||
|
if m.blink {
|
||||||
|
return m.TextStyle.Render(v)
|
||||||
|
}
|
||||||
|
return m.CursorStyle.Inline(true).Reverse(true).Render(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// blinkCmd is an internal command used to manage cursor blinking.
|
||||||
|
func (m *Model) blinkCmd() tea.Cmd {
|
||||||
|
if m.cursorMode != CursorBlink {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.blinkCtx != nil && m.blinkCtx.cancel != nil {
|
||||||
|
m.blinkCtx.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(m.blinkCtx.ctx, m.BlinkSpeed)
|
||||||
|
m.blinkCtx.cancel = cancel
|
||||||
|
|
||||||
|
m.blinkTag++
|
||||||
|
|
||||||
|
return func() tea.Msg {
|
||||||
|
defer cancel()
|
||||||
|
<-ctx.Done()
|
||||||
|
if ctx.Err() == context.DeadlineExceeded {
|
||||||
|
return blinkMsg{id: m.id, tag: m.blinkTag}
|
||||||
|
}
|
||||||
|
return blinkCanceled{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Blink is a command used to initialize cursor blinking.
|
// Blink is a command used to initialize cursor blinking.
|
||||||
func Blink() tea.Msg {
|
func Blink() tea.Msg {
|
||||||
return cursor.Blink()
|
return initialBlinkMsg{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Paste is a command for pasting from the clipboard into the text input.
|
// Paste is a command for pasting from the clipboard into the text input.
|
||||||
@@ -681,31 +858,3 @@ func max(a, b int) int {
|
|||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deprecated.
|
|
||||||
|
|
||||||
// Deprecated: use cursor.Mode.
|
|
||||||
type CursorMode int
|
|
||||||
|
|
||||||
const (
|
|
||||||
// Deprecated: use cursor.CursorBlink.
|
|
||||||
CursorBlink = CursorMode(cursor.CursorBlink)
|
|
||||||
// Deprecated: use cursor.CursorStatic.
|
|
||||||
CursorStatic = CursorMode(cursor.CursorStatic)
|
|
||||||
// Deprecated: use cursor.CursorHide.
|
|
||||||
CursorHide = CursorMode(cursor.CursorHide)
|
|
||||||
)
|
|
||||||
|
|
||||||
func (c CursorMode) String() string {
|
|
||||||
return cursor.Mode(c).String()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deprecated: use cursor.Mode().
|
|
||||||
func (m Model) CursorMode() CursorMode {
|
|
||||||
return CursorMode(m.Cursor.Mode())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deprecated: use cursor.SetMode().
|
|
||||||
func (m *Model) SetCursorMode(mode CursorMode) tea.Cmd {
|
|
||||||
return m.Cursor.SetMode(cursor.Mode(mode))
|
|
||||||
}
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
package viewport
|
package viewport
|
||||||
|
|
||||||
import "github.com/Maks1mS/bubbles/key"
|
import "github.com/charmbracelet/bubbles/key"
|
||||||
|
|
||||||
const spacebar = " "
|
const spacebar = " "
|
||||||
|
|
||||||
|
@@ -4,7 +4,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Maks1mS/bubbles/key"
|
"github.com/charmbracelet/bubbles/key"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
)
|
)
|
||||||
@@ -207,16 +207,6 @@ func (m *Model) LineUp(n int) (lines []string) {
|
|||||||
return m.visibleLines()
|
return m.visibleLines()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TotalLineCount returns the total number of lines (both hidden and visible) within the viewport.
|
|
||||||
func (m Model) TotalLineCount() int {
|
|
||||||
return len(m.lines)
|
|
||||||
}
|
|
||||||
|
|
||||||
// VisibleLineCount returns the number of the visible lines within the viewport.
|
|
||||||
func (m Model) VisibleLineCount() int {
|
|
||||||
return len(m.visibleLines())
|
|
||||||
}
|
|
||||||
|
|
||||||
// GotoTop sets the viewport to the top position.
|
// GotoTop sets the viewport to the top position.
|
||||||
func (m *Model) GotoTop() (lines []string) {
|
func (m *Model) GotoTop() (lines []string) {
|
||||||
if m.AtTop() {
|
if m.AtTop() {
|
||||||
@@ -252,6 +242,7 @@ func Sync(m Model) tea.Cmd {
|
|||||||
//
|
//
|
||||||
// lines := model.ViewDown(1)
|
// lines := model.ViewDown(1)
|
||||||
// cmd := ViewDown(m, lines)
|
// cmd := ViewDown(m, lines)
|
||||||
|
//
|
||||||
func ViewDown(m Model, lines []string) tea.Cmd {
|
func ViewDown(m Model, lines []string) tea.Cmd {
|
||||||
if len(lines) == 0 {
|
if len(lines) == 0 {
|
||||||
return nil
|
return nil
|
||||||
@@ -359,23 +350,18 @@ func (m Model) View() string {
|
|||||||
return strings.Repeat("\n", max(0, m.Height-1))
|
return strings.Repeat("\n", max(0, m.Height-1))
|
||||||
}
|
}
|
||||||
|
|
||||||
w, h := m.Width, m.Height
|
lines := m.visibleLines()
|
||||||
if sw := m.Style.GetWidth(); sw != 0 {
|
|
||||||
w = min(w, sw)
|
// Fill empty space with newlines
|
||||||
|
extraLines := ""
|
||||||
|
if len(lines) < m.Height {
|
||||||
|
extraLines = strings.Repeat("\n", max(0, m.Height-len(lines)))
|
||||||
}
|
}
|
||||||
if sh := m.Style.GetHeight(); sh != 0 {
|
|
||||||
h = min(h, sh)
|
|
||||||
}
|
|
||||||
contentWidth := w - m.Style.GetHorizontalFrameSize()
|
|
||||||
contentHeight := h - m.Style.GetVerticalFrameSize()
|
|
||||||
contents := lipgloss.NewStyle().
|
|
||||||
Height(contentHeight). // pad to height.
|
|
||||||
MaxHeight(contentHeight). // truncate height if taller.
|
|
||||||
MaxWidth(contentWidth). // truncate width.
|
|
||||||
Render(strings.Join(m.visibleLines(), "\n"))
|
|
||||||
return m.Style.Copy().
|
return m.Style.Copy().
|
||||||
UnsetWidth().UnsetHeight(). // Style size already applied in contents.
|
UnsetWidth().
|
||||||
Render(contents)
|
UnsetHeight().
|
||||||
|
Render(strings.Join(lines, "\n") + extraLines)
|
||||||
}
|
}
|
||||||
|
|
||||||
func clamp(v, low, high int) int {
|
func clamp(v, low, high int) int {
|
||||||
|
Reference in New Issue
Block a user