-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.go
More file actions
229 lines (173 loc) · 5.36 KB
/
views.go
File metadata and controls
229 lines (173 loc) · 5.36 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"fmt"
"github.com/devraza/fxtea/fx"
"github.com/charmbracelet/bubbles/table"
"strconv"
"strings"
)
func quadraticView(m model) string {
headerContent := fmt.Sprintf("Enter the values for a quadratic in the form %s", codeStyle.Render("ax² + bx + c"))
helpText := header(headerContent, []string{help("q", "return")})
arguments := strings.Split(m.TextInput.Value(), " ")
m.TextInput.Placeholder = "a b c"
var result string
if len(arguments) == 3 {
var coefficients []float64
for i := range arguments {
parsed, _ := strconv.ParseFloat(arguments[i], 64)
coefficients = append(coefficients, parsed)
}
roots := fx.Quadratic(coefficients[0], coefficients[1], coefficients[2])
if roots[0] != roots[1] {
result = fmt.Sprintf(
"The roots are %v and %v",
keywordStyle.Render(roots[0]),
keywordStyle.Render(roots[1]),
)
} else {
result = fmt.Sprintf(
"The root is %v",
keywordStyle.Render(roots[0]),
)
}
}
content := fmt.Sprintf(
"%s\n\n%v",
m.TextInput.View(),
result,
)
return fmt.Sprintf(helpText, content)
}
func poissonView(m model) string {
helpText := header("Enter the rate and the value of x", []string{help("q", "return")})
arguments := strings.Split(m.TextInput.Value(), " ")
m.TextInput.Placeholder = "λ x"
var result string
if len(arguments) == 2 {
lambda, _ := strconv.ParseFloat(arguments[0], 64)
x, _ := strconv.ParseUint(arguments[1], 10, 64)
result = fmt.Sprintf(
"The cumulative probability is: %s",
keywordStyle.Render(fmt.Sprintf("%.4f", fx.PoissonCD(lambda, x))),
)
}
content := fmt.Sprintf(
"%s\n\n%v",
m.TextInput.View(),
result,
)
return fmt.Sprintf(helpText, content)
}
func chiView(m model) string {
helpText := header("Enter the degrees of freedom and the significance level", []string{help("q", "return")})
arguments := strings.Split(strings.TrimSpace(m.TextInput.Value()), " ")
m.TextInput.Placeholder = "ν α"
var result string
if len(arguments) == 2 {
var floatArgs []float64
for i := range arguments {
parsed, _ := strconv.ParseFloat(arguments[i], 64)
floatArgs = append(floatArgs, parsed)
}
critical := fx.ChiCritical(floatArgs[0], floatArgs[1])
result = fmt.Sprintf(
"The critical value is %v",
keywordStyle.Render(fx.FormatFloat(critical)),
)
}
content := fmt.Sprintf("%s\n\n%v", m.TextInput.View(), result)
return fmt.Sprintf(helpText, content)
}
func fibonacciView(m model) string {
helpText := header("Enter the length of the generated sequence", []string{help("q", "return")})
m.TextInput.Placeholder = "n"
parsed, _ := strconv.ParseUint(m.TextInput.Value(), 10, 64)
sequence := fx.Fibonacci(parsed)
formattedSequence := ""
for _, v := range sequence {
formattedSequence += listStyle.Render(strconv.FormatUint(v, 10)) + " "
}
content := fmt.Sprintf("%s\n\n%s", m.TextInput.View(), formattedSequence)
return fmt.Sprintf(helpText, content)
}
func binarySearchView(m model) string {
headerText := fmt.Sprintf(
"%s %s",
"Enter the query in the format",
codeStyle.Render("<query> <list>"),
)
helpText := header(headerText, []string{help("q", "return")})
arguments := strings.Split(strings.TrimSpace(m.TextInput.Value()), " ")
m.TextInput.Placeholder = "3 1 2 3 4 5"
query, _ := strconv.ParseInt(arguments[0], 10, 64)
var integerList []int64
for _, v := range arguments[1:] {
parsed, _ := strconv.ParseInt(v, 10, 64)
integerList = append(integerList, parsed)
}
integerList = fx.QuickSort(integerList)
result := fx.BinarySearch(integerList, query)
sortedText := ""
if len(integerList) > 0 {
sortedText = "The sorted list is "
for _, v := range integerList {
sortedText += listStyle.Render(strconv.FormatInt(v, 10)) + " "
}
}
resultText := errorStyle.Render("The element is not in the list")
if result >= 0 {
resultText = fmt.Sprintf(
"Element %v found at index %v",
keywordStyle.Render(strconv.FormatInt(query, 10)),
resultStyle.Render(strconv.FormatInt(result, 10)),
)
}
content := fmt.Sprintf("%s\n\n%s\n\n%s", m.TextInput.View(), sortedText, resultText)
return fmt.Sprintf(helpText, content)
}
func pmccView(m model) string {
headerText := "Add rows to the table"
helpText := header(headerText, []string{help("enter", "add row to table"), help("q", "return")})
pmccColumns := []table.Column{
{Title: "x", Width: 10},
{Title: "y", Width: 10},
}
m.TextInput.Placeholder = "x y"
pmccTable := table.New(
table.WithColumns(pmccColumns),
table.WithFocused(false),
table.WithHeight(10),
)
style := table.DefaultStyles()
style.Header = tableHeaderStyle
style.Selected = tableSelectedStyle
pmccTable.SetStyles(style)
pmccTable.FromValues(m.PMCCData, " ")
var resultText string
rows_str := strings.Split(m.PMCCData, "\n")
if len(rows_str) < 3 {
resultText = "Enter the data first"
} else {
var x, y []float64
for _, row := range rows_str {
values := strings.Split(row, " ")
if len(values) < 2 {
break
}
var valX, valY float64
valX, _ = strconv.ParseFloat(values[0], 64)
valY, _ = strconv.ParseFloat(values[1], 64)
x = append(x, valX)
y = append(y, valY)
}
r, err := fx.PMCC(x, y)
if err != nil {
resultText = fmt.Sprintf("ERR: %s", err)
} else {
resultText = fmt.Sprintf("r = %.4f", r)
}
}
content := fmt.Sprintf("%s\n\n%s\n\n%s", m.TextInput.View(), resultText, pmccTable.View())
return fmt.Sprintf(helpText, content)
}