2021-12-13 09:38:07 +02:00
|
|
|
// gorc project
|
|
|
|
|
// Copyright (C) 2021 IllusionMan1212
|
|
|
|
|
//
|
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation; either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
|
// with this program; if not, see https://www.gnu.org/licenses.
|
|
|
|
|
|
|
|
|
|
package mainscreen
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2021-12-25 21:45:58 +02:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2022-01-05 00:54:54 +02:00
|
|
|
"github.com/illusionman1212/gorc/client"
|
2021-12-13 09:38:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type InputState struct {
|
2022-01-05 00:54:54 +02:00
|
|
|
Input textinput.Model
|
|
|
|
|
Style lipgloss.Style
|
|
|
|
|
Client *client.Client
|
2021-12-13 09:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-09 04:30:22 +02:00
|
|
|
func NewInputBox() InputState {
|
2022-01-15 22:50:38 +02:00
|
|
|
input := textinput.New()
|
2021-12-13 09:38:07 +02:00
|
|
|
input.Placeholder = "Send a message..."
|
2021-12-31 00:04:58 +02:00
|
|
|
input.Focus()
|
2021-12-13 09:38:07 +02:00
|
|
|
|
2022-01-09 04:30:22 +02:00
|
|
|
return InputState{
|
|
|
|
|
Input: input,
|
|
|
|
|
Style: InputboxStyle.Copy(),
|
|
|
|
|
}
|
2021-12-13 09:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s InputState) Update(msg tea.Msg) (InputState, tea.Cmd) {
|
2022-01-05 00:54:54 +02:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
case tea.KeyMsg:
|
|
|
|
|
key := msg.String()
|
|
|
|
|
switch key {
|
|
|
|
|
case "enter":
|
|
|
|
|
value := s.Input.Value()
|
2022-01-09 04:30:22 +02:00
|
|
|
if len(value) == 0 {
|
|
|
|
|
return s, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 00:54:54 +02:00
|
|
|
s.Input.Reset()
|
|
|
|
|
|
|
|
|
|
return s, s.SendingPrivMsg(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 21:45:58 +02:00
|
|
|
var cmd tea.Cmd
|
|
|
|
|
s.Input, cmd = s.Input.Update(msg)
|
2021-12-31 00:04:58 +02:00
|
|
|
return s, cmd
|
2021-12-25 21:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-15 21:56:06 +02:00
|
|
|
func (s *InputState) Focus() {
|
|
|
|
|
s.Input.Focus()
|
|
|
|
|
s.Style = s.Style.Copy().BorderForeground(lipgloss.Color("105"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *InputState) Blur() {
|
|
|
|
|
s.Input.Blur()
|
|
|
|
|
s.Style = s.Style.Copy().BorderForeground(lipgloss.Color("#EEE"))
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 21:45:58 +02:00
|
|
|
func (s *InputState) SetSize(width int) {
|
2022-01-15 21:56:06 +02:00
|
|
|
// set a max width for the input field so it scrolls horizontally instead of wrapping to a newline
|
|
|
|
|
// -4 for input prompt char, cursor, and some extra magical padding
|
|
|
|
|
s.Input.Width = width - s.Style.GetHorizontalFrameSize() - 4
|
2021-12-25 21:45:58 +02:00
|
|
|
s.Style = s.Style.Width(width - s.Style.GetHorizontalBorderSize())
|
2021-12-13 09:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s InputState) View() string {
|
2021-12-25 21:45:58 +02:00
|
|
|
return s.Style.Render(s.Input.View())
|
2021-12-13 09:38:07 +02:00
|
|
|
}
|