feat(font): add official Cascadia Code release

resolves #4936
This commit is contained in:
Jan De Dobbeleer 2024-05-17 13:50:34 +02:00 committed by Jan De Dobbeleer
parent 6a84d275ac
commit 4e9f7f9060
2 changed files with 40 additions and 10 deletions

View file

@ -98,12 +98,13 @@ func (m *main) buildFontList(nerdFonts []*Asset) {
} }
func getFontsList() { func getFontsList() {
nerdFonts, err := Nerds() fonts, err := Fonts()
if err != nil { if err != nil {
program.Send(errMsg(err)) program.Send(errMsg(err))
return return
} }
program.Send(loadMsg(nerdFonts))
program.Send(loadMsg(fonts))
} }
func downloadFontZip(location string) { func downloadFontZip(location string) {

View file

@ -4,7 +4,9 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"net/http" "net/http"
"sort"
"strings" "strings"
"time" "time"
@ -23,30 +25,57 @@ type Asset struct {
func (a Asset) FilterValue() string { return a.Name } func (a Asset) FilterValue() string { return a.Name }
func Nerds() ([]*Asset, error) { func Fonts() ([]*Asset, error) {
ctx, cancelF := context.WithTimeout(context.Background(), time.Second*time.Duration(20)) assets, err := fetchFontAssets("ryanoasis/nerd-fonts")
defer cancelF()
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest", nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
cascadiaCode, err := fetchFontAssets("microsoft/cascadia-code")
if err != nil {
return assets, nil
}
assets = append(assets, cascadiaCode...)
sort.Slice(assets, func(i, j int) bool { return assets[i].Name < assets[j].Name })
return assets, nil
}
func CascadiaCode() ([]*Asset, error) {
return fetchFontAssets("microsoft/cascadia-code")
}
func fetchFontAssets(repo string) ([]*Asset, error) {
ctx, cancelF := context.WithTimeout(context.Background(), time.Second*time.Duration(20))
defer cancelF()
repoURL := "https://api.github.com/repos/" + repo + "/releases/latest"
req, err := http.NewRequestWithContext(ctx, "GET", repoURL, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/vnd.github.v3+json") req.Header.Add("Accept", "application/vnd.github.v3+json")
response, err := platform.Client.Do(req) response, err := platform.Client.Do(req)
if err != nil || response.StatusCode != http.StatusOK { if err != nil || response.StatusCode != http.StatusOK {
return nil, errors.New("failed to get nerd fonts release") return nil, fmt.Errorf("failed to get %s release", repo)
} }
defer response.Body.Close() defer response.Body.Close()
var release release var release release
err = json.NewDecoder(response.Body).Decode(&release) err = json.NewDecoder(response.Body).Decode(&release)
if err != nil { if err != nil {
return nil, errors.New("failed to parse nerd fonts release") return nil, errors.New("failed to parse nerd fonts release")
} }
var nerdFonts []*Asset
var fonts []*Asset
for _, asset := range release.Assets { for _, asset := range release.Assets {
if asset.State == "uploaded" && strings.HasSuffix(asset.Name, ".zip") { if asset.State == "uploaded" && strings.HasSuffix(asset.Name, ".zip") {
asset.Name = strings.TrimSuffix(asset.Name, ".zip") asset.Name = strings.TrimSuffix(asset.Name, ".zip")
nerdFonts = append(nerdFonts, asset) fonts = append(fonts, asset)
} }
} }
return nerdFonts, nil
return fonts, nil
} }