fix: make pipeline for tui work

* 'cat foo.md | glow -t'

Signed-off-by: shane.xb.qian <shane.qian@foxmail.com>
This commit is contained in:
shane.xb.qian 2025-02-12 19:29:10 +08:00 committed by Andrey Nering
commit 9dd8a6f3fd
2 changed files with 16 additions and 8 deletions

10
main.go
View file

@ -226,7 +226,7 @@ func execute(cmd *cobra.Command, args []string) error {
switch len(args) {
// TUI running on cwd
case 0:
return runTUI("")
return runTUI("", "")
// TUI with possible dir argument
case 1:
@ -236,7 +236,7 @@ func execute(cmd *cobra.Command, args []string) error {
if err == nil && info.IsDir() {
p, err := filepath.Abs(args[0])
if err == nil {
return runTUI(p)
return runTUI(p, "")
}
}
fallthrough
@ -318,14 +318,14 @@ func executeCLI(cmd *cobra.Command, src *source, w io.Writer) error {
c.Stdout = os.Stdout
return c.Run()
case tui || cmd.Flags().Changed("tui"):
return runTUI(src.URL)
return runTUI(src.URL, s)
default:
_, err = fmt.Fprint(w, out)
return err
}
}
func runTUI(path string) error {
func runTUI(path string, s string) error {
// Read environment to get debugging stuff
cfg, err := env.ParseAs[ui.Config]()
if err != nil {
@ -345,7 +345,7 @@ func runTUI(path string) error {
cfg.PreserveNewLines = preserveNewLines
// Run Bubble Tea program
if _, err := ui.NewProgram(cfg).Run(); err != nil {
if _, err := ui.NewProgram(cfg, s).Run(); err != nil {
return err
}

View file

@ -29,7 +29,7 @@ var (
)
// NewProgram returns a new Tea program.
func NewProgram(cfg Config) *tea.Program {
func NewProgram(cfg Config, s string) *tea.Program {
log.Debug(
"Starting glow",
"high_perf_pager",
@ -43,7 +43,7 @@ func NewProgram(cfg Config) *tea.Program {
if cfg.EnableMouse {
opts = append(opts, tea.WithMouseCellMotion())
}
m := newModel(cfg)
m := newModel(cfg, s)
return tea.NewProgram(m, opts...)
}
@ -129,7 +129,7 @@ func (m *model) unloadDocument() []tea.Cmd {
return batch
}
func newModel(cfg Config) tea.Model {
func newModel(cfg Config, s string) tea.Model {
initSections()
if cfg.GlamourStyle == styles.AutoStyle {
@ -152,6 +152,14 @@ func newModel(cfg Config) tea.Model {
}
path := cfg.Path
if path == "" && s != "" {
m.state = stateShowDocument
m.pager.currentDocument = markdown{
Body: s,
}
return m
}
if path == "" {
path = "."
}