Skip to content

feat: handle focus/blur events #749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions filter/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (o Options) Run() error {

options := []tea.ProgramOption{
tea.WithOutput(os.Stderr),
tea.WithReportFocus(),
tea.WithContext(ctx),
}
if o.Height == 0 {
Expand Down
9 changes: 4 additions & 5 deletions filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ type model struct {
strict bool
}

func (m model) Init() tea.Cmd { return nil }
func (m model) Init() tea.Cmd { return textinput.Blink }

func (m model) View() string {
if m.quitting {
Expand Down Expand Up @@ -229,7 +229,8 @@ func (m model) helpView() string {
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmd, icmd tea.Cmd
m.textinput, icmd = m.textinput.Update(msg)
switch msg := msg.(type) {
case tea.WindowSizeMsg:
if m.height == 0 || m.height > msg.Height {
Expand Down Expand Up @@ -278,8 +279,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
m.ToggleSelection()
default:
m.textinput, cmd = m.textinput.Update(msg)

// yOffsetFromBottom is the number of lines from the bottom of the
// list to the top of the viewport. This is used to keep the viewport
// at a constant position when the number of matches are reduced
Expand Down Expand Up @@ -324,7 +323,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// It's possible that filtering items have caused fewer matches. So, ensure
// that the selected index is within the bounds of the number of matches.
m.cursor = clamp(0, len(m.matches)-1, m.cursor)
return m, cmd
return m, tea.Batch(cmd, icmd)
}

func (m *model) CursorUp() {
Expand Down
1 change: 1 addition & 0 deletions input/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (o Options) Run() error {
p := tea.NewProgram(
m,
tea.WithOutput(os.Stderr),
tea.WithReportFocus(),
tea.WithContext(ctx),
)
tm, err := p.Run()
Expand Down
1 change: 1 addition & 0 deletions pager/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (o Options) Run() error {
_, err := tea.NewProgram(
m,
tea.WithAltScreen(),
tea.WithReportFocus(),
tea.WithContext(ctx),
).Run()
if err != nil {
Expand Down
20 changes: 12 additions & 8 deletions pager/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"strings"

"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
Expand All @@ -32,15 +33,17 @@ func (m model) Init() tea.Cmd { return nil }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.ProcessText(msg)
m.processText(msg)
case tea.KeyMsg:
return m.KeyHandler(msg)
return m.keyHandler(msg)
}

return m, nil
var cmd tea.Cmd
m.search.input, cmd = m.search.input.Update(msg)
return m, cmd
}

func (m *model) ProcessText(msg tea.WindowSizeMsg) {
func (m *model) processText(msg tea.WindowSizeMsg) {
m.viewport.Height = msg.Height - lipgloss.Height(m.helpStyle.Render("?")) - 1
m.viewport.Width = msg.Width
textStyle := lipgloss.NewStyle().Width(m.viewport.Width)
Expand Down Expand Up @@ -84,7 +87,7 @@ func (m *model) ProcessText(msg tea.WindowSizeMsg) {

const heightOffset = 2

func (m model) KeyHandler(key tea.KeyMsg) (model, func() tea.Msg) {
func (m model) keyHandler(key tea.KeyMsg) (model, tea.Cmd) {
var cmd tea.Cmd
if m.search.active {
switch key.String() {
Expand All @@ -95,7 +98,7 @@ func (m model) KeyHandler(key tea.KeyMsg) (model, func() tea.Msg) {

// Trigger a view update to highlight the found matches.
m.search.NextMatch(&m)
m.ProcessText(tea.WindowSizeMsg{Height: m.viewport.Height + heightOffset, Width: m.viewport.Width})
m.processText(tea.WindowSizeMsg{Height: m.viewport.Height + heightOffset, Width: m.viewport.Width})
} else {
m.search.Done()
}
Expand All @@ -112,12 +115,13 @@ func (m model) KeyHandler(key tea.KeyMsg) (model, func() tea.Msg) {
m.viewport.GotoBottom()
case "/":
m.search.Begin()
return m, textinput.Blink
case "p", "N":
m.search.PrevMatch(&m)
m.ProcessText(tea.WindowSizeMsg{Height: m.viewport.Height + heightOffset, Width: m.viewport.Width})
m.processText(tea.WindowSizeMsg{Height: m.viewport.Height + heightOffset, Width: m.viewport.Width})
case "n":
m.search.NextMatch(&m)
m.ProcessText(tea.WindowSizeMsg{Height: m.viewport.Height + heightOffset, Width: m.viewport.Width})
m.processText(tea.WindowSizeMsg{Height: m.viewport.Height + heightOffset, Width: m.viewport.Width})
case "q", "esc":
return m, tea.Quit
case "ctrl+c":
Expand Down
Loading