mirror of
https://github.com/anotherhadi/ilovetui.git
synced 2026-10-05 10:58:26 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b22ce7cbf7 | ||
|
|
0648e7a761 |
+33
@@ -0,0 +1,33 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import tea "charm.land/bubbletea/v2"
|
||||||
|
|
||||||
|
type QuitMsg struct{}
|
||||||
|
|
||||||
|
func Quit() tea.Cmd {
|
||||||
|
return func() tea.Msg { return QuitMsg{} }
|
||||||
|
}
|
||||||
|
|
||||||
|
type FocusMsg struct{}
|
||||||
|
|
||||||
|
func Focus() tea.Cmd {
|
||||||
|
return func() tea.Msg { return FocusMsg{} }
|
||||||
|
}
|
||||||
|
|
||||||
|
type BlurMsg struct{}
|
||||||
|
|
||||||
|
func Blur() tea.Cmd {
|
||||||
|
return func() tea.Msg { return BlurMsg{} }
|
||||||
|
}
|
||||||
|
|
||||||
|
type RequestFocusMsg struct{}
|
||||||
|
|
||||||
|
func RequestFocus() tea.Cmd {
|
||||||
|
return func() tea.Msg { return RequestFocusMsg{} }
|
||||||
|
}
|
||||||
|
|
||||||
|
type SetTitleMsg struct{ Title string }
|
||||||
|
|
||||||
|
func SetTitle(title string) tea.Cmd {
|
||||||
|
return func() tea.Msg { return SetTitleMsg{Title: title} }
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package minsize
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"charm.land/lipgloss/v2"
|
||||||
|
|
||||||
|
"github.com/anotherhadi/ilovetui/style"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Model struct {
|
||||||
|
MinWidth int
|
||||||
|
MinHeight int
|
||||||
|
Style lipgloss.Style
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option func(*Model)
|
||||||
|
|
||||||
|
func WithStyle(s lipgloss.Style) Option {
|
||||||
|
return func(m *Model) { m.Style = s }
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(minWidth, minHeight int, opts ...Option) Model {
|
||||||
|
m := Model{
|
||||||
|
MinWidth: minWidth,
|
||||||
|
MinHeight: minHeight,
|
||||||
|
Style: lipgloss.NewStyle().Foreground(style.S.Muted),
|
||||||
|
}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&m)
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Model) Fits(width, height int) bool {
|
||||||
|
return width >= m.MinWidth && height >= m.MinHeight
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Model) View(width, height int) string {
|
||||||
|
msg := m.Style.Render(m.message(width, height))
|
||||||
|
if width < 1 || height < 1 {
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
return lipgloss.Place(width, height, lipgloss.Center, lipgloss.Center, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Model) message(width, height int) string {
|
||||||
|
tooNarrow := width < m.MinWidth
|
||||||
|
tooShort := height < m.MinHeight
|
||||||
|
switch {
|
||||||
|
case tooNarrow && tooShort:
|
||||||
|
return fmt.Sprintf("minimum is %dx%d", m.MinWidth, m.MinHeight)
|
||||||
|
case tooNarrow:
|
||||||
|
return fmt.Sprintf("minimum width: %d", m.MinWidth)
|
||||||
|
case tooShort:
|
||||||
|
return fmt.Sprintf("minimum height: %d", m.MinHeight)
|
||||||
|
default:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user