mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 11:04:18 +02:00
add pager tui for 'api show' (#46)
This commit is contained in:
@@ -35,7 +35,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
Version string = `v0.0.19`
|
||||
Version string = `v0.0.20`
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -307,6 +307,7 @@ func ApiPathNames() []string {
|
||||
|
||||
func ApiShow(conf *cfg.Config, showpath, verb string) error {
|
||||
assets.LoadAssetOpenApi()
|
||||
out := printer.Builder{}
|
||||
|
||||
op, err := matchOperation(showpath, verb)
|
||||
if err != nil {
|
||||
@@ -335,43 +336,46 @@ func ApiShow(conf *cfg.Config, showpath, verb string) error {
|
||||
MarginBottom(1).
|
||||
MarginLeft(2)
|
||||
|
||||
fmt.Println(bold.Render("ID: " + op.Op.ID))
|
||||
fmt.Println(paragraph.Render(fmt.Sprintf("%s %s", op.Verb, showpath)))
|
||||
out.WriteStringLine(bold.Render("ID: " + op.Op.ID))
|
||||
out.WriteStringLine(paragraph.Render(fmt.Sprintf("%s %s", op.Verb, showpath)))
|
||||
|
||||
fmt.Println(bold.Render("Tags"))
|
||||
fmt.Println(paragraph.Render(strings.Join(op.Op.Tags, ", ")))
|
||||
out.WriteStringLine(bold.Render("Tags"))
|
||||
out.WriteStringLine(paragraph.Render(strings.Join(op.Op.Tags, ", ")))
|
||||
|
||||
fmt.Println(bold.Render("Summary"))
|
||||
fmt.Println(paragraph.Render(strings.TrimSpace(op.Op.Summary)))
|
||||
out.WriteStringLine(bold.Render("Summary"))
|
||||
out.WriteStringLine(paragraph.Render(strings.TrimSpace(op.Op.Summary)))
|
||||
|
||||
fmt.Println(bold.Render("Description"))
|
||||
fmt.Println(string(description)) // already indented
|
||||
out.WriteStringLine(bold.Render("Description"))
|
||||
out.WriteStringLine(string(description)) // already indented
|
||||
|
||||
if len(params.Path) > 0 {
|
||||
fmt.Println(bold.Render("Path Parameters"))
|
||||
out.WriteStringLine(bold.Render("Path Parameters"))
|
||||
for _, param := range params.Path {
|
||||
fmt.Println(boldparagraph.Render(param.Param))
|
||||
out.WriteStringLine(boldparagraph.Render(param.Param))
|
||||
if param.Description != "" {
|
||||
fmt.Println(indentparagraph.Render(param.Description))
|
||||
//out.WriteStringLine(indentparagraph.Render(param.Description))
|
||||
out.WriteStringLine(param.Description)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(params.Query) > 0 {
|
||||
fmt.Println(bold.Render("Query Parameters"))
|
||||
out.WriteStringLine(bold.Render("Query Parameters"))
|
||||
for _, param := range params.Query {
|
||||
fmt.Println(boldparagraph.Render(param.Param))
|
||||
out.WriteStringLine(boldparagraph.Render(param.Param))
|
||||
if param.Description != "" {
|
||||
fmt.Println(indentparagraph.Render(param.Description))
|
||||
out.WriteStringLine(indentparagraph.Render(param.Description))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if sample != "" {
|
||||
fmt.Println(bold.Render("Example"))
|
||||
fmt.Println(paragraph.Render(sample))
|
||||
out.WriteStringLine(bold.Render("Example"))
|
||||
out.WriteStringLine(paragraph.Render(sample))
|
||||
}
|
||||
|
||||
printer.Pager(showpath, out.String())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
28
pkg/printer/builder.go
Normal file
28
pkg/printer/builder.go
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
Copyright © 2026 Thomas von Dein
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package printer
|
||||
|
||||
import "strings"
|
||||
|
||||
// a wrapper around strings.Builder for convenience
|
||||
type Builder struct {
|
||||
strings.Builder
|
||||
}
|
||||
|
||||
func (builder *Builder) WriteStringLine(in string) {
|
||||
builder.WriteString(in + "\n")
|
||||
}
|
||||
136
pkg/printer/pager.go
Normal file
136
pkg/printer/pager.go
Normal file
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
Copyright © 2026 Thomas von Dein
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package printer
|
||||
|
||||
// pager setup using bubbletea
|
||||
// file shamlelessly copied from:
|
||||
// https://github.com/charmbracelet/bubbletea/tree/main/examples/pager
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/bubbles/viewport"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
var (
|
||||
titleStyle = func() lipgloss.Style {
|
||||
b := lipgloss.RoundedBorder()
|
||||
b.Right = "├"
|
||||
return lipgloss.NewStyle().BorderStyle(b).Padding(0, 1)
|
||||
}()
|
||||
|
||||
infoStyle = func() lipgloss.Style {
|
||||
b := lipgloss.RoundedBorder()
|
||||
b.Left = "┤"
|
||||
return titleStyle.BorderStyle(b)
|
||||
}()
|
||||
)
|
||||
|
||||
type model struct {
|
||||
content string
|
||||
title string
|
||||
ready bool
|
||||
viewport viewport.Model
|
||||
}
|
||||
|
||||
func (m model) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
var (
|
||||
cmd tea.Cmd
|
||||
cmds []tea.Cmd
|
||||
)
|
||||
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
if k := msg.String(); k == "ctrl+c" || k == "q" || k == "esc" {
|
||||
return m, tea.Quit
|
||||
}
|
||||
|
||||
case tea.WindowSizeMsg:
|
||||
headerHeight := lipgloss.Height(m.headerView())
|
||||
footerHeight := lipgloss.Height(m.footerView())
|
||||
verticalMarginHeight := headerHeight + footerHeight
|
||||
|
||||
if !m.ready {
|
||||
// Since this program is using the full size of the viewport we
|
||||
// need to wait until we've received the window dimensions before
|
||||
// we can initialize the viewport. The initial dimensions come in
|
||||
// quickly, though asynchronously, which is why we wait for them
|
||||
// here.
|
||||
m.viewport = viewport.New(msg.Width, msg.Height-verticalMarginHeight)
|
||||
m.viewport.YPosition = headerHeight
|
||||
m.viewport.SetContent(m.content)
|
||||
m.ready = true
|
||||
} else {
|
||||
m.viewport.Width = msg.Width
|
||||
m.viewport.Height = msg.Height - verticalMarginHeight
|
||||
}
|
||||
}
|
||||
|
||||
// Handle keyboard and mouse events in the viewport
|
||||
m.viewport, cmd = m.viewport.Update(msg)
|
||||
cmds = append(cmds, cmd)
|
||||
|
||||
return m, tea.Batch(cmds...)
|
||||
}
|
||||
|
||||
func (m model) View() string {
|
||||
if !m.ready {
|
||||
return "\n Initializing..."
|
||||
}
|
||||
return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.viewport.View(), m.footerView())
|
||||
}
|
||||
|
||||
func (m model) headerView() string {
|
||||
// title := titleStyle.Render("RPN Help Overview")
|
||||
title := titleStyle.Render(m.title)
|
||||
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(title)))
|
||||
return lipgloss.JoinHorizontal(lipgloss.Center, title, line)
|
||||
}
|
||||
|
||||
func (m model) footerView() string {
|
||||
info := infoStyle.Render(fmt.Sprintf("%3.f%%", m.viewport.ScrollPercent()*100))
|
||||
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(info)))
|
||||
return lipgloss.JoinHorizontal(lipgloss.Center, line, info)
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func Pager(title, message string) {
|
||||
p := tea.NewProgram(
|
||||
model{content: message, title: title},
|
||||
tea.WithAltScreen(), // use the full size of the terminal in its "alternate screen buffer"
|
||||
tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel
|
||||
)
|
||||
|
||||
if _, err := p.Run(); err != nil {
|
||||
fmt.Println("could not run pager:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user