command.go
This file
Path
pager/command.go
59 lines · 1.5 KiB · Go
Last changed by
chore: prepare v2 (#1116)Drew Smirnoffbefore adoption02d6f06
History of this fileTake it
1package pager23import (4 "fmt"5 "regexp"67 "charm.land/bubbles/v2/help"8 "charm.land/bubbles/v2/viewport"9 tea "charm.land/bubbletea/v2"10 "charm.land/gum/v2/internal/stdin"11 "charm.land/gum/v2/internal/timeout"12)1314// Run provides a shell script interface for the viewport bubble.15// https://github.com/charmbracelet/bubbles/viewport16func (o Options) Run() error {17 vp := viewport.New(viewport.WithWidth(o.Style.Width), viewport.WithHeight(o.Style.Height))18 vp.Style = o.Style.ToLipgloss()1920 if o.Content == "" {21 stdin, err := stdin.Read()22 if err != nil {23 return fmt.Errorf("unable to read stdin")24 }25 if stdin != "" {26 // Sanitize the input from stdin by removing backspace sequences.27 backspace := regexp.MustCompile(".\x08")28 o.Content = backspace.ReplaceAllString(stdin, "")29 } else {30 return fmt.Errorf("provide some content to display")31 }32 }3334 m := model{35 viewport: vp,36 help: help.New(),37 content: o.Content,38 origContent: o.Content,39 showLineNumbers: o.ShowLineNumbers,40 lineNumberStyle: o.LineNumberStyle.ToLipgloss(),41 softWrap: o.SoftWrap,42 matchStyle: o.MatchStyle.ToLipgloss(),43 matchHighlightStyle: o.MatchHighlightStyle.ToLipgloss(),44 keymap: defaultKeymap(),45 }4647 ctx, cancel := timeout.Context(o.Timeout)48 defer cancel()4950 _, err := tea.NewProgram(51 m,52 tea.WithContext(ctx),53 ).Run()54 if err != nil {55 return fmt.Errorf("unable to start program: %w", err)56 }5758 return nil59}