Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Scheuermann committed Feb 28, 2022
2 parents c823117 + fe1d81f commit fe23edb
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 9 deletions.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ The goal of GoPS to offer a fast powerline-like prompt.
## Installation
There are two approaches of getting GoPS: Using a release or building it from source.

GoPS relies on modded fonts (namely powerline-fonts) to display some of the symbols. Make sure to install it and activate it for your terminal: <https://github.com/powerline/fonts#installation>
GoPS relies on modded fonts (namely powerline-fonts) to display some of the symbols. Make sure to install them and activate one of them in your terminal: <https://github.com/powerline/fonts#installation>

### Using a release
Click on ["release"](https://github.com/noxer/gops/releases) and download the newest binary for your OS. Place it in a folder which is in `$PATH`.
- Click on ["release"](https://github.com/noxer/gops/releases) and download the newest binary for your OS.
- Rename it to `gops`.
- Place it in a folder which is in `$PATH`.

### Building from source
GoPS relies on modded fonts (namely powerline-fonts) to display some of the symbols. Make sure to install it and activate it for your terminal: <https://github.com/powerline/fonts#installation>

Make sure the current Go version is installed. If not, refer to this guide: <https://golang.org/doc/install#install>

Also make sure your `$GOPATH/bin` directory is in the `$PATH`.
Expand Down Expand Up @@ -59,11 +59,24 @@ To apply the change, you need to either close your shell and open it again or so
source ~/.zshrc
```

## Currently available segments
Currently GoPS comes with a sall selection of segments for your prompt, which can be found in segments/

| Segment | Displayed Information |
| ---------- | ---------- |
| git | current git branch |
| dir | path of the current dir (can be shortened) |
| host | hostname (FQDN) (can be shortened) |
| nodejs | version of the currently installed node.js |
| user | name of the current user |
| userathost | name of the current user and the hostname combined as user@host (can be shortened) |
| virtualenv | name of the currently active python viratualenv |

## FAQ
(Questions that have never been asked but I think the answers may help you)

### Where do it put my config file? How do I configure GoPS?
You don't. Right now GoPS does not support any config files or command line parameters. Instead you can edit `main.go` or the segments themselfs to adapt the prompt to your needs.
You don't. Right now GoPS does not support any config files or command line parameters. Instead you can edit `main.go` or the segments themselves to adapt the prompt to your needs.

### I've successfully built and activated GoPS but it prints strange characters in the prompt. How do I fix that?
You've either not installed `powerline-fonts` or it is not configured to be used in your terminal. Make sure you've installed it from [here](https://github.com/powerline/fonts#installation) and you've activated one of the fonts. The font used for the screenshot is `Source Code Pro for Powerline`.
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/noxer/gops

go 1.13
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func ps1(shell string) {
var segs []separator.Segment
segs = common.AddUserAtHost(segs, true)
segs = common.AddDir(segs, true)
segs = common.AddNodeVersion(segs)
segs = git.Add(segs)
segs = append(segs, end)

Expand Down
31 changes: 31 additions & 0 deletions segments/common/nodejs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package common

import (
"os/exec"
"strings"

"github.com/noxer/gops/color"
"github.com/noxer/gops/separator"
)

// AddNodeVersion adds the version of the currently used node.js installation the prompt
func AddNodeVersion(segs []separator.Segment) []separator.Segment {

out, err := exec.Command("node", "-v").Output()

version := strings.TrimSpace(string(out))

if err != nil {
// did not work? just place a ?
version = " ? "
}

// create the segment
s := separator.Segment{
Text: " node: " + version + " ",
Foreground: color.White,
Background: color.Blue,
}

return append(segs, s)
}
55 changes: 51 additions & 4 deletions segments/git/git.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package git

import (
"bufio"
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/noxer/gops/color"
"github.com/noxer/gops/separator"
Expand Down Expand Up @@ -58,14 +60,59 @@ func findGitRepo() string {
}

func readHEAD(repoDir string) string {
headName := filepath.Join(repoDir, "HEAD")

repoDir = filepath.Join(repoDir, "HEAD")

p, err := ioutil.ReadFile(repoDir)
p, err := ioutil.ReadFile(headName)
if err != nil {
return "?"
}

return string(bytes.TrimPrefix(bytes.TrimSpace(p), []byte("ref: refs/heads/")))
head := string(bytes.TrimSpace(p))

if strings.HasPrefix(head, "ref: refs/heads/") {
return head[16:]
}

// this seems to be just a hash. Try to find it in the packed refs
pr := parsePackedRefs(repoDir)
if pr == nil {
return head
}

tag, ok := pr[head]
if !ok {
return head
}

return strings.TrimPrefix(tag, "refs/tags/")
}

func parsePackedRefs(repoDir string) map[string]string {
prName := filepath.Join(repoDir, "packed-refs")
f, err := os.Open(prName)
if err != nil {
return nil
}
defer f.Close()

s := bufio.NewScanner(f)

m := make(map[string]string)
for s.Scan() {
line := strings.TrimSpace(s.Text())
// strings.Cut is part of Go 1.18 and not yet released
// hash, ref, ok := strings.Cut(line, " ")
// if !ok {
// continue
// }
parts := strings.SplitN(line, " ", 2)
if len(parts) != 2 {
continue
}
hash, ref := parts[0], parts[1]

m[hash] = ref
}

return m
}

0 comments on commit fe23edb

Please sign in to comment.