Skip to content

Commit

Permalink
fix: display headers tab
Browse files Browse the repository at this point in the history
  • Loading branch information
jackMort committed Nov 11, 2024
1 parent f2a4aa0 commit d9d54ca
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 35 deletions.
45 changes: 43 additions & 2 deletions components/headers/headers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package headers

import (
"restman/app"
"restman/components/config"
"strings"

Expand All @@ -23,9 +24,10 @@ type Model struct {
width int
height int
simpleTable table.Model
call *app.Call
}

func New(headers []string, width int, height int) Model {
func GetRows(headers []string) []table.Row {
rows := make([]table.Row, 0, len(headers))
for _, header := range headers {
// split : to get key and value
Expand All @@ -36,14 +38,24 @@ func New(headers []string, width int, height int) Model {
})
rows = append(rows, row)
}
return rows
}

func New(call *app.Call, width int, height int) Model {

headers := []string{}
if call != nil {
headers = call.Headers
}

return Model{
call: call,
width: width,
height: height,
simpleTable: table.New([]table.Column{
table.NewColumn(columnKeyKey, " Key", 20),
table.NewColumn(columnKeyValue, " Value", width-23),
}).WithRows(rows).BorderRounded().
}).WithRows(GetRows(headers)).BorderRounded().
WithBaseStyle(styleBase).
Focused(true),
}
Expand All @@ -59,12 +71,41 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds []tea.Cmd
)

switch msg := msg.(type) {
case tea.KeyMsg:
{
switch msg.String() {
case "x":
key := strings.TrimSpace(m.simpleTable.HighlightedRow().Data[columnKeyKey].(string))
headers := []string{}
for _, header := range m.call.Headers {
if key != strings.Split(header, ":")[0] {
headers = append(headers, header)
}
}
m.call.Headers = headers

cmd := func() tea.Msg {
return app.CallUpdatedMsg{Call: m.call}
}
m.simpleTable = m.simpleTable.WithRows(GetRows(m.call.Headers))
cmds = append(cmds, cmd)
}
}
case app.CallUpdatedMsg:
m.call = msg.Call

}

m.simpleTable, cmd = m.simpleTable.Update(msg)
cmds = append(cmds, cmd)

return m, tea.Batch(cmds...)
}

func (m Model) View() string {
if m.call != nil && len(m.call.Headers) == 0 {
return config.EmptyMessageStyle.Padding(2, 2).Render("No headers defined")
}
return m.simpleTable.View()
}
51 changes: 18 additions & 33 deletions components/request/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,22 @@ func New() Request {

// satisfy the tea.Model interface
func (b Request) Init() tea.Cmd {
b.activeTab = 0
return nil
}

func (b Request) GetContent() tea.Model {
if b.activeTab == 2 {
m := make(map[string][]string)
if b.activeTab == 0 {
if b.call != nil {
u, err := url.Parse(b.call.Url)
if err == nil && b.call.Url != "" {
m, _ = url.ParseQuery(u.RawQuery)
}
}
return params.New(m, b.width, b.height)
} else if b.activeTab == 1 {
return headers.New(b.call, b.width-2, b.width)
} else if b.activeTab == 2 {
return auth.New(b.width, b.call)
} else if b.activeTab == 3 {
body := ""
Expand All @@ -87,8 +97,6 @@ func (b Request) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

case app.CallSelectedMsg:
b.call = msg.Call
// TODO:
// b.body = msg.Tab.Results
b.content = b.GetContent()

case tea.WindowSizeMsg:
Expand Down Expand Up @@ -145,6 +153,7 @@ func (b Request) View() string {
tabGap = tabGap.BorderForeground(config.COLOR_SUBTLE)
}

tabNames := ""
for i, t := range b.Tabs {
var style lipgloss.Style
isFirst, isActive := i == 0, i == b.activeTab
Expand All @@ -161,9 +170,10 @@ func (b Request) View() string {
}

style = style.Border(border)
renderedTabs = append(renderedTabs, zone.Mark("tab_"+t, style.Render(t)))
renderedTabs = append(renderedTabs, zone.Mark("tab_"+t, style.Render(t+" ")))
tabNames += t + " "
}
renderedTabs = append(renderedTabs, tabGap.Render(strings.Repeat(" ", b.width-43)))
renderedTabs = append(renderedTabs, tabGap.Render(strings.Repeat(" ", b.width-len(tabNames)-14)))

windowStyle = windowStyle.Height(b.height - 4)

Expand All @@ -179,34 +189,9 @@ func (b Request) View() string {

var content string
if b.activeTab == 0 {
content = emptyMessage.Render("No url params")
if b.call != nil {

u, err := url.Parse(b.call.Url)
if err == nil && b.call.Url != "" {
m, _ := url.ParseQuery(u.RawQuery)
if len(m) > 0 {

table := params.New(m, b.width, b.height)
content = lipgloss.NewStyle().
UnsetBold().
Render(
table.View(),
)
}
}
}
content = b.content.View()
} else if b.activeTab == 1 {
h := []string{}
if b.call != nil {
h = b.call.Headers
}
table := headers.New(h, b.width-2, b.width)
content = lipgloss.NewStyle().
UnsetBold().
Render(
table.View(),
)
content = b.content.View()
} else if b.activeTab == 2 {
content = b.content.View()
} else if b.activeTab == 3 {
Expand Down

0 comments on commit d9d54ca

Please sign in to comment.