Skip to content

Commit f5bfd9d

Browse files
committed
Merge branch 'dev'
2 parents 9e3639e + 14a21de commit f5bfd9d

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

docs/custom-api.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ Output:
238238
<div>90</div>
239239
```
240240

241-
Other operations include `add`, `mul`, and `div`.
241+
Other operations include `add`, `mul`, `div` and `mod`.
242242

243243
<hr>
244244

@@ -415,6 +415,14 @@ The following functions are available on the `JSON` object:
415415
- `Array(key string) []JSON`: Returns the value of the key as an array of `JSON` objects.
416416
- `Exists(key string) bool`: Returns true if the key exists in the JSON object.
417417

418+
The following functions are available on the `Options` object:
419+
420+
- `StringOr(key string, default string) string`: Returns the value of the key as a string, or the default value if the key does not exist.
421+
- `IntOr(key string, default int) int`: Returns the value of the key as an integer, or the default value if the key does not exist.
422+
- `FloatOr(key string, default float) float`: Returns the value of the key as a float, or the default value if the key does not exist.
423+
- `BoolOr(key string, default bool) bool`: Returns the value of the key as a boolean, or the default value if the key does not exist.
424+
- `JSON(key string) JSON`: Returns the value of the key as a stringified `JSON` object, or throws an error if the key does not exist.
425+
418426
The following helper functions provided by Glance are available:
419427

420428
- `toFloat(i int) float`: Converts an integer to a float.
@@ -431,6 +439,7 @@ The following helper functions provided by Glance are available:
431439
- `sub(a, b float) float`: Subtracts two numbers.
432440
- `mul(a, b float) float`: Multiplies two numbers.
433441
- `div(a, b float) float`: Divides two numbers.
442+
- `mod(a, b int) int`: Remainder after dividing a by b (a % b).
434443
- `formatApproxNumber(n int) string`: Formats a number to be more human-readable, e.g. 1000 -> 1k.
435444
- `formatNumber(n float|int) string`: Formats a number with commas, e.g. 1000 -> 1,000.
436445
- `trimPrefix(prefix string, str string) string`: Trims the prefix from a string.

internal/glance/diagnose.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"time"
1313
)
1414

15-
const httpTestRequestTimeout = 10 * time.Second
15+
const httpTestRequestTimeout = 15 * time.Second
1616

1717
var diagnosticSteps = []diagnosticStep{
1818
{
@@ -75,7 +75,9 @@ var diagnosticSteps = []diagnosticStep{
7575
{
7676
name: "fetch data from Reddit API",
7777
fn: func() (string, error) {
78-
return testHttpRequest("GET", "https://www.reddit.com/search.json", 200)
78+
return testHttpRequestWithHeaders("GET", "https://www.reddit.com/search.json", map[string]string{
79+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:137.0) Gecko/20100101 Firefox/137.0",
80+
}, 200)
7981
},
8082
},
8183
{
@@ -165,7 +167,7 @@ func testHttpRequestWithHeaders(method, url string, headers map[string]string, e
165167
request.Header.Add(key, value)
166168
}
167169

168-
response, err := http.DefaultClient.Do(request)
170+
response, err := defaultHTTPClient.Do(request)
169171
if err != nil {
170172
return "", err
171173
}

internal/glance/static/js/page.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ function setupTruncatedElementTitles() {
661661

662662
for (let i = 0; i < elements.length; i++) {
663663
const element = elements[i];
664-
if (element.getAttribute("title") === null) element.title = element.textContent;
664+
if (element.getAttribute("title") === null) element.title = element.innerText;
665665
}
666666
}
667667

internal/glance/widget-custom-api.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ func (o *customAPIOptions) BoolOr(key string, defaultValue bool) bool {
108108
return customAPIGetOptionOrDefault(*o, key, defaultValue)
109109
}
110110

111+
func (o *customAPIOptions) JSON(key string) string {
112+
value, exists := (*o)[key]
113+
if !exists {
114+
panic(fmt.Sprintf("key %q does not exist in options", key))
115+
}
116+
117+
encoded, err := json.Marshal(value)
118+
if err != nil {
119+
panic(fmt.Sprintf("marshaling %s: %v", key, err))
120+
}
121+
122+
return string(encoded)
123+
}
124+
111125
func customAPIGetOptionOrDefault[T any](o customAPIOptions, key string, defaultValue T) T {
112126
if value, exists := o[key]; exists {
113127
if typedValue, ok := value.(T); ok {
@@ -479,6 +493,12 @@ var customAPITemplateFuncs = func() template.FuncMap {
479493
"div": func(a, b any) any {
480494
return doMathOpWithAny(a, b, "div")
481495
},
496+
"mod": func(a, b int) int {
497+
if b == 0 {
498+
return 0
499+
}
500+
return a % b
501+
},
482502
"now": func() time.Time {
483503
return time.Now()
484504
},

internal/glance/widget-utils.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const defaultClientTimeout = 5 * time.Second
2626
var defaultHTTPClient = &http.Client{
2727
Transport: &http.Transport{
2828
MaxIdleConnsPerHost: 10,
29+
Proxy: http.ProxyFromEnvironment,
2930
},
3031
Timeout: defaultClientTimeout,
3132
}
@@ -34,6 +35,7 @@ var defaultInsecureHTTPClient = &http.Client{
3435
Timeout: defaultClientTimeout,
3536
Transport: &http.Transport{
3637
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
38+
Proxy: http.ProxyFromEnvironment,
3739
},
3840
}
3941

0 commit comments

Comments
 (0)