Browse Source

docs: docstrings

main
decentral1se 1 year ago
parent
commit
43a4f9b483
No known key found for this signature in database GPG Key ID: 3789458B3D0C410
  1. 34
      gshmm.go

34
gshmm.go

@ -1,3 +1,4 @@
// go-sh-manymanuals TODO
package main package main
import ( import (
@ -15,6 +16,7 @@ import (
"github.com/sahilm/fuzzy" "github.com/sahilm/fuzzy"
) )
// help is the command-line interface help output.
const help = `go-sh-manymanuals: TODO const help = `go-sh-manymanuals: TODO
TODO TODO
@ -27,13 +29,20 @@ Options:
// required before the filtering logic commences actually filtering. // required before the filtering logic commences actually filtering.
const minCharsUntilFilter = 2 const minCharsUntilFilter = 2
// helpFlag is the help flag for the command-line interface.
var helpFlag bool var helpFlag bool
// handleCliFlags handles command-line flags.
func handleCliFlags() { func handleCliFlags() {
flag.BoolVar(&helpFlag, "h", false, "output help") flag.BoolVar(&helpFlag, "h", false, "output help")
flag.Parse() flag.Parse()
} }
// readPDF reads the plain text contents of a PDF. This does not include the
// formatting. Only the content you see when you view it through a PDF reader.
// The library we're using makes use of the C bindings to the Poppler PDF
// library https://poppler.freedesktop.org which appears to offer a good mix of
// reliability and effectiveness.
func readPDF(name string) (string, error) { func readPDF(name string) (string, error) {
doc, err := pdf.Open(name) doc, err := pdf.Open(name)
if err != nil { if err != nil {
@ -49,15 +58,17 @@ func readPDF(name string) (string, error) {
return txt, nil return txt, nil
} }
// model offers the core of the state for the entire UI.
type model struct { type model struct {
filterInput textinput.Model // fuzzy search interface filterInput textinput.Model // Fuzzy search interface
datasheets []datasheet // all datasheets under cwd datasheets []datasheet // All datasheets under cwd
dataSheetsView []string // filtered view on all datasheets dataSheetsView []string // Filtered view on all datasheets
dataSheetViewport viewport.Model dataSheetViewport viewport.Model // Viewport for the PDF content
} }
// TODO: cache this somewhere, it's called several times... in the model? // dataSheetNames lists all datasheet names.
func (m model) dataSheetNames() []string { func (m model) dataSheetNames() []string {
// TODO: cache this somewhere, it's called several times... in the model?
var names []string var names []string
for _, datasheet := range m.datasheets { for _, datasheet := range m.datasheets {
names = append(names, datasheet.filename) names = append(names, datasheet.filename)
@ -65,6 +76,7 @@ func (m model) dataSheetNames() []string {
return names return names
} }
// datasheetFromName retrieves a datasheet via a name.
func (m model) datasheetFromName(name string) string { func (m model) datasheetFromName(name string) string {
for _, d := range m.datasheets { for _, d := range m.datasheets {
if d.filename == name { if d.filename == name {
@ -74,12 +86,14 @@ func (m model) datasheetFromName(name string) string {
return "" return ""
} }
// datasheet represents a datasheet on disk.
type datasheet struct { type datasheet struct {
filename string filename string // The name of the file
absPath string absPath string // the absolute file path
contents string contents string // the contents of the PDF
} }
// initialModel constucts an initial state for the UI.
func initialModel() model { func initialModel() model {
input := textinput.New() input := textinput.New()
input.Focus() input.Focus()
@ -123,10 +137,12 @@ func initialModel() model {
return m return m
} }
// Init initialises the program.
func (m model) Init() tea.Cmd { func (m model) Init() tea.Cmd {
return textinput.Blink return textinput.Blink
} }
// Update updates the program state.
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var ( var (
cmd tea.Cmd cmd tea.Cmd
@ -178,6 +194,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(cmds...) return m, tea.Batch(cmds...)
} }
// View outputs the program state for viewing.
func (m model) View() string { func (m model) View() string {
body := strings.Builder{} body := strings.Builder{}
@ -194,6 +211,7 @@ func (m model) View() string {
return body.String() return body.String()
} }
// main is the command-line entrypoint.
func main() { func main() {
handleCliFlags() handleCliFlags()

Loading…
Cancel
Save