Skip to content

Commit

Permalink
Update to gitstatus@v0.4.1 (#4)
Browse files Browse the repository at this point in the history
* Moves functions to main.go

* format: add and use package

* ci: use latest Go minor
  • Loading branch information
arl committed Nov 6, 2019
1 parent ffafab1 commit 6f1a6ec
Show file tree
Hide file tree
Showing 11 changed files with 482 additions and 115 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
language: go

go:
- "1.13"
- tip
- "1.13.x"

# calls goreleaser
deploy:
Expand All @@ -11,5 +10,3 @@ deploy:
script: curl -sL https://git.io/goreleaser | bash
on:
tags: true
condition: $TRAVIS_OS_NAME = linux
condition: $TRAVIS_GO_VERSION = 1.13
73 changes: 0 additions & 73 deletions cli.go

This file was deleted.

8 changes: 0 additions & 8 deletions config.go

This file was deleted.

22 changes: 22 additions & 0 deletions format/json/formater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package json

import (
"encoding/json"
"fmt"
"io"

"github.com/arl/gitstatus"
)

// A Formater formats git status to JSON.
type Formater struct{}

// Format writes st as json into w.
func (Formater) Format(w io.Writer, st *gitstatus.Status) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(st); err != nil {
return fmt.Errorf("can't format status to json: %v", err)
}
return nil
}
193 changes: 193 additions & 0 deletions format/tmux/formater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package tmux

import (
"bytes"
"fmt"
"io"
"strings"

"github.com/arl/gitstatus"
)

const clear string = "#[fg=default]"

// Config is the configuration of the Git status tmux formatter.
type Config struct {
// Symbols contains the symbols printed before the Git status components.
Symbols symbols
// Styles contains the tmux style strings for symbols and Git status
// components.
Styles styles
}

type symbols struct {
Branch string // Branch is the string shown before local branch name.
HashPrefix string // HasPrefix is the string shown before a SHA1 ref.

Ahead string // Ahead is the string shown before the ahead count for the local/upstream branch divergence.
Behind string // Behind is the string shown before the behind count for the local/upstream branch divergence.

Staged string // Staged is the string shown before the count of staged files.
Conflict string // Conflict is the string shown before the count of files with conflicts.
Modified string // Modified is the string shown before the count of modified files.
Untracked string // Untracked is the string shown before the count of untracked files.
Stashed string // Stashed is the string shown before the count of stash entries.
Clean string // Clean is the string shown when the working tree is clean.
}

type styles struct {
State string // State is the style string printed before eventual special state.
Branch string // Branch is the style string printed before the local branch.
Remote string // Remote is the style string printed before the upstream branch.
Staged string // Staged is the style string printed before the staged files count.
Conflict string // Conflict is the style string printed before the conflict count.
Modified string // Modified is the style string printed before the modified files count.
Untracked string // Untracked is the style string printed before the untracked files count.
Stashed string // Stashed is the style string printed before the stash entries count.
Clean string // Clean is the style string printed before the clean symbols.
}

var DefaultCfg = Config{
Symbols: symbols{
Branch: "⎇ ",
Staged: "●",
Conflict: "✖ ",
Modified: "✚ ",
Untracked: "…",
Stashed: "⚑ ",
Clean: "✔",
Ahead: "↑·",
Behind: "↓·",
HashPrefix: ":",
},
Styles: styles{
State: "#[fg=red,bold]",
Branch: "#[fg=white,bold]",
Remote: "#[fg=cyan]",
Staged: "#[fg=green,bold]",
Conflict: "#[fg=red,bold]",
Modified: "#[fg=red,bold]",
Untracked: "#[fg=magenta,bold]",
Stashed: "#[fg=cyan,bold]",
Clean: "#[fg=green,bold]",
},
}

// A Formater formats git status to a tmux style string.
type Formater struct {
Config
b bytes.Buffer
st *gitstatus.Status
}

