Skip to content

Commit 1570a60

Browse files
authored
Merge pull request #15 from NiloCK/versionChecks
Version checks - wip
2 parents cb30aa5 + c06e133 commit 1570a60

File tree

8 files changed

+234
-197
lines changed

8 files changed

+234
-197
lines changed

.github/workflows/test.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Go Test
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
name: Run Go Tests
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
with:
12+
fetch-depth: 0
13+
with-tags: true
14+
- name: Set up Go
15+
uses: actions/setup-go@v2
16+
with:
17+
go-version: '1.20'
18+
- name: Test
19+
run: go test ./...

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch Package",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "auto",
12+
"program": "./main.go"
13+
}
14+
]
15+
}

go.mod

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/nilock/tuido
22

3-
go 1.16
3+
go 1.20
44

55
require (
66
github.com/alecthomas/chroma/v2 v2.3.0
@@ -10,5 +10,39 @@ require (
1010
github.com/go-git/go-git/v5 v5.11.0
1111
github.com/lucasb-eyer/go-colorful v1.2.0
1212
github.com/muesli/termenv v0.15.1
13-
github.com/nilock/walk-repo v0.1.0 // indirect
13+
github.com/nilock/walk-repo v0.1.0
14+
)
15+
16+
require (
17+
dario.cat/mergo v1.0.0 // indirect
18+
github.com/Microsoft/go-winio v0.6.1 // indirect
19+
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
20+
github.com/atotto/clipboard v0.1.4 // indirect
21+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
22+
github.com/cloudflare/circl v1.3.3 // indirect
23+
github.com/containerd/console v1.0.3 // indirect
24+
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
25+
github.com/dlclark/regexp2 v1.4.0 // indirect
26+
github.com/emirpasic/gods v1.18.1 // indirect
27+
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
28+
github.com/go-git/go-billy/v5 v5.5.0 // indirect
29+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
30+
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
31+
github.com/kevinburke/ssh_config v1.2.0 // indirect
32+
github.com/mattn/go-isatty v0.0.17 // indirect
33+
github.com/mattn/go-runewidth v0.0.14 // indirect
34+
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
35+
github.com/muesli/reflow v0.3.0 // indirect
36+
github.com/pjbgf/sha1cd v0.3.0 // indirect
37+
github.com/rivo/uniseg v0.2.0 // indirect
38+
github.com/sergi/go-diff v1.1.0 // indirect
39+
github.com/skeema/knownhosts v1.2.1 // indirect
40+
github.com/xanzy/ssh-agent v0.3.3 // indirect
41+
golang.org/x/crypto v0.16.0 // indirect
42+
golang.org/x/mod v0.12.0 // indirect
43+
golang.org/x/net v0.19.0 // indirect
44+
golang.org/x/sys v0.15.0 // indirect
45+
golang.org/x/term v0.15.0 // indirect
46+
golang.org/x/tools v0.13.0 // indirect
47+
gopkg.in/warnings.v0 v0.1.2 // indirect
1448
)

go.sum

Lines changed: 24 additions & 189 deletions
Large diffs are not rendered by default.

tui/tui.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
lg "github.com/charmbracelet/lipgloss"
1818
"github.com/lucasb-eyer/go-colorful"
1919
"github.com/nilock/tuido/tuido"
20+
"github.com/nilock/tuido/utils"
2021
walkrepo "github.com/nilock/walk-repo"
2122
)
2223

