use real terminal width to determine markdown width in 'api show'

This commit is contained in:
2026-06-17 13:33:03 +02:00
parent f1ff361c12
commit 49bc2c7acb
3 changed files with 32 additions and 8 deletions

View File

@@ -41,9 +41,12 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/chzyer/readline"
"github.com/go-openapi/spec"
"golang.org/x/term"
)
const intro = `Input format: verb path [data]"
const (
DefaultMargin = 4
intro = `Input format: verb path [data]"
Example:
@@ -59,6 +62,7 @@ put /yourindex/_settings
If you do NOT supply a JSON in the first line, you need to hit ENTER
twice to complete.`
)
// holds an API operation via go-openapi/spec
type Op struct {
@@ -313,18 +317,19 @@ func ApiShow(conf *cfg.Config, showpath, verb string) error {
cleanMarkup := regexp.MustCompile(`<[^<>]+>`)
params := getApiParameters(op, showpath)
width := getTermWidth()
params := getApiParameters(op, showpath, width)
sample := getApiExample(op)
description := markdown.Render(cleanMarkup.ReplaceAllString(op.Op.Description, ""), 80, 6)
description := markdown.Render(cleanMarkup.ReplaceAllString(op.Op.Description, ""), width, DefaultMargin)
var bold = lipgloss.NewStyle().
Bold(true)
var paragraph = lipgloss.NewStyle().
MarginBottom(1).
MarginLeft(4)
MarginLeft(DefaultMargin)
var boldparagraph = lipgloss.NewStyle().
MarginBottom(1).
MarginLeft(4).
MarginLeft(DefaultMargin).
Bold(true)
var indentparagraph = lipgloss.NewStyle().
MarginBottom(1).
@@ -372,7 +377,7 @@ func findTags(op *Op) []string {
}
// Get API parameters
func getApiParameters(op *Op, path string) Params {
func getApiParameters(op *Op, path string, width int) Params {
params := Params{}
pathParams := []string{}
pathParamsReg := regexp.MustCompile(`{([a-z_]+)}`)
@@ -400,7 +405,7 @@ func getApiParameters(op *Op, path string) Params {
}
} else {
par := Param{
Description: string(markdown.Render(param.Description, 80, 6)),
Description: string(markdown.Render(param.Description, width, DefaultMargin)),
Param: param.Name,
}
@@ -501,3 +506,17 @@ func findOperation(item spec.PathItemProps) []*Op {
return ops
}
func getTermWidth() int {
if term.IsTerminal(int(os.Stdout.Fd())) {
println("in a term")
} else {
println("not in a term")
}
width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
return 80
}
return width - DefaultMargin
}