wip: very dodgy attempt at styles

This commit is contained in:
decentral1se 2023-05-10 19:48:55 +02:00
parent 6fd65285ae
commit 6bbd94cba6
No known key found for this signature in database
GPG Key ID: 03789458B3D0C410

View File

@ -4,6 +4,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"log"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -15,6 +16,7 @@ import (
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
pdf "github.com/johbar/go-poppler" pdf "github.com/johbar/go-poppler"
"github.com/sahilm/fuzzy" "github.com/sahilm/fuzzy"
"golang.org/x/term"
) )
// filenameFilterMode searches by PDF file name. // filenameFilterMode searches by PDF file name.
@ -67,17 +69,17 @@ func readPDF(name string) (string, error) {
// model offers the core of the state for the entire UI. // model offers the core of the state for the entire UI.
type model struct { type model struct {
input textinput.Model // Fuzzy search interface input textinput.Model // Fuzzy search interface
filterMode string // The filtering mode ("filename", "content")
datasheets []datasheet // All datasheets under cwd datasheets []datasheet // All datasheets under cwd
datasheetNames []string // All datasheet names (caching) datasheetNames []string // All datasheet names (caching)
filteredDatasheets []string // Filtered view on all datasheets datasheetsStyle lipgloss.Style // Style to use for listing datasheets
loadDatasheetSpinner spinner.Model // Spinner to show while loading datasheets filteredDatasheets []string // Filtered view on all datasheets
datasheetsLoaded bool // Whether or not the datasheets are loaded or not loadDatasheetSpinner spinner.Model // Spinner to show while loading datasheets
datasheetsLoaded bool // Whether or not the datasheets are loaded or not
datasheetViewport viewport.Model // Viewport for the PDF content datasheetViewport viewport.Model // Viewport for the PDF content
datasheetViewportStyle lipgloss.Style // Style to show while showing datasheet viewport
filterMode string // The filtering mode ("filename", "content")
} }
// datasheetFromName retrieves a datasheet via a name. // datasheetFromName retrieves a datasheet via a name.
@ -107,25 +109,38 @@ type datasheet struct {
} }
// initialModel constucts an initial state for the UI. // initialModel constucts an initial state for the UI.
func initialModel() model { func initialModel(width int, height int) model {
input := textinput.New() input := textinput.New()
input.Focus() input.Focus()
// TODO: set width/heigh to match terminal. this should also datasheetViewport := viewport.New(width/2+20, height/2)
// be set in relation to the list of filenames also. they datasheetViewportStyle := lipgloss.NewStyle().
// should have some visually pleasing ratio set i imagine BorderStyle(lipgloss.NormalBorder()).
viewp := viewport.New(60, 30) BorderForeground(lipgloss.Color("63")).
Padding(2)
loadDatasheetSpinner := spinner.New() loadDatasheetSpinner := spinner.New()
loadDatasheetSpinner.Spinner = spinner.Dot loadDatasheetSpinner.Spinner = spinner.Dot
loadDatasheetSpinner.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205")) loadDatasheetSpinner.Style = lipgloss.NewStyle().
Foreground(lipgloss.Color("205"))
datasheetsStyle := lipgloss.NewStyle().
Width(width/2 - 30).
Height(height/2 - 30).
MarginRight(2).
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("63")).
Padding(2)
m := model{ m := model{
input: input, input: input,
datasheetsLoaded: false, filterMode: filenameFilterMode,
loadDatasheetSpinner: loadDatasheetSpinner,
datasheetViewport: viewp, datasheetsLoaded: false,
filterMode: filenameFilterMode, loadDatasheetSpinner: loadDatasheetSpinner,
datasheetViewport: datasheetViewport,
datasheetViewportStyle: datasheetViewportStyle,
datasheetsStyle: datasheetsStyle,
} }
return m return m
@ -235,7 +250,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.datasheetViewport.SetContent(viewportText) m.datasheetViewport.SetContent(viewportText)
} }
// TODO: handle terminal resizing
switch msg := msg.(type) { switch msg := msg.(type) {
case loadedDatasheetsMsg: case loadedDatasheetsMsg:
m.datasheets = msg.datasheets m.datasheets = msg.datasheets
@ -246,6 +260,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.datasheetViewport.SetContent(selectedDatasheet) m.datasheetViewport.SetContent(selectedDatasheet)
m.datasheetsLoaded = true m.datasheetsLoaded = true
case tea.WindowSizeMsg:
// TODO: handle terminal resizing
// resize listing / viewport
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "ctrl+c": case "ctrl+c":
@ -271,7 +288,11 @@ func (m model) View() string {
} }
// TODO: style further with lipgloss, e.g. borders, margins, etc. // TODO: style further with lipgloss, e.g. borders, margins, etc.
panes := lipgloss.JoinHorizontal(lipgloss.Left, sheets, m.datasheetViewport.View()) panes := lipgloss.JoinHorizontal(
lipgloss.Left,
m.datasheetsStyle.Render(sheets),
m.datasheetViewportStyle.Render(m.datasheetViewport.View()),
)
body.WriteString(panes) body.WriteString(panes)
@ -303,13 +324,17 @@ func main() {
f, err := tea.LogToFile("debug.log", "debug") f, err := tea.LogToFile("debug.log", "debug")
if err != nil { if err != nil {
fmt.Println("fatal:", err) log.Fatal(err)
os.Exit(1)
} }
defer f.Close() defer f.Close()
width, height, err := term.GetSize(0)
if err != nil {
log.Fatal(err)
}
p := tea.NewProgram( p := tea.NewProgram(
initialModel(), initialModel(width, height),
tea.WithAltScreen(), tea.WithAltScreen(),
tea.WithMouseCellMotion(), tea.WithMouseCellMotion(),
) )