@@ -59,7 +60,10 @@ func Run() {
5960

6061
sortItems(items)
6162

62-
prog := tea.NewProgram(newTUI(items, runConfig), tea.WithAltScreen())
63+
tui := newTUI(items, runConfig)
64+
tui.houseKeeping()
65+
66+
prog := tea.NewProgram(tui, tea.WithAltScreen())
6367

6468
if err := prog.Start(); err != nil {
6569
panic(err)
@@ -84,6 +88,7 @@ func newTUI(items []*tuido.Item, cfg config) tui {
8488
return tui{
8589
config: cfg,
8690
err: nil,
91+
notifs: []string{},
8792
items: items,
8893
renderSelection: nil,
8994
itemsFilter: todo,
@@ -98,6 +103,18 @@ func newTUI(items []*tuido.Item, cfg config) tui {
98103
}
99104
}
100105

106+
func (t *tui) houseKeeping() {
107+
local := utils.Version()
108+
curent := utils.LatestVersion()
109+
110+
if local != curent {
111+
t.notifs = append(t.notifs,
112+
fmt.Sprintf("New version available: %s. Currently running %s. \nUpdates at %s",
113+
curent, local, utils.ReleaseURL))
114+
}
115+
116+
}
117+
101118
// populateTagColorStyles returns a coloring style for
102119
// each #tag that exists in the list of items.
103120
func populateTagColorStyles(items []*tuido.Item) map[string]lg.Style {
@@ -140,6 +157,8 @@ type tui struct {
140157
config config
141158
err error
142159

160+
notifs []string
161+
143162
items []*tuido.Item
144163
itemsFilter itemType
145164

@@ -292,7 +311,7 @@ func (t *tui) applyFilter() {
292311
keywords := strings.Fields(filter)
293312

294313
// display all items in case of a trailing space in the filter
295-
if (strings.HasSuffix(filter, " ")) {
314+
if strings.HasSuffix(filter, " ") {
296315
keywords = append(keywords, "")
297316
}
298317

tui/view.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ func (t tui) header() string {
5555

5656
tabs := lg.JoinHorizontal(lg.Bottom, todoTab, doneTab)
5757
searchBox := tabGapStyle.Render(t.filter.View())
58-
helpPrompt := tabGapStyle.Copy().Faint(true).Render("? - help")
58+
59+
helpPrompt := "? - help"
60+
if len(t.notifs) > 0 {
61+
helpPrompt += fmt.Sprintf(" (%d)", len(t.notifs))
62+
}
63+
helpPrompt = tabGapStyle.Copy().Faint(true).Render(helpPrompt)
64+
5965
gap := tabGapStyle.Render(strings.Repeat(" ", max(0, t.w-lg.Width(
6066
lg.JoinHorizontal(lg.Bottom, tabs, searchBox, helpPrompt))-5),
6167
))
@@ -85,8 +91,8 @@ func (t tui) footer() string {
8591
right = footStyle.Copy().Faint(true).
8692
Render("[enter] - Save Changes, [esc] - Discard Changes")
8793
} else if t.mode == peek {
88-
right = footStyle.Copy().Faint(true).Render("[esc] - Return to list view")
89-
}
94+
right = footStyle.Copy().Faint(true).Render("[esc] - Return to list view")
95+
}
9096
}
9197

9298
spacerWidth := max(0, t.w-lg.Width(lg.JoinHorizontal(lg.Bottom, itemStr, right))-5)
@@ -150,7 +156,11 @@ func (t tui) View() string {
150156

151157
txt := lg.NewStyle().Width(28).Align(lg.Left).
152158
Render("\n\n\ntuido reads txt, md, and xit files from the working directory and locates xit style todo items, allowing for quick navigation and discovery.\n\nUpdating an item's status in tuido writes the corresponding change to disk.")
153-
return lg.JoinHorizontal(lg.Top, " ", controls, " ", txt)
159+
160+
notifications := strings.Join(t.notifs, "\n")
161+
notifications = lg.NewStyle().Bold(true).Foreground(lg.Color("#ffbbaa")).Render(notifications)
162+
163+
return lg.JoinVertical(lg.Left, notifications, lg.JoinHorizontal(lg.Top, " ", controls, " ", txt))
154164
case peek:
155165
return t.peek.View(t.h, t.w, t.footer)
156166
default:

utils/versioning.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package utils
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"strings"
9+
)
10+
11+
const version = "v0.0.8"
12+
const ReleaseURL = "https://github.com/NiloCK/tuido/releases/latest"
13+
14+
// Version returns the currently running version of the application.
15+
func Version() string {
16+
return version
17+
}
18+
19+
func getLatestRedirectURL() (string, error) {
20+
21+
client := &http.Client{
22+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
23+
return http.ErrUseLastResponse // Stop after the first redirect
24+
},
25+
}
26+
27+
resp, err := client.Get(ReleaseURL)
28+
if err != nil {
29+
// If the error is due to stopping redirects, extract the Location header
30+
var urlErr *url.Error
31+
if errors.As(err, &urlErr) && urlErr.Err == http.ErrUseLastResponse {
32+
if location, err := resp.Location(); err == nil {
33+
return location.String(), nil
34+
}
35+
}
36+
return "", err
37+
}
38+
defer resp.Body.Close()
39+
40+
loc, err := resp.Location()
41+
if err != nil {
42+
return loc.String(), nil
43+
}
44+
45+
split := strings.Split(loc.String(), "/")
46+
version := split[len(split)-1]
47+
if version != "" {
48+
return version, nil
49+
}
50+
51+
return "", errors.New("no redirect")
52+
}
53+
54+
func LatestVersion() string {
55+
// lookup latest version from github
56+
latest, err := getLatestRedirectURL()
57+
58+
if err != nil {
59+
return fmt.Sprintf("Error getting latest version: %s", err)
60+
}
61+
62+
return latest
63+
}

utils/versioning_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package utils_test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/go-git/go-git/v5"
9+
"github.com/go-git/go-git/v5/plumbing"
10+
"github.com/nilock/tuido/utils"
11+
)
12+
13+
func TestVersion(t *testing.T) {
14+
// get latest tag
15+
16+
// print working directory
17+
wd, err := os.Getwd()
18+
if err != nil {
19+
t.Fatalf("Error getting working directory: %s", err)
20+
}
21+
22+
fmt.Println("PWD:", wd)
23+
24+
gitRepo, err := git.PlainOpen("..")
25+
if err != nil {
26+
t.Fatalf("Error opening git repo: %s", err)
27+
}
28+
29+
tagRefs, err := gitRepo.Tags()
30+
tags := []string{}
31+
32+
tagRefs.ForEach(func(tag *plumbing.Reference) error {
33+
tags = append(tags, tag.Name().Short())
34+
return nil
35+
})
36+
37+
want := tags[len(tags)-1]
38+
39+
if got := utils.Version(); got != want {
40+
t.Errorf("Version() = %v, want %v", got, want)
41+
}
42+
}

0 commit comments

Comments
 (0)