mirror of
https://github.com/openziti/zrok.git
synced 2025-06-24 19:51:32 +02:00
minor access private tui tweaks; share tui aligned with new pattern (#56)
This commit is contained in:
parent
48314c013d
commit
3db9f992e4
@ -10,7 +10,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const accessBacklog = 64
|
const accessTuiBacklog = 256
|
||||||
|
|
||||||
type accessModel struct {
|
type accessModel struct {
|
||||||
shrToken string
|
shrToken string
|
||||||
@ -38,18 +38,16 @@ func (m *accessModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case *endpoints.Request:
|
case *endpoints.Request:
|
||||||
m.requests = append(m.requests, msg)
|
m.requests = append(m.requests, msg)
|
||||||
if len(m.requests) > accessBacklog {
|
if len(m.requests) > accessTuiBacklog {
|
||||||
m.requests = m.requests[1:]
|
m.requests = m.requests[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
case accessLogLine:
|
case accessLogLine:
|
||||||
m.showLog = true
|
m.showLog = true
|
||||||
splitHeight := m.height - 5
|
m.adjustPaneHeights()
|
||||||
accessRequestsStyle.Height(splitHeight/2 - 1)
|
|
||||||
accessLogStyle.Height(splitHeight/2 - 1)
|
|
||||||
|
|
||||||
m.log = append(m.log, string(msg))
|
m.log = append(m.log, string(msg))
|
||||||
if len(m.log) > accessBacklog {
|
if len(m.log) > accessTuiBacklog {
|
||||||
m.log = m.log[1:]
|
m.log = m.log[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,13 +58,7 @@ func (m *accessModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
accessLogStyle.Width(m.width - 2)
|
accessLogStyle.Width(m.width - 2)
|
||||||
|
|
||||||
m.height = msg.Height
|
m.height = msg.Height
|
||||||
if !m.showLog {
|
m.adjustPaneHeights()
|
||||||
accessRequestsStyle.Height(m.height - 5)
|
|
||||||
} else {
|
|
||||||
splitHeight := m.height - 5
|
|
||||||
accessRequestsStyle.Height(splitHeight/2 - 1)
|
|
||||||
accessLogStyle.Height(splitHeight/2 - 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
@ -76,13 +68,7 @@ func (m *accessModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
return m, tea.ClearScreen
|
return m, tea.ClearScreen
|
||||||
case "l":
|
case "l":
|
||||||
m.showLog = !m.showLog
|
m.showLog = !m.showLog
|
||||||
if !m.showLog {
|
m.adjustPaneHeights()
|
||||||
accessRequestsStyle.Height(m.height - 5)
|
|
||||||
} else {
|
|
||||||
splitHeight := m.height - 5
|
|
||||||
accessRequestsStyle.Height(splitHeight/2 - 1)
|
|
||||||
accessLogStyle.Height(splitHeight/2 - 1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,6 +93,16 @@ func (m *accessModel) View() string {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *accessModel) adjustPaneHeights() {
|
||||||
|
if !m.showLog {
|
||||||
|
accessRequestsStyle.Height(m.height - 5)
|
||||||
|
} else {
|
||||||
|
splitHeight := m.height - 5
|
||||||
|
accessRequestsStyle.Height(splitHeight/2 - 1)
|
||||||
|
accessLogStyle.Height(splitHeight/2 - 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (m *accessModel) renderRequests() string {
|
func (m *accessModel) renderRequests() string {
|
||||||
var requestLines []string
|
var requestLines []string
|
||||||
for _, req := range m.requests {
|
for _, req := range m.requests {
|
||||||
|
@ -186,7 +186,9 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
|
|||||||
} else {
|
} else {
|
||||||
shareDescription := fmt.Sprintf("access your share with: %v", tui.CodeStyle.Render(fmt.Sprintf("zrok access private %v", resp.Payload.ShrToken)))
|
shareDescription := fmt.Sprintf("access your share with: %v", tui.CodeStyle.Render(fmt.Sprintf("zrok access private %v", resp.Payload.ShrToken)))
|
||||||
mdl := newShareModel(resp.Payload.ShrToken, []string{shareDescription}, "private", cmd.backendMode)
|
mdl := newShareModel(resp.Payload.ShrToken, []string{shareDescription}, "private", cmd.backendMode)
|
||||||
|
logrus.SetOutput(mdl)
|
||||||
prg := tea.NewProgram(mdl, tea.WithAltScreen())
|
prg := tea.NewProgram(mdl, tea.WithAltScreen())
|
||||||
|
mdl.prg = prg
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
|
@ -188,7 +188,9 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
mdl := newShareModel(resp.Payload.ShrToken, resp.Payload.FrontendProxyEndpoints, "public", cmd.backendMode)
|
mdl := newShareModel(resp.Payload.ShrToken, resp.Payload.FrontendProxyEndpoints, "public", cmd.backendMode)
|
||||||
|
logrus.SetOutput(mdl)
|
||||||
prg := tea.NewProgram(mdl, tea.WithAltScreen())
|
prg := tea.NewProgram(mdl, tea.WithAltScreen())
|
||||||
|
mdl.prg = prg
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
|
@ -163,7 +163,9 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mdl := newShareModel(shrToken, []string{shareDescription}, resp.Payload.ShareMode, resp.Payload.BackendMode)
|
mdl := newShareModel(shrToken, []string{shareDescription}, resp.Payload.ShareMode, resp.Payload.BackendMode)
|
||||||
|
logrus.SetOutput(mdl)
|
||||||
prg := tea.NewProgram(mdl, tea.WithAltScreen())
|
prg := tea.NewProgram(mdl, tea.WithAltScreen())
|
||||||
|
mdl.prg = prg
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
|
@ -4,22 +4,30 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
|
"github.com/muesli/reflow/wordwrap"
|
||||||
"github.com/openziti-test-kitchen/zrok/endpoints"
|
"github.com/openziti-test-kitchen/zrok/endpoints"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const shareTuiBacklog = 256
|
||||||
|
|
||||||
type shareModel struct {
|
type shareModel struct {
|
||||||
shrToken string
|
shrToken string
|
||||||
frontendDescriptions []string
|
frontendDescriptions []string
|
||||||
shareMode string
|
shareMode string
|
||||||
backendMode string
|
backendMode string
|
||||||
requests []*endpoints.Request
|
requests []*endpoints.Request
|
||||||
logMessages []string
|
log []string
|
||||||
|
showLog bool
|
||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
|
headerHeight int
|
||||||
|
prg *tea.Program
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type shareLogLine string
|
||||||
|
|
||||||
func newShareModel(shrToken string, frontendEndpoints []string, shareMode, backendMode string) *shareModel {
|
func newShareModel(shrToken string, frontendEndpoints []string, shareMode, backendMode string) *shareModel {
|
||||||
return &shareModel{
|
return &shareModel{
|
||||||
shrToken: shrToken,
|
shrToken: shrToken,
|
||||||
@ -34,9 +42,18 @@ func (m *shareModel) Init() tea.Cmd { return nil }
|
|||||||
func (m *shareModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
func (m *shareModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case *endpoints.Request:
|
case *endpoints.Request:
|
||||||
m.requests = append([]*endpoints.Request{msg}, m.requests...)
|
m.requests = append(m.requests, msg)
|
||||||
if len(m.requests) > 2048 {
|
if len(m.requests) > shareTuiBacklog {
|
||||||
m.requests = m.requests[:2048]
|
m.requests = m.requests[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
case shareLogLine:
|
||||||
|
m.showLog = true
|
||||||
|
m.adjustPaneHeights()
|
||||||
|
|
||||||
|
m.log = append(m.log, string(msg))
|
||||||
|
if len(m.log) > shareTuiBacklog {
|
||||||
|
m.log = m.log[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
case tea.WindowSizeMsg:
|
case tea.WindowSizeMsg:
|
||||||
@ -44,9 +61,11 @@ func (m *shareModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
shareHeaderStyle.Width(m.width - 30)
|
shareHeaderStyle.Width(m.width - 30)
|
||||||
shareConfigStyle.Width(26)
|
shareConfigStyle.Width(26)
|
||||||
shareRequestsStyle.Width(m.width - 2)
|
shareRequestsStyle.Width(m.width - 2)
|
||||||
|
shareLogStyle.Width(m.width - 2)
|
||||||
|
|
||||||
m.height = msg.Height
|
m.height = msg.Height
|
||||||
shareRequestsStyle.Height(m.height - (len(m.frontendDescriptions) + 6))
|
m.headerHeight = len(m.frontendDescriptions) + 4
|
||||||
|
m.adjustPaneHeights()
|
||||||
|
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
@ -54,6 +73,9 @@ func (m *shareModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
case "ctrl+l":
|
case "ctrl+l":
|
||||||
return m, tea.ClearScreen
|
return m, tea.ClearScreen
|
||||||
|
case "l":
|
||||||
|
m.showLog = !m.showLog
|
||||||
|
m.adjustPaneHeights()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,9 +87,26 @@ func (m *shareModel) View() string {
|
|||||||
shareHeaderStyle.Render(strings.Join(m.frontendDescriptions, "\n")),
|
shareHeaderStyle.Render(strings.Join(m.frontendDescriptions, "\n")),
|
||||||
shareConfigStyle.Render(m.renderConfig()),
|
shareConfigStyle.Render(m.renderConfig()),
|
||||||
)
|
)
|
||||||
requests := shareRequestsStyle.Render(m.renderBackendRequests())
|
var panes string
|
||||||
all := lipgloss.JoinVertical(lipgloss.Left, topRow, requests)
|
if m.showLog {
|
||||||
return all
|
panes = lipgloss.JoinVertical(lipgloss.Left,
|
||||||
|
shareRequestsStyle.Render(m.renderRequests()),
|
||||||
|
shareLogStyle.Render(m.renderLog()),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
panes = shareRequestsStyle.Render(m.renderRequests())
|
||||||
|
}
|
||||||
|
return lipgloss.JoinVertical(lipgloss.Left, topRow, panes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *shareModel) adjustPaneHeights() {
|
||||||
|
if !m.showLog {
|
||||||
|
shareRequestsStyle.Height(m.height - m.headerHeight)
|
||||||
|
} else {
|
||||||
|
splitHeight := m.height - m.headerHeight
|
||||||
|
shareRequestsStyle.Height(splitHeight/2 - 1)
|
||||||
|
shareLogStyle.Height(splitHeight/2 - 1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *shareModel) renderConfig() string {
|
func (m *shareModel) renderConfig() string {
|
||||||
@ -87,21 +126,37 @@ func (m *shareModel) renderConfig() string {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *shareModel) renderBackendRequests() string {
|
func (m *shareModel) renderRequests() string {
|
||||||
out := ""
|
var requestLines []string
|
||||||
maxRows := shareRequestsStyle.GetHeight()
|
for _, req := range m.requests {
|
||||||
for i := 0; i < maxRows && i < len(m.requests); i++ {
|
reqLine := fmt.Sprintf("%v %v -> %v %v",
|
||||||
req := m.requests[i]
|
|
||||||
out += fmt.Sprintf("%v %v -> %v %v",
|
|
||||||
timeStyle.Render(req.Stamp.Format(time.RFC850)),
|
timeStyle.Render(req.Stamp.Format(time.RFC850)),
|
||||||
addressStyle.Render(req.RemoteAddr),
|
addressStyle.Render(req.RemoteAddr),
|
||||||
m.renderMethod(req.Method),
|
m.renderMethod(req.Method),
|
||||||
req.Path,
|
req.Path,
|
||||||
)
|
)
|
||||||
if i != maxRows-1 {
|
reqLineWrapped := wordwrap.String(reqLine, m.width-2)
|
||||||
out += "\n"
|
splitWrapped := strings.Split(reqLineWrapped, "\n")
|
||||||
|
for _, splitLine := range splitWrapped {
|
||||||
|
splitLine := strings.ReplaceAll(splitLine, "\n", "")
|
||||||
|
if splitLine != "" {
|
||||||
|
requestLines = append(requestLines, splitLine)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
maxRows := shareRequestsStyle.GetHeight()
|
||||||
|
startRow := 0
|
||||||
|
if len(requestLines) > maxRows {
|
||||||
|
startRow = len(requestLines) - maxRows
|
||||||
|
}
|
||||||
|
out := ""
|
||||||
|
for i := startRow; i < len(requestLines); i++ {
|
||||||
|
outLine := requestLines[i]
|
||||||
|
if i < len(requestLines)-1 {
|
||||||
|
outLine += "\n"
|
||||||
|
}
|
||||||
|
out += outLine
|
||||||
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,23 +171,61 @@ func (m *shareModel) renderMethod(method string) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *shareModel) renderLog() string {
|
||||||
|
var splitLines []string
|
||||||
|
for _, line := range m.log {
|
||||||
|
wrapped := wordwrap.String(line, m.width-2)
|
||||||
|
wrappedLines := strings.Split(wrapped, "\n")
|
||||||
|
for _, wrappedLine := range wrappedLines {
|
||||||
|
splitLine := strings.ReplaceAll(wrappedLine, "\n", "")
|
||||||
|
if splitLine != "" {
|
||||||
|
splitLines = append(splitLines, splitLine)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
maxRows := shareLogStyle.GetHeight()
|
||||||
|
startRow := 0
|
||||||
|
if len(splitLines) > maxRows {
|
||||||
|
startRow = len(splitLines) - maxRows
|
||||||
|
}
|
||||||
|
out := ""
|
||||||
|
for i := startRow; i < len(splitLines); i++ {
|
||||||
|
outLine := splitLines[i]
|
||||||
|
if i < len(splitLines)-1 {
|
||||||
|
outLine += "\n"
|
||||||
|
}
|
||||||
|
out += outLine
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *shareModel) Write(p []byte) (n int, err error) {
|
||||||
|
in := string(p)
|
||||||
|
lines := strings.Split(in, "\n")
|
||||||
|
for _, line := range lines {
|
||||||
|
cleanLine := strings.ReplaceAll(line, "\n", "")
|
||||||
|
if cleanLine != "" {
|
||||||
|
m.prg.Send(shareLogLine(cleanLine))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
var shareHeaderStyle = lipgloss.NewStyle().
|
var shareHeaderStyle = lipgloss.NewStyle().
|
||||||
Height(3).
|
|
||||||
PaddingTop(1).PaddingLeft(2).PaddingBottom(1).PaddingRight(2).
|
|
||||||
BorderStyle(lipgloss.RoundedBorder()).
|
BorderStyle(lipgloss.RoundedBorder()).
|
||||||
BorderForeground(lipgloss.Color("63")).
|
BorderForeground(lipgloss.Color("63")).
|
||||||
Align(lipgloss.Center)
|
Align(lipgloss.Center)
|
||||||
|
|
||||||
var shareConfigStyle = lipgloss.NewStyle().
|
var shareConfigStyle = lipgloss.NewStyle().
|
||||||
Height(3).
|
|
||||||
PaddingTop(1).PaddingLeft(2).PaddingBottom(1).PaddingRight(2).
|
|
||||||
BorderStyle(lipgloss.RoundedBorder()).
|
BorderStyle(lipgloss.RoundedBorder()).
|
||||||
BorderForeground(lipgloss.Color("63")).
|
BorderForeground(lipgloss.Color("63")).
|
||||||
Align(lipgloss.Center)
|
Align(lipgloss.Center)
|
||||||
|
|
||||||
var shareRequestsStyle = lipgloss.NewStyle().
|
var shareRequestsStyle = lipgloss.NewStyle().
|
||||||
Height(3).
|
BorderStyle(lipgloss.RoundedBorder()).
|
||||||
PaddingLeft(2).PaddingRight(2).
|
BorderForeground(lipgloss.Color("63"))
|
||||||
|
|
||||||
|
var shareLogStyle = lipgloss.NewStyle().
|
||||||
BorderStyle(lipgloss.RoundedBorder()).
|
BorderStyle(lipgloss.RoundedBorder()).
|
||||||
BorderForeground(lipgloss.Color("63"))
|
BorderForeground(lipgloss.Color("63"))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user