-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_model.go
80 lines (65 loc) · 1.83 KB
/
response_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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type ResponseModel struct {
body viewport.Model
headers viewport.Model
paginator paginator.Model
border lipgloss.Style
}
func InitialResponseModel() *ResponseModel {
bodyViewPort := viewport.New(160, 9)
bodyViewPort.SetContent("ResponseModel body")
headersViewPort := viewport.New(160, 9)
headersViewPort.SetContent("ResponseModel headers")
newPaginator := paginator.New()
newPaginator.Type = paginator.Dots
newPaginator.SetTotalPages(2)
newPaginator.ActiveDot = StyleInactivecCurrentPageOnPaginator
newPaginator.InactiveDot = StyleInactivePageOnPaginator
return &ResponseModel{
body: bodyViewPort,
headers: headersViewPort,
paginator: newPaginator,
border: StyleResponseBorder,
}
}
func (rs ResponseModel) Init() tea.Cmd {
return nil
}
func (rs ResponseModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return rs, nil
}
func (rs ResponseModel) View() string {
var sBuilder strings.Builder
responseSlice := []string{rs.body.View(), rs.headers.View()}
start, end := rs.paginator.GetSliceBounds(len(responseSlice))
for _, item := range responseSlice[start:end] {
sBuilder.WriteString(string(item) + "\n")
}
sBuilder.WriteString(" " + rs.paginator.View())
return rs.border.Render(sBuilder.String())
}
func (m mainModel) saveResponseOutputToFile(content string, pathname string) {
dir := filepath.Dir(pathname)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
log.Println(err)
return
}
file, err := os.Create(pathname)
if err != nil {
log.Println("Error creating the output file: ", err)
return
}
defer file.Close()
fmt.Fprintf(file, "%s", content)
}