mirror of
https://github.com/anotherhadi/ilovetui.git
synced 2026-10-05 19:08:25 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2fd1861339 | ||
|
|
b22ce7cbf7 |
+1
-5
@@ -43,11 +43,7 @@ func (m Model) renderOne(d Drawer, background string, w, h int) string {
|
|||||||
x = max(w-bw, 0)
|
x = max(w-bw, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
compositor := lipgloss.NewCompositor(
|
return style.Overlay(dim(background, s.DimColor), box, x, 0)
|
||||||
lipgloss.NewLayer(dim(background, s.DimColor)),
|
|
||||||
lipgloss.NewLayer(box).X(x).Y(0).Z(1),
|
|
||||||
)
|
|
||||||
return compositor.Render()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func dim(s string, c color.Color) string {
|
func dim(s string, c color.Color) string {
|
||||||
|
|||||||
@@ -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 ""
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-5
@@ -42,11 +42,7 @@ func (m Model) renderOne(mo Modal, background string, w, h int) string {
|
|||||||
bw, bh := lipgloss.Width(box), lipgloss.Height(box)
|
bw, bh := lipgloss.Width(box), lipgloss.Height(box)
|
||||||
x, y := max((w-bw)/2, 0), max((h-bh)/2, 0)
|
x, y := max((w-bw)/2, 0), max((h-bh)/2, 0)
|
||||||
|
|
||||||
compositor := lipgloss.NewCompositor(
|
return style.Overlay(dim(background, s.DimColor), box, x, y)
|
||||||
lipgloss.NewLayer(dim(background, s.DimColor)),
|
|
||||||
lipgloss.NewLayer(box).X(x).Y(y).Z(1),
|
|
||||||
)
|
|
||||||
return compositor.Render()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func dim(s string, c color.Color) string {
|
func dim(s string, c color.Color) string {
|
||||||
|
|||||||
@@ -24,11 +24,7 @@ func (m Model) Render(background string) string {
|
|||||||
sw, sh := lipgloss.Width(stack), lipgloss.Height(stack)
|
sw, sh := lipgloss.Width(stack), lipgloss.Height(stack)
|
||||||
x, y := placement(m.position, w, h, sw, sh)
|
x, y := placement(m.position, w, h, sw, sh)
|
||||||
|
|
||||||
compositor := lipgloss.NewCompositor(
|
return style.Overlay(background, stack, x, y)
|
||||||
lipgloss.NewLayer(background),
|
|
||||||
lipgloss.NewLayer(stack).X(x).Y(y).Z(1),
|
|
||||||
)
|
|
||||||
return compositor.Render()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Model) View(width, height int) string {
|
func (m Model) View(width, height int) string {
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package style
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/x/ansi"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Overlay(base, top string, x, y int) string {
|
||||||
|
baseLines := strings.Split(base, "\n")
|
||||||
|
topLines := strings.Split(top, "\n")
|
||||||
|
|
||||||
|
for i, tl := range topLines {
|
||||||
|
row := y + i
|
||||||
|
if row < 0 || row >= len(baseLines) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
bl := baseLines[row]
|
||||||
|
tw := ansi.StringWidth(tl)
|
||||||
|
left := ansi.Cut(bl, 0, x)
|
||||||
|
right := ansi.Cut(bl, x+tw, ansi.StringWidth(bl))
|
||||||
|
baseLines[row] = left + tl + right
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(baseLines, "\n")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user