162 lines
5.4 KiB
Go
162 lines
5.4 KiB
Go
package tui
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"proxyctl/internal/app"
|
|
"proxyctl/internal/profile"
|
|
)
|
|
|
|
func (p terminalPresentation) mainView(model presentationState, width, height int) terminalPage {
|
|
activeProfile := model.snapshot.ActiveProfile
|
|
if activeProfile == "" {
|
|
activeProfile = "No Active Profile"
|
|
}
|
|
header := fmt.Sprintf("%s Operations Console\nActive Profile: %s Targets: %s",
|
|
p.theme.title.Render("proxyctl"), activeProfile, p.stateCountSummary(model.snapshot))
|
|
if width < wideWidth {
|
|
header = fmt.Sprintf("%s Operations Console\nActive Profile: %s\nTargets: %s",
|
|
p.theme.title.Render("proxyctl"), activeProfile, p.stateCountSummary(model.snapshot))
|
|
}
|
|
|
|
catalog := newTargetCatalog(model.snapshot, model.showUnavailable, model.selectedTargetID)
|
|
footer := p.dashboardFooter(model, catalog)
|
|
if model.targetConfirm && !model.applying {
|
|
footer = "y: confirm n/Esc: cancel"
|
|
}
|
|
return terminalPage{
|
|
header: header,
|
|
footer: footer,
|
|
dashboard: &dashboardRenderState{
|
|
catalog: catalog,
|
|
selectedTargetID: model.selectedTargetID,
|
|
focus: model.dashboardFocus,
|
|
detailsOffset: model.detailsOffset,
|
|
applying: model.applying,
|
|
profileTransition: model.profileTransition,
|
|
progressTarget: model.progressTarget,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (p terminalPresentation) dashboardFooter(model presentationState, catalog targetCatalog) string {
|
|
visibility := "u: show unavailable"
|
|
if model.showUnavailable {
|
|
visibility = "u: hide unavailable"
|
|
}
|
|
if model.dashboardFocus == targetDetailsPane {
|
|
return "↑/k ↓/j PgUp/PgDn: scroll Tab: focus Targets " + visibility + "\np: manage Proxy Profiles ?: help q/Esc: quit"
|
|
}
|
|
actions := "↑/k ↓/j: select Tab: focus details " + visibility
|
|
if selected, ok := catalog.selected(model.selectedTargetID); ok && selected.Available && selected.DetectionPhase == app.TargetReady {
|
|
actions = "Space: toggle " + actions
|
|
}
|
|
return actions + "\np: manage Proxy Profiles r: refresh ?: help q/Esc: quit"
|
|
}
|
|
|
|
func (p terminalPresentation) helpView(model presentationState) terminalPage {
|
|
var body strings.Builder
|
|
for _, item := range model.snapshot.Targets {
|
|
fmt.Fprintf(&body, "%s\n%s\n%s\n", p.theme.section.Render(item.Description.Name), item.Description.Summary, item.Description.Details)
|
|
if item.Available {
|
|
fmt.Fprintf(&body, "Detected: %s\n\n", item.AvailabilityEvidence)
|
|
} else {
|
|
fmt.Fprintf(&body, "Not detected: %s\n\n", item.AvailabilityEvidence)
|
|
}
|
|
}
|
|
return terminalPage{
|
|
header: p.theme.title.Render("proxyctl Help — built-in Targets"),
|
|
primary: strings.TrimRight(body.String(), "\n"),
|
|
footer: "↑/k ↓/j: navigate Space: toggle p: manage Proxy Profiles\nr: refresh ?: help q/Escape: back",
|
|
focusLine: 0,
|
|
}
|
|
}
|
|
|
|
func (p terminalPresentation) profilesView(model presentationState) terminalPage {
|
|
var list strings.Builder
|
|
if len(model.snapshot.Profiles) == 0 {
|
|
list.WriteString("No Proxy Profiles. Press n to create one.")
|
|
}
|
|
for i, item := range model.snapshot.Profiles {
|
|
cursor, active := " ", " "
|
|
if i == model.selected {
|
|
cursor = "> "
|
|
}
|
|
if item.Name == model.snapshot.ActiveProfile {
|
|
active = "*"
|
|
}
|
|
if list.Len() > 0 {
|
|
list.WriteByte('\n')
|
|
}
|
|
fmt.Fprintf(&list, "%s%s %s", cursor, active, item.Name)
|
|
}
|
|
|
|
context := selectedProfileDetails(p, model)
|
|
footer := "n: new e: edit d: delete a/Enter: activate\n↑/k ↓/j: select q/Esc: back"
|
|
switch {
|
|
case model.profileConfirm:
|
|
footer = "y: confirm n/Esc: cancel"
|
|
case model.confirm:
|
|
footer = "y: confirm n/Esc: cancel"
|
|
if len(model.snapshot.Profiles) == 1 {
|
|
context = "Delete Profile — this is the final Proxy Profile. Managed configuration will remain in place and become Drifted."
|
|
} else {
|
|
context = "Delete Profile?"
|
|
}
|
|
}
|
|
return terminalPage{
|
|
header: p.theme.title.Render("Proxy Profiles"),
|
|
primary: list.String(),
|
|
context: context,
|
|
footer: footer,
|
|
focusLine: model.selected,
|
|
}
|
|
}
|
|
|
|
func selectedProfileDetails(p terminalPresentation, model presentationState) string {
|
|
selected, ok := profileAt(model.snapshot, model.selected)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
var details strings.Builder
|
|
details.WriteString(p.theme.section.Render("Selected Profile"))
|
|
fmt.Fprintf(&details, "\nMode: %s", selected.Mode)
|
|
if selected.Mode == profile.SingleMode {
|
|
fmt.Fprintf(&details, "\nProxy: %s", profile.ObscureURL(selected.SingleURL))
|
|
} else {
|
|
fmt.Fprintf(&details, "\nHTTP: %s\nHTTPS: %s\nALL/SOCKS: %s",
|
|
profile.ObscureURL(selected.HTTPURL), profile.ObscureURL(selected.HTTPSURL), profile.ObscureURL(selected.AllURL))
|
|
}
|
|
fmt.Fprintf(&details, "\nBypass: %s", strings.Join(selected.Bypass, ", "))
|
|
return details.String()
|
|
}
|
|
|
|
func (p terminalPresentation) formView(model presentationState) terminalPage {
|
|
action := "Create"
|
|
if model.form.originalName != "" {
|
|
action = "Edit"
|
|
}
|
|
var fields strings.Builder
|
|
for i, label := range model.form.labels() {
|
|
cursor := " "
|
|
if i == model.form.field {
|
|
cursor = "> "
|
|
}
|
|
value := model.form.valueAt(i)
|
|
if label != "Name" && label != "Bypass" {
|
|
value = profile.ObscureURL(value)
|
|
}
|
|
if fields.Len() > 0 {
|
|
fields.WriteByte('\n')
|
|
}
|
|
fmt.Fprintf(&fields, "%s%s: %s", cursor, label, value)
|
|
}
|
|
return terminalPage{
|
|
header: p.theme.title.Render(fmt.Sprintf("%s Proxy Profile — %s mode", action, model.form.profile.Mode)),
|
|
primary: fields.String(),
|
|
footer: "Tab/↑/↓: move Ctrl+T: toggle Single/Advanced\nCtrl+S: save Ctrl+D: detect protocol Esc: cancel",
|
|
focusLine: model.form.field,
|
|
}
|
|
}
|