Some checks failed
Docs Deploy / build_and_deploy (push) Has been cancelled
Generate Docs / cli (push) Has been cancelled
Generate Config Doc / cli (push) Has been cancelled
Go formatting / go-formatting (push) Has been cancelled
Check links / markdown-link-check (push) Has been cancelled
Integration / pre-test (push) Has been cancelled
Integration / test on (push) Has been cancelled
Integration / status (push) Has been cancelled
Lint / Lint Go code (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
50 lines
1 KiB
Go
50 lines
1 KiB
Go
package markdownviewer
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/charmbracelet/glow/ui"
|
|
"golang.org/x/term"
|
|
|
|
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/safeconverter"
|
|
)
|
|
|
|
// View starts the Markdown viewer at path that .md files are located at.
|
|
func View(path string) error {
|
|
conf, err := config(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// TODO: Enable bubbletea WithAltScreen and WithMouseCellMotion options when glow supports them
|
|
p := ui.NewProgram(conf)
|
|
|
|
_, err = p.Run()
|
|
return err
|
|
}
|
|
|
|
func config(path string) (ui.Config, error) {
|
|
var width uint
|
|
|
|
fd := safeconverter.ToInt(os.Stdout.Fd())
|
|
w, _, err := term.GetSize(fd)
|
|
if err != nil {
|
|
return ui.Config{}, err
|
|
}
|
|
|
|
width = min(uint(w), 120) //nolint:gosec,nolintlint // conversion is fine
|
|
|
|
docTypes := ui.NewDocTypeSet()
|
|
docTypes.Add(ui.LocalDoc)
|
|
|
|
conf := ui.Config{
|
|
WorkingDirectory: path,
|
|
DocumentTypes: docTypes,
|
|
GlamourStyle: "auto",
|
|
HighPerformancePager: true,
|
|
GlamourEnabled: true,
|
|
GlamourMaxWidth: width,
|
|
}
|
|
|
|
return conf, nil
|
|
}
|