// Format writes st as json into w.
func (f *Formater) Format(w io.Writer, st *gitstatus.Status) error {
f.st = st
f.clear()

// overall working tree state
if f.st.IsInitial {
fmt.Fprintf(w, "%s%s [no commits yet]", f.Styles.Branch, f.st.LocalBranch)
goto fileCounts
}

f.specialState()
f.remote()

fileCounts:
f.flags()

_, err := f.b.WriteTo(w)
return err
}

func (f *Formater) specialState() {
f.clear()
switch f.st.State {
case gitstatus.Rebasing:
fmt.Fprintf(&f.b, "%s[rebase] ", f.Styles.State)
case gitstatus.AM:
fmt.Fprintf(&f.b, "%s[am] ", f.Styles.State)
case gitstatus.AMRebase:
fmt.Fprintf(&f.b, "%s[am-rebase] ", f.Styles.State)
case gitstatus.Merging:
fmt.Fprintf(&f.b, "%s[merge] ", f.Styles.State)
case gitstatus.CherryPicking:
fmt.Fprintf(&f.b, "%s[cherry-pick] ", f.Styles.State)
case gitstatus.Reverting:
fmt.Fprintf(&f.b, "%s[revert] ", f.Styles.State)
case gitstatus.Bisecting:
fmt.Fprintf(&f.b, "%s[bisect] ", f.Styles.State)
case gitstatus.Default:
fmt.Fprintf(&f.b, "%s%s", f.Styles.Branch, f.Symbols.Branch)
}
f.currentRef()
}

func (f *Formater) remote() {
f.clear()
if f.st.RemoteBranch != "" {
fmt.Fprintf(&f.b, "..%s%s", f.Styles.Remote, f.st.RemoteBranch)
f.divergence()
}
}

func (f *Formater) clear() {
// clear global style
f.b.WriteString(clear)
}

func (f *Formater) currentRef() {
f.clear()
if f.st.IsDetached {
fmt.Fprintf(&f.b, "%s%s", f.Symbols.HashPrefix, f.st.HEAD)
return
}

fmt.Fprintf(&f.b, "%s", f.st.LocalBranch)
}

func (f *Formater) divergence() {
f.clear()
pref := " "
if f.st.BehindCount != 0 {
fmt.Fprintf(&f.b, " %s%d", f.Symbols.Behind, f.st.BehindCount)
pref = ""
}
if f.st.AheadCount != 0 {
fmt.Fprintf(&f.b, "%s%s%d", pref, f.Symbols.Ahead, f.st.AheadCount)
}
}

func (f *Formater) flags() {
f.clear()
f.b.WriteString(" - ")
if f.st.IsClean {
fmt.Fprintf(&f.b, "%s%s", f.Styles.Clean, f.Symbols.Clean)
return
}

var flags []string

if f.st.NumStaged != 0 {
flags = append(flags,
fmt.Sprintf("%s%s%d", f.Styles.Staged, f.Symbols.Staged, f.st.NumStaged))
}
if f.st.NumConflicts != 0 {
flags = append(flags,
fmt.Sprintf("%s%s%d", f.Styles.Conflict, f.Symbols.Conflict, f.st.NumConflicts))
}
if f.st.NumModified != 0 {
flags = append(flags,
fmt.Sprintf("%s%s%d", f.Styles.Modified, f.Symbols.Modified, f.st.NumModified))
}
if f.st.NumStashed != 0 {
flags = append(flags,
fmt.Sprintf("%s%s%d", f.Styles.Stashed, f.Symbols.Stashed, f.st.NumStashed))
}
if f.st.NumUntracked != 0 {
flags = append(flags,
fmt.Sprintf("%s%s%d", f.Styles.Untracked, f.Symbols.Untracked, f.st.NumUntracked))
}
f.b.WriteString(strings.Join(flags, " "))
}
Loading

0 comments on commit 6f1a6ec

Please sign in to comment.