Skip to content

Commit

Permalink
Merge pull request #15 from NiloCK/versionChecks
Browse files Browse the repository at this point in the history
Version checks - wip
  • Loading branch information
NiloCK authored Feb 16, 2024
2 parents cb30aa5 + c06e133 commit 1570a60
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 197 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Go Test

on: [push, pull_request]

jobs:
test:
name: Run Go Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
with-tags: true
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.20'
- name: Test
run: go test ./...
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "./main.go"
}
]
}
38 changes: 36 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/nilock/tuido

go 1.16
go 1.20

require (
github.com/alecthomas/chroma/v2 v2.3.0
Expand All @@ -10,5 +10,39 @@ require (
github.com/go-git/go-git/v5 v5.11.0
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/muesli/termenv v0.15.1
github.com/nilock/walk-repo v0.1.0 // indirect
github.com/nilock/walk-repo v0.1.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
213 changes: 24 additions & 189 deletions go.sum

Large diffs are not rendered by default.

23 changes: 21 additions & 2 deletions tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
lg "github.com/charmbracelet/lipgloss"
"github.com/lucasb-eyer/go-colorful"
"github.com/nilock/tuido/tuido"
"github.com/nilock/tuido/utils"
walkrepo "github.com/nilock/walk-repo"
)

Expand Down Expand Up @@ -59,7 +60,10 @@ func Run() {

sortItems(items)

prog := tea.NewProgram(newTUI(items, runConfig), tea.WithAltScreen())
tui := newTUI(items, runConfig)
tui.houseKeeping()

prog := tea.NewProgram(tui, tea.WithAltScreen())

if err := prog.Start(); err != nil {
panic(err)
Expand All @@ -84,6 +88,7 @@ func newTUI(items []*tuido.Item, cfg config) tui {
return tui{
config: cfg,
err: nil,
notifs: []string{},
items: items,
renderSelection: nil,
itemsFilter: todo,
Expand All @@ -98,6 +103,18 @@ func newTUI(items []*tuido.Item, cfg config) tui {
}
}

func (t *tui) houseKeeping() {
local := utils.Version()
curent := utils.LatestVersion()

if local != curent {
t.notifs = append(t.notifs,
fmt.Sprintf("New version available: %s. Currently running %s. \nUpdates at %s",
curent, local, utils.ReleaseURL))
}

}

// populateTagColorStyles returns a coloring style for
// each #tag that exists in the list of items.
func populateTagColorStyles(items []*tuido.Item) map[string]lg.Style {
Expand Down Expand Up @@ -140,6 +157,8 @@ type tui struct {
config config
err error

notifs []string

items []*tuido.Item
itemsFilter itemType

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

// display all items in case of a trailing space in the filter
if (strings.HasSuffix(filter, " ")) {
if strings.HasSuffix(filter, " ") {
keywords = append(keywords, "")
}

Expand Down
18 changes: 14 additions & 4 deletions tui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ func (t tui) header() string {

tabs := lg.JoinHorizontal(lg.Bottom, todoTab, doneTab)
searchBox := tabGapStyle.Render(t.filter.View())
helpPrompt := tabGapStyle.Copy().Faint(true).Render("? - help")

helpPrompt := "? - help"
if len(t.notifs) > 0 {
helpPrompt += fmt.Sprintf(" (%d)", len(t.notifs))
}
helpPrompt = tabGapStyle.Copy().Faint(true).Render(helpPrompt)

gap := tabGapStyle.Render(strings.Repeat(" ", max(0, t.w-lg.Width(
lg.JoinHorizontal(lg.Bottom, tabs, searchBox, helpPrompt))-5),
))
Expand Down Expand Up @@ -85,8 +91,8 @@ func (t tui) footer() string {
right = footStyle.Copy().Faint(true).
Render("[enter] - Save Changes, [esc] - Discard Changes")
} else if t.mode == peek {
right = footStyle.Copy().Faint(true).Render("[esc] - Return to list view")
}
right = footStyle.Copy().Faint(true).Render("[esc] - Return to list view")
}
}

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

txt := lg.NewStyle().Width(28).Align(lg.Left).
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.")
return lg.JoinHorizontal(lg.Top, " ", controls, " ", txt)

notifications := strings.Join(t.notifs, "\n")
notifications = lg.NewStyle().Bold(true).Foreground(lg.Color("#ffbbaa")).Render(notifications)

return lg.JoinVertical(lg.Left, notifications, lg.JoinHorizontal(lg.Top, " ", controls, " ", txt))
case peek:
return t.peek.View(t.h, t.w, t.footer)
default:
Expand Down
63 changes: 63 additions & 0 deletions utils/versioning.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package utils

import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
)

const version = "v0.0.8"
const ReleaseURL = "https://github.com/NiloCK/tuido/releases/latest"

// Version returns the currently running version of the application.
func Version() string {
return version
}

func getLatestRedirectURL() (string, error) {

client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse // Stop after the first redirect
},
}

resp, err := client.Get(ReleaseURL)
if err != nil {
// If the error is due to stopping redirects, extract the Location header
var urlErr *url.Error
if errors.As(err, &urlErr) && urlErr.Err == http.ErrUseLastResponse {
if location, err := resp.Location(); err == nil {
return location.String(), nil
}
}
return "", err
}
defer resp.Body.Close()

loc, err := resp.Location()
if err != nil {
return loc.String(), nil
}

split := strings.Split(loc.String(), "/")
version := split[len(split)-1]
if version != "" {
return version, nil
}

return "", errors.New("no redirect")
}

func LatestVersion() string {
// lookup latest version from github
latest, err := getLatestRedirectURL()

if err != nil {
return fmt.Sprintf("Error getting latest version: %s", err)
}

return latest
}
42 changes: 42 additions & 0 deletions utils/versioning_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package utils_test

import (
"fmt"
"os"
"testing"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/nilock/tuido/utils"
)

func TestVersion(t *testing.T) {
// get latest tag

// print working directory
wd, err := os.Getwd()
if err != nil {
t.Fatalf("Error getting working directory: %s", err)
}

fmt.Println("PWD:", wd)

gitRepo, err := git.PlainOpen("..")
if err != nil {
t.Fatalf("Error opening git repo: %s", err)
}

tagRefs, err := gitRepo.Tags()
tags := []string{}

tagRefs.ForEach(func(tag *plumbing.Reference) error {
tags = append(tags, tag.Name().Short())
return nil
})

want := tags[len(tags)-1]

if got := utils.Version(); got != want {
t.Errorf("Version() = %v, want %v", got, want)
}
}

0 comments on commit 1570a60

Please sign in to comment.