diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..baf66c3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +watson-starship diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f92b001 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +tool = watson-starship + +all: buildlocal + +buildlocal: + CGO_LDFLAGS='-static' go build -tags osusergo,netgo -ldflags "-extldflags=-static -w" --trimpath -buildmode=pie -o $(tool) + strip --strip-all $(tool) + +install: buildlocal + install -m 755 $(tool) $(HOME)/bin/ + +clean: + rm -rf $(tool) diff --git a/README.md b/README.md new file mode 100644 index 0000000..1560d4e --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# watson-starship + +A simple plugin for [Starship](https://github.com/starship/starship), +which shows the elapsed time of the current project. + +## Setup + +You'll need the Golang toolchain for this (version 1.23+). + +To build: + +```shell +git clone https://github.com/TLINDEN/watson-starship.git +cd watson-starship +make +make install +``` + +Then add this to your `~/.config/starship.toml`: + +```toml +[custom.watson] +when = "watson status -e | grep -v 'No project'" +style = "green" +command = 'watson-starship' +format = '\[[$output]($style)\]' +``` + +And add `${custom.watson}\` to your global `format` setting. + +## Getting help + +Although I'm happy to hear from watson-starship users in private email, that's the +best way for me to forget to do something. + +In order to report a bug, unexpected behavior, feature requests or to +submit a patch, please open an issue on github: +https://github.com/TLINDEN/watson-starship/issues. + +## Copyright and license + +This software is licensed under the GNU GENERAL PUBLIC LICENSE version 3. + +## Authors + +T.v.Dein + +## Project homepage + +https://github.com/TLINDEN/watson-starship + +## Copyright and License + +Licensed under the GNU GENERAL PUBLIC LICENSE version 3. + +## Author + +T.v.Dein + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d574d2e --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module watson-starship + +go 1.23 diff --git a/main.go b/main.go new file mode 100644 index 0000000..abd57f9 --- /dev/null +++ b/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "time" + + "log" +) + +const statsfile string = ".config/watson/state" + +type Stats struct { + Project string `json:"project"` + Start int64 `json:"start"` +} + +func fileExists(filename string) bool { + info, err := os.Stat(filename) + + if err != nil { + return false + } + + return !info.IsDir() +} + +func main() { + statsfile := filepath.Join(os.Getenv("HOME"), statsfile) + + if !fileExists(statsfile) { + return + } + + data, err := os.ReadFile(statsfile) + if err != nil { + log.Fatalf("Could not read watson stats file: %w", err) + } + + var stats Stats + if err = json.Unmarshal(data, &stats); err != nil { + log.Fatalf("Could not unmarshal JSON: %w", err) + } + + start := time.Unix(stats.Start, 0) + elapsed := time.Since(start) + + fmt.Printf("%.02fh\n", elapsed.Hours()) +}