mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 08:14:18 +02:00
use real terminal width to determine markdown width in 'api show'
This commit is contained in:
3
go.mod
3
go.mod
@@ -90,6 +90,7 @@ require (
|
|||||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
|
||||||
golang.org/x/image v0.0.0-20191206065243-da761ea9ff43 // indirect
|
golang.org/x/image v0.0.0-20191206065243-da761ea9ff43 // indirect
|
||||||
golang.org/x/net v0.55.0 // indirect
|
golang.org/x/net v0.55.0 // indirect
|
||||||
golang.org/x/sys v0.45.0 // indirect
|
golang.org/x/sys v0.46.0 // indirect
|
||||||
|
golang.org/x/term v0.44.0 // indirect
|
||||||
golang.org/x/text v0.37.0 // indirect
|
golang.org/x/text v0.37.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -200,6 +200,10 @@ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||||||
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY=
|
golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY=
|
||||||
golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||||
|
golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw=
|
||||||
|
golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||||
|
golang.org/x/term v0.44.0 h1:0rLvDRCtNj0gZkyIXhCyOb2OAzEhLVqc4B+hrsBhrmc=
|
||||||
|
golang.org/x/term v0.44.0/go.mod h1:7ze4MdzUzLXpSAoFP1H0bOI9aXDqveSvatT5vKcFh2Y=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
|
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
|
||||||
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
|
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
|
||||||
|
|||||||
@@ -41,9 +41,12 @@ import (
|
|||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/chzyer/readline"
|
"github.com/chzyer/readline"
|
||||||
"github.com/go-openapi/spec"
|
"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:
|
Example:
|
||||||
|
|
||||||
@@ -59,6 +62,7 @@ put /yourindex/_settings
|
|||||||
|
|
||||||
If you do NOT supply a JSON in the first line, you need to hit ENTER
|
If you do NOT supply a JSON in the first line, you need to hit ENTER
|
||||||
twice to complete.`
|
twice to complete.`
|
||||||
|
)
|
||||||
|
|
||||||
// holds an API operation via go-openapi/spec
|
// holds an API operation via go-openapi/spec
|
||||||
type Op struct {
|
type Op struct {
|
||||||
@@ -313,18 +317,19 @@ func ApiShow(conf *cfg.Config, showpath, verb string) error {
|
|||||||
|
|
||||||
cleanMarkup := regexp.MustCompile(`<[^<>]+>`)
|
cleanMarkup := regexp.MustCompile(`<[^<>]+>`)
|
||||||
|
|
||||||
params := getApiParameters(op, showpath)
|
width := getTermWidth()
|
||||||
|
params := getApiParameters(op, showpath, width)
|
||||||
sample := getApiExample(op)
|
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().
|
var bold = lipgloss.NewStyle().
|
||||||
Bold(true)
|
Bold(true)
|
||||||
var paragraph = lipgloss.NewStyle().
|
var paragraph = lipgloss.NewStyle().
|
||||||
MarginBottom(1).
|
MarginBottom(1).
|
||||||
MarginLeft(4)
|
MarginLeft(DefaultMargin)
|
||||||
var boldparagraph = lipgloss.NewStyle().
|
var boldparagraph = lipgloss.NewStyle().
|
||||||
MarginBottom(1).
|
MarginBottom(1).
|
||||||
MarginLeft(4).
|
MarginLeft(DefaultMargin).
|
||||||
Bold(true)
|
Bold(true)
|
||||||
var indentparagraph = lipgloss.NewStyle().
|
var indentparagraph = lipgloss.NewStyle().
|
||||||
MarginBottom(1).
|
MarginBottom(1).
|
||||||
@@ -372,7 +377,7 @@ func findTags(op *Op) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get API parameters
|
// Get API parameters
|
||||||
func getApiParameters(op *Op, path string) Params {
|
func getApiParameters(op *Op, path string, width int) Params {
|
||||||
params := Params{}
|
params := Params{}
|
||||||
pathParams := []string{}
|
pathParams := []string{}
|
||||||
pathParamsReg := regexp.MustCompile(`{([a-z_]+)}`)
|
pathParamsReg := regexp.MustCompile(`{([a-z_]+)}`)
|
||||||
@@ -400,7 +405,7 @@ func getApiParameters(op *Op, path string) Params {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
par := Param{
|
par := Param{
|
||||||
Description: string(markdown.Render(param.Description, 80, 6)),
|
Description: string(markdown.Render(param.Description, width, DefaultMargin)),
|
||||||
Param: param.Name,
|
Param: param.Name,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -501,3 +506,17 @@ func findOperation(item spec.PathItemProps) []*Op {
|
|||||||
|
|
||||||
return ops
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user