Skip to content

Commit

Permalink
Merge pull request #16 from hatsu38/feature/add-command-comment
Browse files Browse the repository at this point in the history
Add command comment
  • Loading branch information
hatsu38 authored Aug 24, 2021
2 parents 963aeeb + be0b3a7 commit b4fa247
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 205 deletions.
253 changes: 54 additions & 199 deletions ui/ui.go → gui/gui.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package ui
package gui

import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"

Expand All @@ -16,81 +15,38 @@ import (
type Gui struct {
App *tview.Application
Pages *tview.Pages
UrlField *tview.InputField
ParamsTable *tview.Table
BodyTable *tview.Table
ResTextView *tview.TextView
HTTPTextView *tview.TextView
UrlField *urlField
ParamsTable *table
BodyTable *table
ResTextView *resTestView
HTTPTextView *httpTestView
NavTextView *navigate
}

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

func New() *Gui {
g := &Gui{
App: NewApplication(),
App: tview.NewApplication(),
Pages: tview.NewPages(),
UrlField: NewForm(" Request URL: ", "https://httpbin.org/get"),
ParamsTable: NewTable(),
BodyTable: NewTable(),
ResTextView: NewTextView(" Response ", ""),
HTTPTextView: NewTextView(" HTTP Method ", "GET"),
UrlField: newUrlField(" Request URL: ", "https://httpbin.org/get"),
ParamsTable: newTable(),
BodyTable: newTable(),
ResTextView: newResTextView(),
HTTPTextView: newHTTPTextView(),
NavTextView: newNavigate(),
}
return g
}

func NewApplication() *tview.Application {
return tview.NewApplication().EnableMouse(true)
}

func NewForm(label, text string) *tview.InputField {
field := tview.NewInputField()
field.SetLabel(label)
field.SetFieldBackgroundColor(tcell.ColorBlack)
field.SetBorder(true)
field.SetBorderColor(tcell.ColorGreen)
field.SetLabelColor(tcell.ColorIndianRed)
field.SetText(text)

return field
}

func NewTable() *tview.Table {
table := tview.NewTable()
table.SetBorders(true)
table.SetFixed(1, 3)

return table
}

func NewTextView(title, text string) *tview.TextView {
textView := tview.NewTextView()
textView.SetTitle(title)
textView.SetBorder(true)
textView.SetScrollable(true)
textView.SetText(text)
textView.SetTextColor(tcell.ColorGreen)
textView.SetToggleHighlights(true)

return textView
}

func (g *Gui) GetRequestUrl() string {
field := g.UrlField
urlText := field.GetText()
params := g.GetParams(g.ParamsTable)
query := g.GetParamsText(params)
query := g.ParamsTable.GetQuery()

return urlText + query
}

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

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

method := g.HTTPTextView.GetText(true)
req, _ := http.NewRequest(method, url, strings.NewReader(value))
Expand Down Expand Up @@ -128,46 +84,13 @@ func (g *Gui) Run(i interface{}) error {
paramsTable := g.ParamsTable
bodyTable := g.BodyTable

g.SetTableCells(paramsTable, "Query Params")
g.SetTableCells(bodyTable, "Request Body")

httpTextView.SetTextAlign(tview.AlignCenter)
httpTextView.SetDoneFunc(func(key tcell.Key) {
switch key {
case tcell.KeyEnter:
g.NewHTTPListModal()
case tcell.KeyTab:
g.ToFocus()
}
})

inputUrlField.SetDoneFunc(func(key tcell.Key) {
switch key {
case tcell.KeyEnter:
url := g.GetRequestUrl()
resp := g.HttpRequest(url)
defer resp.Body.Close()

body := g.ParseResponse(resp)
resTextView.SetText(body)
case tcell.KeyTab:
g.ToFocus()
}
})

paramsTable.SetDoneFunc(func(key tcell.Key) {
switch key {
case tcell.KeyTab:
g.ToFocus()
}
})
paramsTable.SetTableCells(g, "Query Params")
bodyTable.SetTableCells(g, "Request Body")

bodyTable.SetDoneFunc(func(key tcell.Key) {
switch key {
case tcell.KeyTab:
g.ToFocus()
}
})
httpTextView.setFunc(g)
inputUrlField.setFunc(g)
paramsTable.setFunc(g)
bodyTable.setFunc(g)

httpFlex := tview.NewFlex()
httpFlex.SetDirection(tview.FlexColumn)
Expand All @@ -180,12 +103,19 @@ func (g *Gui) Run(i interface{}) error {
requestFlex.AddItem(paramsTable, 0, 5, false)
requestFlex.AddItem(bodyTable, 0, 5, false)

flex := tview.NewFlex()
flex.SetDirection(tview.FlexColumn)
flex.AddItem(requestFlex, 0, 5, true)
flex.AddItem(resTextView, 0, 3, false)
reqResflex := tview.NewFlex()
reqResflex.SetDirection(tview.FlexColumn)
reqResflex.AddItem(requestFlex, 0, 5, true)
reqResflex.AddItem(resTextView, 0, 3, false)

appFlex := tview.NewFlex()
appFlex.SetDirection(tview.FlexRow)
appFlex.AddItem(reqResflex, 0, 9, true)
appFlex.AddItem(g.NavTextView, 1, 1, false)

g.Pages.AddAndSwitchToPage("main", flex, true)
g.ToUrlFieldFocus()

g.Pages.AddAndSwitchToPage("main", appFlex, true)

if err := app.SetRoot(g.Pages, true).Run(); err != nil {
log.Println(err)
Expand All @@ -195,16 +125,6 @@ func (g *Gui) Run(i interface{}) error {
return nil
}

func (g *Gui) ToParamsTableFocus() {
g.App.SetFocus(g.ParamsTable)
g.ParamsTable.SetSelectable(true, true)
g.HTTPTextView.SetBorderColor(tcell.ColorWhite)
g.UrlField.SetBorderColor(tcell.ColorWhite)
g.BodyTable.SetBorderColor(tcell.ColorWhite)

g.ParamsTable.SetBordersColor(tcell.ColorGreen)
}

func (g *Gui) ToUrlFieldFocus() {
urlField := g.UrlField
g.App.SetFocus(urlField)
Expand All @@ -213,6 +133,7 @@ func (g *Gui) ToUrlFieldFocus() {
g.BodyTable.SetBorderColor(tcell.ColorWhite)

urlField.SetBorderColor(tcell.ColorGreen)
g.NavTextView.update("url")
}

func (g *Gui) ToHTTPFieldFocus() {
Expand All @@ -223,6 +144,19 @@ func (g *Gui) ToHTTPFieldFocus() {
g.UrlField.SetBorderColor(tcell.ColorWhite)

g.HTTPTextView.SetBorderColor(tcell.ColorGreen)
g.NavTextView.update("http")
}

func (g *Gui) ToParamsTableFocus() {
g.App.SetFocus(g.ParamsTable)
g.ParamsTable.SetSelectable(true, true)
g.HTTPTextView.SetBorderColor(tcell.ColorWhite)
g.UrlField.SetBorderColor(tcell.ColorWhite)
g.BodyTable.SetBorderColor(tcell.ColorWhite)

g.ParamsTable.SetBordersColor(tcell.ColorGreen)
g.NavTextView.update("paramsTable")

}

func (g *Gui) ToBodyTable() {
Expand All @@ -234,6 +168,7 @@ func (g *Gui) ToBodyTable() {
g.HTTPTextView.SetBorderColor(tcell.ColorWhite)

g.BodyTable.SetBordersColor(tcell.ColorGreen)
g.NavTextView.update("bodyTable")
}

func (g *Gui) ToFocus() {
Expand All @@ -248,10 +183,12 @@ func (g *Gui) ToFocus() {
g.ToHTTPFieldFocus()
case g.HTTPTextView:
g.ToUrlFieldFocus()
default:
g.ToUrlFieldFocus()
}
}

func (g *Gui) NewInputModal(table *tview.Table) {
func (g *Gui) NewInputModal(table *table) {
row, col := table.GetSelection()
cell := table.GetCell(row, col)
cell.SetTextColor(tcell.ColorWhite)
Expand All @@ -261,14 +198,14 @@ func (g *Gui) NewInputModal(table *tview.Table) {
labelIndexCell := table.GetCell(row, 0)
tableTitle := table.GetCell(0, 0)
label := fmt.Sprintf(" %s %s %s: ", tableTitle.Text, labelCell.Text, labelIndexCell.Text)
input := NewForm(label, text)
input := newUrlField(label, text)
input.SetDoneFunc(func(key tcell.Key) {
switch key {
case tcell.KeyEnter:
txt := input.GetText()
cell.Text = txt
if txt != "" {
g.AddParamsRow(table, row+1)
table.AddParamsRow(row + 1)
}
g.Pages.RemovePage("input")
g.App.SetFocus(table)
Expand Down Expand Up @@ -310,85 +247,3 @@ func (g *Gui) Modal(p tview.Primitive, width, height int) tview.Primitive {

return grid
}

func (g *Gui) SetTableCells(table *tview.Table, title string) {
// 選択された状態でEnterされたとき
table.SetSelectedFunc(func(row, column int) {
g.NewInputModal(table)
})
g.AddTableHeader(table, title)
g.AddParamsRow(table, 1)
}

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

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

func (g *Gui) SetTableCell(title string, width int, color tcell.Color, selectable bool) *tview.TableCell {
tcell := tview.NewTableCell(title)
tcell.SetExpansion(width)
tcell.SetAlign(tview.AlignCenter)
tcell.SetTextColor(color)
tcell.SetSelectable(selectable)
tcell.SetTransparency(true)

return tcell
}

func (g *Gui) GetParams(table *tview.Table) Params {
var params Params

rows := table.GetRowCount()
for r := 1; r < rows; r++ {
key := table.GetCell(r, 1).Text
value := table.GetCell(r, 2).Text
param := Param{
Key: key,
Value: value,
}
params = append(params, param)
}

return params
}

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()
}
36 changes: 36 additions & 0 deletions gui/http_text_view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package gui

import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)

type httpTestView struct {
*tview.TextView
}

func newHTTPTextView() *httpTestView {
textView := &httpTestView{
tview.NewTextView(),
}
textView.SetTitle(" HTTP ")
textView.SetBorder(true)
textView.SetScrollable(true)
textView.SetText("GET")
textView.SetTextColor(tcell.ColorGreen)
textView.SetToggleHighlights(true)
textView.SetTextAlign(tview.AlignCenter)

return textView
}

func (t *httpTestView) setFunc(g *Gui) {
t.SetDoneFunc(func(key tcell.Key) {
switch key {
case tcell.KeyEnter:
g.NewHTTPListModal()
case tcell.KeyTab:
g.ToFocus()
}
})
}
Loading

0 comments on commit b4fa247

Please sign in to comment.