Skip to content

Commit

Permalink
Add params
Browse files Browse the repository at this point in the history
  • Loading branch information
hatsu38 committed Aug 23, 2021
1 parent 1f37a0a commit 708fa81
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 49 deletions.
14 changes: 7 additions & 7 deletions ui/table.go
Expand Up @@ -40,18 +40,18 @@ func (t *table) SetTableCells(g *Gui, title string) {
}

func (t *table) AddTableHeader(cellTxt string) {
t.SetCell(0, 0, t.SetTableCell(cellTxt, 1, tcell.ColorIndianRed, false))
t.SetCell(0, 1, t.SetTableCell("Key", 3, tcell.ColorIndianRed, false))
t.SetCell(0, 2, t.SetTableCell("Value", 3, tcell.ColorIndianRed, false))
t.SetCell(0, 0, SetTableCell(cellTxt, 1, tcell.ColorIndianRed, false))
t.SetCell(0, 1, SetTableCell("Key", 3, tcell.ColorIndianRed, false))
t.SetCell(0, 2, SetTableCell("Value", 3, tcell.ColorIndianRed, false))
}

func (t *table) AddParamsRow(idx int) {
t.SetCell(idx, 0, t.SetTableCell(fmt.Sprint(idx), 1, tcell.ColorWhite, false))
t.SetCell(idx, 1, t.SetTableCell("", 3, tcell.ColorWhite, true))
t.SetCell(idx, 2, t.SetTableCell("", 3, tcell.ColorWhite, true))
t.SetCell(idx, 0, SetTableCell(fmt.Sprint(idx), 1, tcell.ColorWhite, false))
t.SetCell(idx, 1, SetTableCell("", 3, tcell.ColorWhite, true))
t.SetCell(idx, 2, SetTableCell("", 3, tcell.ColorWhite, true))
}

func (t *table) SetTableCell(title string, width int, color tcell.Color, selectable bool) *tview.TableCell {
func SetTableCell(title string, width int, color tcell.Color, selectable bool) *tview.TableCell {
tcell := tview.NewTableCell(title)
tcell.SetExpansion(width)
tcell.SetAlign(tview.AlignCenter)
Expand Down
44 changes: 2 additions & 42 deletions ui/ui.go
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"log"
"net/http"
"net/url"
"os"
"strings"

Expand All @@ -24,12 +23,6 @@ type Gui struct {
NavTextView *navigate
}

type Param struct {
Key string
Value string
}
type Params []Param

func New() *Gui {
g := &Gui{
App: tview.NewApplication(),
Expand All @@ -48,15 +41,15 @@ func (g *Gui) GetRequestUrl() string {
field := g.UrlField
urlText := field.GetText()
params := g.ParamsTable.GetParams()
query := g.GetParamsText(params)
query := params.GetParamsText()

return urlText + query
}

func (g *Gui) HttpRequest(url string) *http.Response {

bodyParams := g.BodyTable.GetParams()
value := g.GetBodyParamsText(bodyParams)
value := bodyParams.GetBodyParamsText()

method := g.HTTPTextView.GetText(true)
req, _ := http.NewRequest(method, url, strings.NewReader(value))
Expand Down Expand Up @@ -257,36 +250,3 @@ func (g *Gui) Modal(p tview.Primitive, width, height int) tview.Primitive {

return grid
}

func (g *Gui) GetParamsText(params Params) string {
var query string
for i, v := range params {
if v.Key == "" || v.Value == "" {
continue
}
if i == 0 {
query += "?"
} else {
query += "&"
}
query += fmt.Sprintf("%s=%s", v.Key, v.Value)
}

return query
}

func (g *Gui) GetBodyParamsText(params Params) string {
val := url.Values{}
for i, v := range params {
if v.Key == "" || v.Value == "" {
continue
}
if i == 0 {
val.Set(v.Key, v.Value)
} else {
val.Add(v.Key, v.Value)
}
}

return val.Encode()
}

0 comments on commit 708fa81

Please sign in to comment.