Skip to content

Commit

Permalink
pam/userselection: Hide cursor when we're not in interactive terminal…
Browse files Browse the repository at this point in the history
… mode

It's causing events to happen that we don't really need in this case
and also it's causing races.

See: charmbracelet/bubbletea#909
  • Loading branch information
3v1n0 committed Feb 26, 2024
1 parent afc03e7 commit 3c73890
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pam/internal/adapter/model.go
Expand Up @@ -100,7 +100,7 @@ type ChangeStage struct {
// Init initializes the main model orchestrator.
func (m *UIModel) Init() tea.Cmd {
m.exitStatus = pamError{status: pam.ErrSystem, msg: "model did not return anything"}
m.userSelectionModel = newUserSelectionModel(m.PamMTx)
m.userSelectionModel = newUserSelectionModel(m.PamMTx, m.ClientType)
var cmds []tea.Cmd
cmds = append(cmds, m.userSelectionModel.Init())

Expand Down
14 changes: 11 additions & 3 deletions pam/internal/adapter/userselection.go
@@ -1,6 +1,7 @@
package adapter

import (
"github.com/charmbracelet/bubbles/cursor"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/msteinert/pam/v2"
Expand All @@ -10,7 +11,8 @@ import (
type userSelectionModel struct {
textinput.Model

pamMTx pam.ModuleTransaction
pamMTx pam.ModuleTransaction
clientType PamClientType
}

// userSelected events to select a new username.
Expand All @@ -26,16 +28,22 @@ func sendUserSelected(username string) tea.Cmd {
}

// newUserSelectionModel returns an initialized userSelectionModel.
func newUserSelectionModel(pamMTx pam.ModuleTransaction) userSelectionModel {
func newUserSelectionModel(pamMTx pam.ModuleTransaction, clientType PamClientType) userSelectionModel {
u := textinput.New()
if clientType != InteractiveTerminal {
// Cursor events are racy: https://github.com/charmbracelet/bubbletea/issues/909.
// FIXME: Avoid initializing the text input Model at all.
u.Cursor.SetMode(cursor.CursorHide)
}
u.Prompt = "Username: " // TODO: i18n
u.Placeholder = "user name"

//TODO: u.Validate
return userSelectionModel{
Model: u,

pamMTx: pamMTx,
pamMTx: pamMTx,
clientType: clientType,
}
}

Expand Down

0 comments on commit 3c73890

Please sign in to comment.