tui elaboration (#56)

This commit is contained in:
Michael Quigley 2023-01-10 14:05:18 -05:00
parent 73d124da83
commit d984cbf832
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 40 additions and 5 deletions

View File

@ -25,10 +25,12 @@ type shareRequestModel struct {
path string
}
func newShareModel(shareToken string, frontendEndpoints []string) *shareModel {
func newShareModel(shareToken string, frontendEndpoints []string, shareMode, backendMode string) *shareModel {
return &shareModel{
shareToken: shareToken,
frontendEndpoints: frontendEndpoints,
shareMode: shareMode,
backendMode: backendMode,
}
}
@ -38,14 +40,18 @@ func (m *shareModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
shareHeaderStyle.Width((m.width - 4) / 2)
shareHeaderStyle.Width(m.width - 30)
configHeaderStyle.Width(26)
m.height = msg.Height
requestsStyle.Width(m.width - 2)
requestsStyle.Height(20)
requestsStyle.Height(m.height - (len(m.frontendEndpoints) + 6))
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "ctrl+l":
return m, tea.ClearScreen
}
}
@ -55,13 +61,30 @@ func (m *shareModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m *shareModel) View() string {
topRow := lipgloss.JoinHorizontal(lipgloss.Top,
shareHeaderStyle.Render(strings.Join(m.frontendEndpoints, "\n")),
shareHeaderStyle.Render(m.shareToken),
configHeaderStyle.Render(m.renderConfig()),
)
requests := requestsStyle.Render("hello")
all := lipgloss.JoinVertical(lipgloss.Left, topRow, requests)
return all
}
func (m *shareModel) renderConfig() string {
out := "["
if m.shareMode == "public" {
out += shareModePublicStyle.Render(strings.ToUpper(m.shareMode))
} else {
out += shareModePrivateStyle.Render(strings.ToUpper(m.shareMode))
}
out += "] ["
if m.backendMode == "proxy" {
out += backendModeProxyStyle.Render(strings.ToUpper(m.backendMode))
} else {
out += backendModeWebStyle.Render(strings.ToUpper(m.backendMode))
}
out += "]"
return out
}
var shareHeaderStyle = lipgloss.NewStyle().
Height(3).
PaddingTop(1).PaddingLeft(2).PaddingBottom(1).PaddingRight(2).
@ -69,8 +92,20 @@ var shareHeaderStyle = lipgloss.NewStyle().
BorderForeground(lipgloss.Color("63")).
Align(lipgloss.Center)
var configHeaderStyle = lipgloss.NewStyle().
Height(3).
PaddingTop(1).PaddingLeft(2).PaddingBottom(1).PaddingRight(2).
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("63")).
Align(lipgloss.Center)
var requestsStyle = lipgloss.NewStyle().
Height(3).
PaddingTop(1).PaddingLeft(2).PaddingBottom(1).PaddingRight(2).
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("63"))
var shareModePublicStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#0F0"))
var shareModePrivateStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#F00"))
var backendModeProxyStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFA500"))
var backendModeWebStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#0CC"))

View File

@ -181,7 +181,7 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
bh.Requests()()
}()
prg := tea.NewProgram(newShareModel(resp.Payload.ShrToken, resp.Payload.FrontendProxyEndpoints), tea.WithAltScreen())
prg := tea.NewProgram(newShareModel(resp.Payload.ShrToken, resp.Payload.FrontendProxyEndpoints, "public", cmd.backendMode), tea.WithAltScreen())
if _, err := prg.Run(); err != nil {
tui.Error("An error occurred", err)
}