Skip to content

Commit

Permalink
Add eval and modules-right flags to support right-aligned prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
pdf authored and justjanne committed Jan 27, 2019
1 parent 20dac45 commit 62757e5
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 81 deletions.
46 changes: 26 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,61 +152,67 @@ Usage of powerline-go:
Colorize the hostname based on a hash of itself
-cwd-max-depth int
Maximum number of directories to show in path
(default 5)
(default 5)
-cwd-max-dir-size int
Maximum number of letters displayed for each directory in the path
(default -1)
(default -1)
-cwd-mode string
How to display the current directory
(valid choices: fancy, plain, dironly)
(default "fancy")
(valid choices: fancy, plain, dironly)
(default "fancy")
-duration string
The number of wall-clock seconds elapsed for the previous command. May be an integer (whole seconds) or floating point (seconds and fractions of a second).
The elapsed clock-time of the previous command
-east-asian-width
Use East Asian Ambiguous Widths
-error int
Exit code of previously executed command
-eval
Output prompt in 'eval' format.
-ignore-repos string
A list of git repos to ignore. Separate with ','.
Repos are identified by their root directory.
Repos are identified by their root directory.
-max-width int
Maximum width of the shell that the prompt may use, in percent. Setting this to 0 disables the shrinking subsystem.
-mode string
The characters used to make separators between segments.
(valid choices: patched, compatible, flat)
(default "patched")
(valid choices: patched, compatible, flat)
(default "patched")
-modules string
The list of modules to load, separated by ','
(valid choices: aws, cwd, docker, dotenv, duration, exit, git, gitlite, hg, host, jobs, load, nix-shell, perlbrew, perms, root, shell-var, ssh, termtitle, time, user, venv, node)
(default "venv,user,host,ssh,cwd,perms,git,hg,jobs,exit,root")
(valid choices: aws, cwd, docker, dotenv, duration, exit, git, gitlite, hg, host, jobs, kube, load, newline, perlbrew, perms, root, shell-var, ssh, termtitle, terraform-workspace, time, node, user, venv, vgo, nix-shell)
(default "nix-shell,venv,user,host,ssh,cwd,perms,git,hg,jobs,exit,root,vgo")
-modules-right string
The list of modules to load anchored to the right, for shells that support it, separated by ','
(valid choices: aws, cwd, docker, dotenv, duration, exit, git, gitlite, hg, host, jobs, kube, load, newline, perlbrew, perms, root, shell-var, ssh, termtitle, terraform-workspace, time, node, user, venv, vgo, nix-shell)
-newline
Show the prompt on a new line
-numeric-exit-codes
Shows numeric exit codes for errors.
-path-aliases string
One or more aliases from a path to a short name. Separate with ','.
An alias maps a path like foo/bar/baz to a short name like FBB.
Specify these as key/value pairs like foo/bar/baz=FBB.
Use '~' for your home dir. You may need to escape this character to avoid shell substitution.
An alias maps a path like foo/bar/baz to a short name like FBB.
Specify these as key/value pairs like foo/bar/baz=FBB.
Use '~' for your home dir. You may need to escape this character to avoid shell substitution.
-priority string
Segments sorted by priority, if not enough space exists, the least priorized segments are removed first. Separate with ','
(valid choices: aws, cwd, cwd-path, docker, duration, exit, git-branch, git-status, hg, host, jobs, load, nix-shell, perlbrew, perms, root, ssh, time, user, venv, node)
(default "root,cwd,user,host,ssh,perms,git-branch,git-status,hg,jobs,exit,cwd-path")
(valid choices: aws, cwd, docker, dotenv, duration, exit, git, gitlite, hg, host, jobs, kube, load, newline, perlbrew, perms, root, shell-var, ssh, termtitle, terraform-workspace, time, node, user, venv, vgo, nix-shell)
(default "root,cwd,user,host,ssh,perms,git-branch,git-status,hg,jobs,exit,cwd-path")
-shell string
Set this to your shell type
(valid choices: bare, bash, zsh)
(default "bash")
(valid choices: bare, bash, zsh)
(default "bash")
-shell-var string
A shell variable to add to the segments.
-shorten-gke-names
Shortens names for GKE Kube clusters.
-theme string
Set this to the theme you want to use
(valid choices: default, low-contrast)
(default "default")
(valid choices: default, low-contrast)
(default "default")
-truncate-segment-width int
Minimum width of a segment, segments longer than this will be shortened if space is limited. Setting this to 0 disables it.
(default 16)
(default 16)
```

### Path Aliases
Expand Down
36 changes: 23 additions & 13 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

var symbolTemplates = map[string]Symbols{
"compatible": {
Lock: "RO",
Network: "SSH",
Separator: "\u25B6",
SeparatorThin: "\u276F",
Lock: "RO",
Network: "SSH",
Separator: "\u25B6",
SeparatorThin: "\u276F",
SeparatorReverse: "\u25C0",
SeparatorReverseThin: "\u276E",

RepoDetached: "\u2693",
RepoAhead: "\u2B06",
Expand All @@ -17,10 +19,12 @@ var symbolTemplates = map[string]Symbols{
RepoStashed: "\u2691",
},
"patched": {
Lock: "\uE0A2",
Network: "\uE0A2",
Separator: "\uE0B0",
SeparatorThin: "\uE0B1",
Lock: "\uE0A2",
Network: "\uE0A2",
Separator: "\uE0B0",
SeparatorThin: "\uE0B1",
SeparatorReverse: "\uE0B2",
SeparatorReverseThin: "\uE0B3",

RepoDetached: "\u2693",
RepoAhead: "\u2B06",
Expand Down Expand Up @@ -50,13 +54,19 @@ var shellInfos = map[string]ShellInfo{
escapedBackslash: `\\\\`,
escapedBacktick: "\\`",
escapedDollar: `\$`,
evalPromptPrefix: `PS1="`,
evalPromptSuffix: `"`,
},
"zsh": {
colorTemplate: "%%{\u001b%s%%}",
rootIndicator: "%#",
escapedBackslash: `\\`,
escapedBacktick: "\\`",
escapedDollar: `\$`,
colorTemplate: "%%{\u001b%s%%}",
rootIndicator: "%#",
escapedBackslash: `\\`,
escapedBacktick: "\\`",
escapedDollar: `\$`,
evalPromptPrefix: `PROMPT="`,
evalPromptSuffix: `"`,
evalPromptRightPrefix: `RPROMPT="`,
evalPromptRightSuffix: `"`,
},
"bare": {
colorTemplate: "%s",
Expand Down
36 changes: 24 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ import (
runewidth "github.com/mattn/go-runewidth"
)

type alignment int

const (
MinUnsignedInteger uint = 0
MaxUnsignedInteger = ^MinUnsignedInteger
MaxInteger int = int(MaxUnsignedInteger >> 1)
MinInteger = ^MaxInteger

alignLeft alignment = iota
alignRight
)

type segment struct {
Expand All @@ -40,6 +45,7 @@ type args struct {
Theme *string
Shell *string
Modules *string
ModulesRight *string
Priority *string
MaxWidthPercentage *int
TruncateSegmentWidth *int
Expand All @@ -50,6 +56,7 @@ type args struct {
ShellVar *string
PathAliases *string
Duration *string
Eval *bool
}

func (s segment) computeWidth() int {
Expand Down Expand Up @@ -174,12 +181,17 @@ func main() {
"modules",
"nix-shell,venv,user,host,ssh,cwd,perms,git,hg,jobs,exit,root,vgo",
commentsWithDefaults("The list of modules to load, separated by ','",
"(valid choices: aws, cwd, docker, dotenv, exit, git, gitlite, hg, host, jobs, load, nix-shell, perlbrew, perms, root, shell-var, ssh, svn, termtitle, time, user, venv, vgo)")),
"(valid choices: aws, cwd, docker, dotenv, duration, exit, git, gitlite, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, root, shell-var, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo)")),
ModulesRight: flag.String(
"modules-right",
"",
comments("The list of modules to load anchored to the right, for shells that support it, separated by ','",
"(valid choices: aws, cwd, docker, dotenv, duration, exit, git, gitlite, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, root, shell-var, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo)")),
Priority: flag.String(
"priority",
"root,cwd,user,host,ssh,perms,git-branch,git-status,hg,jobs,exit,cwd-path",
commentsWithDefaults("Segments sorted by priority, if not enough space exists, the least priorized segments are removed first. Separate with ','",
"(valid choices: aws, cwd, cwd-path, docker, exit, git-branch, git-status, hg, host, jobs, load, nix-shell, perlbrew, perms, root, ssh, svn, time, user, venv, vgo)")),
"(valid choices: aws, cwd, docker, dotenv, duration, exit, git, gitlite, hg, host, jobs, kube, load, newline, perlbrew, perms, root, shell-var, ssh, termtitle, terraform-workspace, time, node, user, venv, vgo, nix-shell)")),
MaxWidthPercentage: flag.Int(
"max-width",
0,
Expand Down Expand Up @@ -220,6 +232,10 @@ func main() {
"duration",
"",
comments("The elapsed clock-time of the previous command")),
Eval: flag.Bool(
"eval",
false,
comments("Output prompt in 'eval' format.")),
}
flag.Parse()
if strings.HasSuffix(*args.Theme, ".json") {
Expand All @@ -238,15 +254,11 @@ func main() {
priorities[priority] = len(priorityList) - idx
}

powerline := NewPowerline(args, getValidCwd(), priorities)

for _, module := range strings.Split(*powerline.args.Modules, ",") {
elem, ok := modules[module]
if ok {
elem(powerline)
} else {
println("Module not found: " + module)
}
p := newPowerline(args, getValidCwd(), priorities, alignLeft)
if p.supportsRightModules() && p.hasRightModules() && !*args.Eval {
fmt.Fprint(os.Stderr, "Flag '-modules-right' requires '-eval' mode.")
os.Exit(1)
}
fmt.Print(powerline.draw())

fmt.Print(p.draw())
}
Loading

0 comments on commit 62757e5

Please sign in to comment.