-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_model.go
81 lines (64 loc) · 1.66 KB
/
url_model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type httpMethod int
const (
GET httpMethod = iota
POST
PUT
DELETE
)
func (hm httpMethod) String() string {
return []string{"GET", "POST", "PUT", "DELETE"}[hm]
}
type Url struct {
methods []string
chosenMethod httpMethod
textInput textinput.Model
border lipgloss.Style
httpMethodPaginator paginator.Model
}
func InitialUrlModel() *Url {
newTextInput := textinput.New()
newTextInput.Placeholder = "https://www.example.com/"
newTextInput.Focus()
methodsSlice := []string{"GET", "POST", "PUT", "DELETE"}
newPaginator := paginator.New()
newPaginator.Type = paginator.Dots
newPaginator.SetTotalPages(len(methodsSlice))
newPaginator.ActiveDot = StyleInactivecCurrentPageOnPaginator
newPaginator.InactiveDot = StyleInactivePageOnPaginator
return &Url{
methods: methodsSlice,
chosenMethod: GET,
textInput: newTextInput,
border: StyleRequestBorder,
httpMethodPaginator: newPaginator,
}
}
func (u Url) Init() tea.Cmd {
return nil
}
func (u *Url) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return u, nil
}
func (u Url) View() string {
var stringBuilder strings.Builder
start, end := u.httpMethodPaginator.GetSliceBounds(len(u.methods))
for _, method := range u.methods[start:end] {
stringBuilder.WriteString(" " + string(method) + "\n")
}
stringBuilder.WriteString(" " + u.httpMethodPaginator.View())
s := fmt.Sprintf(
"\n%s\n\n%s\n",
stringBuilder.String(),
u.textInput.View(),
)
return u.border.Render(s)
}