-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathhelpers.go
266 lines (235 loc) · 8.1 KB
/
helpers.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// helpers.go contains various helper functions used in the main application.
package main
import (
"fmt"
"os"
"strings"
"github.com/charmbracelet/lipgloss"
"github.com/sammcj/gollama/config"
"github.com/sammcj/gollama/logging"
"github.com/sammcj/gollama/styles"
"github.com/ollama/ollama/api"
"golang.org/x/term"
)
func parseAPIResponse(resp *api.ListResponse) []Model {
logging.DebugLogger.Println("Fetching models from API")
models := make([]Model, len(resp.Models))
for i, modelResp := range resp.Models {
models[i] = Model{
Name: modelResp.Name,
ID: truncate(modelResp.Digest, 7),
Size: float64(modelResp.Size) / (1024 * 1024 * 1024), // Convert bytes to GB
QuantizationLevel: modelResp.Details.QuantizationLevel,
Family: modelResp.Details.Family,
Modified: modelResp.ModifiedAt,
ParameterSize: modelResp.Details.ParameterSize,
}
}
logging.DebugLogger.Println("Models:", models)
return models
}
func normalizeSize(size float64) float64 {
return size // Sizes are already in GB in the API response
}
// Constant for parameter size column width
const minParamSizeWidth = 10
func calculateColumnWidths(totalWidth int) (nameWidth, sizeWidth, quantWidth, modifiedWidth, idWidth, familyWidth, paramSizeWidth int) {
// Calculate column widths
nameWidth = int(0.40 * float64(totalWidth))
sizeWidth = int(0.05 * float64(totalWidth))
quantWidth = int(0.05 * float64(totalWidth))
familyWidth = int(0.05 * float64(totalWidth))
modifiedWidth = int(0.05 * float64(totalWidth))
idWidth = int(0.02 * float64(totalWidth))
paramSizeWidth = int(0.05 * float64(totalWidth))
// Set the absolute minimum width for each column
if nameWidth < minNameWidth {
nameWidth = minNameWidth
}
if sizeWidth < minSizeWidth {
sizeWidth = minSizeWidth
}
if quantWidth < minQuantWidth {
quantWidth = minQuantWidth
}
if modifiedWidth < minModifiedWidth {
modifiedWidth = minModifiedWidth
}
if idWidth < minIDWidth {
idWidth = minIDWidth
}
if familyWidth < minFamilyWidth {
familyWidth = minFamilyWidth
}
if paramSizeWidth < minParamSizeWidth {
paramSizeWidth = minParamSizeWidth
}
// If the total width is less than the sum of the minimum column widths, adjust the name column width and make sure all columns are aligned
if totalWidth < nameWidth+sizeWidth+quantWidth+familyWidth+modifiedWidth+idWidth+paramSizeWidth {
nameWidth = totalWidth - sizeWidth - quantWidth - familyWidth - modifiedWidth - idWidth - paramSizeWidth
}
return
}
func removeModels(models []Model, selectedModels []Model) []Model {
result := make([]Model, 0)
for _, model := range models {
found := false
for _, selectedModel := range selectedModels {
if model.Name == selectedModel.Name {
found = true
break
}
}
if !found {
result = append(result, model)
}
}
return result
}
// truncate ensures the string fits within the specified width
func truncate(text string, width int) string {
if len(text) > width {
return text[:width]
}
return text
}
// wrapText ensures the text wraps to the next line if it exceeds the column width
func wrapText(text string, width int) string {
var wrapped string
for len(text) > width {
wrapped += text[:width]
text = text[width:] + " "
}
wrapped += text
return wrapped
}
func calculateColumnWidthsTerminal() (nameWidth, sizeWidth, quantWidth, modifiedWidth, idWidth, familyWidth, paramSizeWidth int) {
// use the terminal width to calculate column widths
minWidth := 120
width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
logging.ErrorLogger.Println("Error getting terminal size:", err)
width = minWidth
}
// make sure there's at least minWidth characters for each column
if width < minWidth {
width = minWidth
}
return calculateColumnWidths(width)
}
func listModels(models []Model) {
// read the config file to see if the user wants to strip a string from the model name
cfg, err := config.LoadConfig()
if err != nil {
fmt.Println("Error loading config:", err)
os.Exit(1)
}
if len(models) == 0 {
fmt.Println("No models available to display.")
return
}
stripString := cfg.StripString
nameWidth, sizeWidth, quantWidth, modifiedWidth, idWidth, familyWidth, paramSizeWidth := calculateColumnWidthsTerminal()
// Add extra spacing between columns
colSpacing := 2
longestNameAllowed := 60
// Create the header with proper padding and alignment
header := fmt.Sprintf("%-*s%-*s%-*s%-*s%-*s%-*s%-*s",
nameWidth, "Name",
sizeWidth+colSpacing, "Size",
paramSizeWidth+colSpacing, "Params",
quantWidth+colSpacing, "Quant",
familyWidth+colSpacing, "Family",
modifiedWidth+colSpacing, "Modified",
idWidth, "ID")
// if stripString is set, replace the model name with the stripped string
if stripString != "" {
for i, model := range models {
models[i].Name = strings.Replace(model.Name, stripString, "", 1)
}
}
// Prepare columns for padding
var names, sizes, quants, families, modified, ids, paramSizes []string
var longestName int
for _, model := range models {
if len(model.Name) > longestName {
longestName = len(model.Name)
}
// truncate long names
if len(model.Name) > longestNameAllowed {
model.Name = model.Name[:longestNameAllowed] + "..."
}
names = append(names, model.Name)
sizes = append(sizes, fmt.Sprintf("%.2fGB", model.Size))
paramSizes = append(paramSizes, model.ParameterSize)
quants = append(quants, model.QuantizationLevel)
families = append(families, model.Family)
modified = append(modified, model.Modified.Format("2006-01-02"))
ids = append(ids, model.ID)
}
// Calculate maximum width for each column
maxNameWidth := nameWidth
maxSizeWidth := sizeWidth + colSpacing
maxParamSizeWidth := paramSizeWidth + colSpacing
maxQuantWidth := quantWidth + colSpacing
maxFamilyWidth := familyWidth + colSpacing
maxModifiedWidth := modifiedWidth + colSpacing
maxIdWidth := idWidth
// Pad columns to ensure alignment with calculated widths
for i := range names {
names[i] = fmt.Sprintf("%-*s", maxNameWidth, names[i])
sizes[i] = fmt.Sprintf("%-*s", maxSizeWidth, sizes[i])
paramSizes[i] = fmt.Sprintf("%-*s", maxParamSizeWidth, paramSizes[i])
quants[i] = fmt.Sprintf("%-*s", maxQuantWidth, quants[i])
families[i] = fmt.Sprintf("%-*s", maxFamilyWidth, families[i])
modified[i] = fmt.Sprintf("%-*s", maxModifiedWidth, modified[i])
// if the longest name is more than longestNameAllowed characters, don't display the model sha
if longestName > longestNameAllowed {
ids[i] = ""
// remove the ID header
header = fmt.Sprintf("%-*s%-*s%-*s%-*s%-*s%-*s",
nameWidth, "Name",
sizeWidth+colSpacing, "Size",
paramSizeWidth+colSpacing, "Params",
quantWidth+colSpacing, "Quant",
familyWidth+colSpacing, "Family",
modifiedWidth, "Modified")
} else {
ids[i] = fmt.Sprintf("%-*s", maxIdWidth, ids[i])
}
}
// Print the header
fmt.Println(styles.HeaderStyle().Render(header))
modelList := []string{}
for index, model := range models {
name := styles.ItemNameStyle(index).Render(names[index])
id := styles.ItemIDStyle().Render(ids[index])
size := styles.SizeStyle(model.Size).Render(sizes[index])
// Apply direct color based on parameter size
var paramSize string
if paramSizes[index] != "" {
// Format the string first
formattedParamSize := fmt.Sprintf("%-*s", maxParamSizeWidth, paramSizes[index])
// Apply color directly using paramSizeColour
paramSize = lipgloss.NewStyle().Foreground(paramSizeColour(paramSizes[index])).Render(formattedParamSize)
} else {
paramSize = fmt.Sprintf("%-*s", maxParamSizeWidth, paramSizes[index])
}
family := styles.FamilyStyle(model.Family).Render(families[index])
quant := styles.QuantStyle(model.QuantizationLevel).Render(quants[index])
modified := styles.ItemIDStyle().Render(modified[index])
row := fmt.Sprintf("%-*s%-*s%-*s%-*s%-*s%-*s%-*s",
maxNameWidth, name,
maxSizeWidth, size,
maxParamSizeWidth, paramSize,
maxQuantWidth, quant,
maxFamilyWidth, family,
maxModifiedWidth, modified,
maxIdWidth, id)
modelList = append(modelList, row)
}
// Print the models with proper spacing
for _, row := range modelList {
fmt.Printf("%s\n", row)
}
}