Compare commits

...

2 Commits

Author SHA1 Message Date
f0b0139b79 Request data. 2025-12-01 23:33:26 +00:00
f0b979708d Create base layout for the rest page. 2025-11-28 23:10:05 +00:00
4 changed files with 110 additions and 21 deletions

View File

@@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) 2025 saeedafzal Copyright (c) 2025 Saeed Afzal
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, including associated documentation files (the "Software"), to deal in the Software without restriction, including

View File

@@ -1,15 +1,94 @@
package rest package rest
import "github.com/rivo/tview" import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type Rest struct{} type Rest struct {
app *tview.Application
func New() Rest { panels []tview.Primitive
return Rest{} index int
} }
func (r Rest) Root() *tview.Box { func New(app *tview.Application) *Rest {
return tview.NewBox(). return &Rest{
app,
make([]tview.Primitive, 5),
0,
}
}
func (r *Rest) Root() *tview.Flex {
flex := tview.NewFlex().
AddItem(r.request(), 0, 1, true).
AddItem(r.response(), 0, 1, false)
flex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyCtrlJ {
r.index = (r.index + 1) % len(r.panels)
r.app.SetFocus(r.panels[r.index])
return nil
} else if event.Key() == tcell.KeyCtrlK {
length := len(r.panels)
r.index = (r.index - 1 + length) % length
r.app.SetFocus(r.panels[r.index])
return nil
}
return event
})
return flex
}
func (r *Rest) request() *tview.Flex {
form := tview.NewForm().
AddDropDown("Method", []string{"GET", "POST", "PUT", "DELETE"}, 0, nil).
AddInputField("URL", "", 0, nil, nil).
AddButton("Headers", nil).
AddButton("Send", nil)
form.
SetBorder(true). SetBorder(true).
SetTitle("Hello Rest") SetTitle("Request Form")
body := tview.NewTextArea().
SetPlaceholder("Enter request body (ctrl + e to open editor)")
body.
SetBorder(true).
SetTitle("Request Body")
summary := tview.NewTextView()
summary.
SetBorder(true).
SetTitle("Request Summary")
r.panels[0] = form
r.panels[1] = body
r.panels[2] = summary
return tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(form, 9, 0, true).
AddItem(body, 0, 1, false).
AddItem(summary, 0, 1, false)
}
func (r *Rest) response() *tview.Flex {
summary := tview.NewTextView()
summary.
SetBorder(true).
SetTitle("Response Summary")
body := tview.NewTextView()
body.
SetBorder(true).
SetTitle("Response Body")
r.panels[3] = summary
r.panels[4] = body
return tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(summary, 0, 1, false).
AddItem(body, 0, 1, false)
} }

11
tui/rest/request.go Normal file
View File

@@ -0,0 +1,11 @@
package rest
import "net/http"
type Request struct{
method string
url string
headers http.Header
}
func (r Request) queryParams() {}

View File

@@ -2,12 +2,12 @@ package tui
import ( import (
"git.embergate.com/saeedafzal/restui/tui/rest" "git.embergate.com/saeedafzal/restui/tui/rest"
"github.com/rivo/tview"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
) )
type Tui struct { type Tui struct {
app *tview.Application app *tview.Application
pages *tview.Pages pages *tview.Pages
} }
@@ -16,18 +16,17 @@ func New(app *tview.Application) Tui {
} }
func (t Tui) Root() *tview.Pages { func (t Tui) Root() *tview.Pages {
rest := rest.New() rest := rest.New(t.app)
t.pages.AddPage("rest", rest.Root(), true, true) t.pages.AddPage("rest", rest.Root(), true, true)
t.pages.SetInputCapture(t.rootInputCapture)
t.pages.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Rune() == 'q' {
t.app.Stop()
return nil
}
return event
})
return t.pages return t.pages
} }
func (t Tui) rootInputCapture(event *tcell.EventKey) *tcell.EventKey {
if event.Rune() == 'q' {
t.app.Stop()
return nil
}
return event
}