~fmac/corylus

0a6019d236e40f5e872b340b4862175c1e542a74 — Francesco Camuffo 8 days ago 1558e20 main
Add seg cwd-count
4 files changed, 26 insertions(+), 9 deletions(-)

M README.md
M cwd.go
M main.go
M misc.go
M README.md => README.md +12 -7
@@ 4,22 4,26 @@ Simple shell prompt generator in Go.

## Usage

Corylus expects strings as arguments.  
If an argument matches the name of a known segment generator, then the result will be appended.  
Corylus expects strings as arguments.

If an argument matches the name of a known segment generator, then the result will be appended.

If an argument does not match any generator then it is appended as string.

---

Edit the prompt variable in your shell configuration file, probably `.bashrc`, `.zshrc` or `.profile`.  
Edit the prompt variable in your shell configuration file, probably `.bashrc`, `.zshrc` or `.profile`.

For ZSH you may need to enable the [prompt expansion](http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html) by adding `setopt PROMPT_SUBST` to your `.zshrc`.

---

At the moment the available segments are:
- hostname
- username
- cwd
- cwd-count
- hostname
- tail
- username

---



@@ 29,14 33,15 @@ Here is an example with some colors.
PS1='$(corylus \
	"[" hostname "] " \
	"\033[34m" username "\033[0m " \
	"\033[35m" cwd "\033[0m" \
	"\033[35m" cwd "\033[0m" " " \
	"[" cwd-count " files]" \
	tail)'
```

Which outputs with this format:

```
[myhost] myname ~/r/c/.g/branches
[maze] fmac ~/r/c/.git [13 files]
$
```
## Install

M cwd.go => cwd.go +13 -1
@@ 1,8 1,8 @@
package main

import (
	"fmt"
	"os"
	"os/user"
	"strings"
)



@@ 36,3 36,15 @@ func segCwd() (string, error) {
	}
	return wd, nil
}

func segCwdCount() (string, error) {
	wd, err := os.Getwd()
	if err != nil {
		return "", err
	}
	ff, err := os.ReadDir(wd)
	if err != nil {
		return "", err
	}
	return fmt.Sprint(len(ff)), nil
}

M main.go => main.go +1 -0
@@ 14,6 14,7 @@ func main() {

	generators := map[string]func() (string, error) {
		"cwd": segCwd,
		"cwd-count": segCwdCount,
		"hostname": segHostname,
		"tail": segTail,
		"username": segUsername,

M misc.go => misc.go +0 -1
@@ 3,7 3,6 @@ package main
import (
	"os"
	"os/user"
	"strings"
)

func segHostname() (string, error) {