2024-05-30 00:44:40 -06:00
|
|
|
package util
|
2024-03-16 15:56:45 -06:00
|
|
|
|
|
|
|
import (
|
2024-05-30 00:44:40 -06:00
|
|
|
"fmt"
|
2024-03-16 15:56:45 -06:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2024-03-30 20:03:53 -06:00
|
|
|
"github.com/charmbracelet/bubbles/viewport"
|
2024-03-16 15:56:45 -06:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2024-05-30 00:44:40 -06:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2024-03-30 20:03:53 -06:00
|
|
|
"github.com/muesli/reflow/ansi"
|
2024-03-16 15:56:45 -06:00
|
|
|
)
|
|
|
|
|
2024-05-30 00:44:40 -06:00
|
|
|
type MsgTempfileEditorClosed string
|
2024-03-16 15:56:45 -06:00
|
|
|
|
2024-05-30 12:22:56 -06:00
|
|
|
// OpenTempfileEditor opens $EDITOR on a temporary file with the given content.
|
|
|
|
// Upon closing, the contents of the file are read and returned wrapped in a
|
|
|
|
// MsgTempfileEditorClosed
|
2024-05-30 00:44:40 -06:00
|
|
|
func OpenTempfileEditor(pattern string, content string, placeholder string) tea.Cmd {
|
2024-03-16 15:56:45 -06:00
|
|
|
msgFile, _ := os.CreateTemp("/tmp", pattern)
|
|
|
|
|
|
|
|
err := os.WriteFile(msgFile.Name(), []byte(placeholder+content), os.ModeAppend)
|
|
|
|
if err != nil {
|
2024-05-30 00:44:40 -06:00
|
|
|
return func() tea.Msg { return err }
|
2024-03-16 15:56:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
editor := os.Getenv("EDITOR")
|
|
|
|
if editor == "" {
|
|
|
|
editor = "vim"
|
|
|
|
}
|
|
|
|
|
|
|
|
c := exec.Command(editor, msgFile.Name())
|
|
|
|
return tea.ExecProcess(c, func(err error) tea.Msg {
|
|
|
|
bytes, err := os.ReadFile(msgFile.Name())
|
|
|
|
if err != nil {
|
2024-05-30 00:44:40 -06:00
|
|
|
return err
|
2024-03-16 15:56:45 -06:00
|
|
|
}
|
2024-03-29 16:26:28 -06:00
|
|
|
os.Remove(msgFile.Name())
|
2024-03-16 15:56:45 -06:00
|
|
|
fileContents := string(bytes)
|
|
|
|
if strings.HasPrefix(fileContents, placeholder) {
|
|
|
|
fileContents = fileContents[len(placeholder):]
|
|
|
|
}
|
|
|
|
stripped := strings.Trim(fileContents, "\n \t")
|
2024-05-30 00:44:40 -06:00
|
|
|
return MsgTempfileEditorClosed(stripped)
|
2024-03-16 15:56:45 -06:00
|
|
|
})
|
|
|
|
}
|
2024-03-30 20:03:53 -06:00
|
|
|
|
2024-05-30 12:22:56 -06:00
|
|
|
// similar to lipgloss.Height, except returns 0 instead of 1 on empty strings
|
2024-05-30 00:44:40 -06:00
|
|
|
func Height(str string) int {
|
2024-03-30 20:03:53 -06:00
|
|
|
if str == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return strings.Count(str, "\n") + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// truncate a string until its rendered cell width + the provided tail fits
|
|
|
|
// within the given width
|
2024-05-30 00:44:40 -06:00
|
|
|
func TruncateToCellWidth(str string, width int, tail string) string {
|
2024-03-30 20:03:53 -06:00
|
|
|
cellWidth := ansi.PrintableRuneWidth(str)
|
|
|
|
if cellWidth <= width {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
tailWidth := ansi.PrintableRuneWidth(tail)
|
|
|
|
for {
|
|
|
|
str = str[:len(str)-((cellWidth+tailWidth)-width)]
|
|
|
|
cellWidth = ansi.PrintableRuneWidth(str)
|
|
|
|
if cellWidth+tailWidth <= max(width, 0) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str + tail
|
|
|
|
}
|
|
|
|
|
2024-05-30 00:44:40 -06:00
|
|
|
func ScrollIntoView(vp *viewport.Model, offset int, edge int) {
|
2024-03-30 20:03:53 -06:00
|
|
|
currentOffset := vp.YOffset
|
|
|
|
if offset >= currentOffset && offset < currentOffset+vp.Height {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
distance := currentOffset - offset
|
|
|
|
if distance < 0 {
|
|
|
|
// we should scroll down until it just comes into view
|
2024-04-02 00:53:29 -06:00
|
|
|
vp.SetYOffset(currentOffset - (distance + (vp.Height - edge)) + 1)
|
2024-03-30 20:03:53 -06:00
|
|
|
} else {
|
|
|
|
// we should scroll up
|
2024-04-02 00:53:29 -06:00
|
|
|
vp.SetYOffset(currentOffset - distance - edge)
|
2024-03-30 20:03:53 -06:00
|
|
|
}
|
|
|
|
}
|
2024-05-30 00:44:40 -06:00
|
|
|
|
|
|
|
func ErrorBanner(err error, width int) string {
|
|
|
|
if err == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return lipgloss.NewStyle().
|
|
|
|
Width(width).
|
|
|
|
AlignHorizontal(lipgloss.Center).
|
|
|
|
Bold(true).
|
|
|
|
Foreground(lipgloss.Color("1")).
|
|
|
|
Render(fmt.Sprintf("%s", err))
|
|
|
|
}
|
|
|
|
|