Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
devShells.x86_64-linux.default =
let pkgs = import nixpkgs { system = "x86_64-linux"; };
in pkgs.mkShell {
buildInputs = [ pkgs.go pkgs.zsh ];
buildInputs = [ pkgs.go pkgs.zsh pkgs.sqlite ];

shellHook = ''
export GOPATH=$PWD/.gopath
Expand Down
25 changes: 25 additions & 0 deletions global/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package global

import (
"github.com/maniac-en/req/internal/collections"
"github.com/maniac-en/req/internal/endpoints"
"github.com/maniac-en/req/internal/history"
"github.com/maniac-en/req/internal/http"
)

type AppContext struct {
Collections *collections.CollectionsManager
Endpoints *endpoints.EndpointsManager
HTTP *http.HTTPManager
History *history.HistoryManager
}

var globalAppContext *AppContext

func SetAppContext(ctx *AppContext) {
globalAppContext = ctx
}

func GetAppContext() *AppContext {
return globalAppContext
}
21 changes: 21 additions & 0 deletions global/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package global

type State struct {
currentCollection string
}

func NewGlobalState() *State {
return &State{
currentCollection: "",
}
}

// Gets the current collection from the app state
func (s *State) GetCurrentCollection() string {
return s.currentCollection
}

// Sets the current collection to the app state
func (s *State) SetCurrentCollection(collection string) {
s.currentCollection = collection
}
28 changes: 21 additions & 7 deletions internal/app/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/maniac-en/req/global"
"github.com/maniac-en/req/internal/messages"
"github.com/maniac-en/req/internal/tabs"
)
Expand All @@ -12,19 +13,27 @@ type Model struct {
activeTab int
width int
height int

// Global state for sharing data
state *global.State
}

func InitialModel() Model {
tabList := []tabs.Tab{
tabs.NewCollectionsTab(),
tabs.NewAddCollectionTab(),
tabs.NewEditCollectionTab(),
}

globalState := global.NewGlobalState()

return Model{
tabs: tabList,
activeTab: 0,
state: globalState,
tabs: []tabs.Tab{
tabs.NewCollectionsTab(globalState),
tabs.NewAddCollectionTab(),
tabs.NewEditCollectionTab(),
tabs.NewEndpointsTab(globalState),
tabs.NewAddEndpointTab(globalState),
tabs.NewEditEndpointTab(globalState),
},
}

}

func (m Model) Init() tea.Cmd {
Expand All @@ -47,6 +56,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
editTab.SetEditingCollection(msg.Label, msg.Value)
}
return m, nil
case messages.EditEndpointMsg:
if editTab, ok := m.tabs[5].(*tabs.EditEndpointTab); ok {
editTab.SetEditingEndpoint(msg)
}
return m, nil

case tea.KeyMsg:
switch msg.String() {
Expand Down
7 changes: 7 additions & 0 deletions internal/messages/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ type EditCollectionMsg struct {
Label string
Value string
}

type EditEndpointMsg struct {
Name string
Method string
URL string
ID string
}
11 changes: 9 additions & 2 deletions internal/tabs/add-collections.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package tabs

import (
"context"
"strconv"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/maniac-en/req/global"
"github.com/maniac-en/req/internal/messages"
)

Expand Down Expand Up @@ -98,9 +102,12 @@ func (a *AddCollectionTab) View() string {
}

func (a *AddCollectionTab) addCollection(name string) (Tab, tea.Cmd) {
ctx := global.GetAppContext()
collection, _ := ctx.Collections.Create(context.Background(), name)
value := strconv.Itoa(int(collection.GetID()))
newOption := OptionPair{
Label: name,
Value: name,
Label: collection.GetName(),
Value: value,
}

GlobalCollections = append(GlobalCollections, newOption)
Expand Down
156 changes: 156 additions & 0 deletions internal/tabs/add-endpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package tabs

import (
"context"
"strconv"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/maniac-en/req/global"
"github.com/maniac-en/req/internal/endpoints"
"github.com/maniac-en/req/internal/messages"
)

type AddEndpointTab struct {
name string
inputs []textinput.Model
focusedInput int
state *global.State
focused bool
}

func NewAddEndpointTab(globalState *global.State) *AddEndpointTab {
name := textinput.New()
name.Placeholder = "Enter your endpoint's name... "
name.CharLimit = 100
name.Width = 50

method := textinput.New()
method.Placeholder = "Enter your method... "
method.CharLimit = 100
method.Width = 50

url := textinput.New()
url.Placeholder = "Enter your url..."
url.CharLimit = 100
url.Width = 50

name.Focus()

return &AddEndpointTab{
name: "Add Endpoint",
inputs: []textinput.Model{
name,
method,
url,
},
focused: true,
state: globalState,
}
}

func (a *AddEndpointTab) Name() string {
return a.name
}
func (a *AddEndpointTab) Instructions() string {
return "none"
}
func (a *AddEndpointTab) Init() tea.Cmd {
return textinput.Blink
}
func (a *AddEndpointTab) Update(msg tea.Msg) (Tab, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
if a.inputs[0].Value() != "" && a.inputs[1].Value() != "" && a.inputs[2].Value() != "" {
return a.addEndpoint(a.inputs[0].Value(), a.inputs[1].Value(), a.inputs[2].Value())
}
return a, nil
case "tab":
a.inputs[a.focusedInput].Blur()
a.focusedInput = (a.focusedInput + 1) % len(a.inputs)
a.inputs[a.focusedInput].Focus()
case "esc":
return a, func() tea.Msg {
return messages.SwitchTabMsg{TabIndex: 3}
}
}
}

a.inputs[a.focusedInput], _ = a.inputs[a.focusedInput].Update(msg)
return a, nil
}
func (a *AddEndpointTab) View() string {
titleStyle := lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("205")).
MarginBottom(2)

inputStyle := lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62")).
Padding(1, 2).
MarginBottom(2)

form := lipgloss.JoinVertical(lipgloss.Center,
titleStyle.Render("Create New Endpoint"),
inputStyle.Render(a.inputs[0].View()),
inputStyle.Render(a.inputs[1].View()),
inputStyle.Render(a.inputs[2].View()),
)

containerStyle := lipgloss.NewStyle().
Width(60).
Height(20).
Align(lipgloss.Center, lipgloss.Center)

return containerStyle.Render(form)
}

func (a *AddEndpointTab) OnFocus() tea.Cmd {
a.inputs[a.focusedInput].Focus()
a.focused = true
return textinput.Blink
}

func (a *AddEndpointTab) OnBlur() tea.Cmd {
a.inputs[0].Blur()
a.inputs[1].Blur()
a.inputs[2].Blur()
a.focused = false
return nil
}

func (a *AddEndpointTab) addEndpoint(name, method, url string) (Tab, tea.Cmd) {
ctx := global.GetAppContext()

collectionId := a.state.GetCurrentCollection()
int64Collection, err := strconv.ParseInt(collectionId, 10, 64)
if err != nil {
return a, func() tea.Msg {
return messages.SwitchTabMsg{TabIndex: 3}
}
}

_, _ = ctx.Endpoints.CreateEndpoint(context.Background(), endpoints.EndpointData{
Name: name,
Method: method,
URL: url,
CollectionID: int64Collection,
})

// newOption := OptionPair{
// Label: collection.GetName(),
// Value: string(collection.GetID()),
// }

a.inputs[0].SetValue("")
a.inputs[1].SetValue("")
a.inputs[2].SetValue("")

return a, func() tea.Msg {
return messages.SwitchTabMsg{TabIndex: 3}
}
}
Loading