Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic right modules support. #369

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,6 @@ func main() {
}

p := newPowerline(cfg, getValidCwd(), alignLeft)
if p.supportsRightModules() && p.hasRightModules() && !cfg.Eval {
panic("Flag '-modules-right' requires '-eval' mode.")
}

fmt.Print(p.draw())
}
83 changes: 80 additions & 3 deletions powerline.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/user"
"path"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -394,6 +395,65 @@ func (p *powerline) drawRow(rowNum int, buffer *bytes.Buffer) {
}
}

func (p *powerline) actualPromptLength(prompt string) int {
stripped := prompt

type replacements struct {
name string
value string
}

for rowNum := range p.Segments {
row := p.Segments[rowNum]

// Some segments contain control characters for the shell that mess up
// the length calculation unless we replace them with something appropriate.
replaces := []replacements{
{"termtitle", ""},
{"user", p.username},
{"host", p.hostname},
}

for _, replacement := range replaces {
for _, segment := range row {
if segment.Name == replacement.name {
stripped = strings.ReplaceAll(stripped, segment.Content, replacement.value)
}
}
}
}

removeControlSequences := regexp.MustCompile(`\\\[[^]]*\]`)
stripped = removeControlSequences.ReplaceAllString(stripped, "")
s := []rune(stripped)
return len(s)
}

func (p *powerline) paddedRightPrompt(left string) string {
if !p.hasRightModules() {
return ""
}

var right = p.rightPowerline.draw()
if p.cfg.PromptOnNewLine {
var skipNewlinePrompt = regexp.MustCompile(`\n.*$`)
right = skipNewlinePrompt.ReplaceAllString(right, "")
}

if p.hasTerminalWidth() {
width := termWidth()
// Calculate padding for right aligned prompt (accounting for unicode characters).
pad := width - p.actualPromptLength(left) - p.rightPowerline.actualPromptLength(right)
if p.cfg.Eval {
pad = pad + len(p.shell.EvalPromptPrefix)
}
// Produce padded prompt.
padded := fmt.Sprintf("%*s%s", pad, "", right)
return padded
}
return right
}

func (p *powerline) draw() string {

var buffer bytes.Buffer
Expand All @@ -414,6 +474,10 @@ func (p *powerline) draw() string {
}
}

if !p.supportsNativeRightModules() {
buffer.WriteString(p.paddedRightPrompt(buffer.String()))
}

if p.cfg.PromptOnNewLine {
buffer.WriteRune('\n')

Expand Down Expand Up @@ -447,12 +511,12 @@ func (p *powerline) draw() string {
}
}
case alignRight:
if p.supportsRightModules() {
if p.supportsNativeRightModules() {
buffer.Truncate(buffer.Len() - 1)
buffer.WriteString(p.shell.EvalPromptRightSuffix)
}
}
if p.hasRightModules() {
if p.supportsNativeRightModules() && p.hasRightModules() {
buffer.WriteString(p.rightPowerline.draw())
}
}
Expand All @@ -465,9 +529,22 @@ func (p *powerline) hasRightModules() bool {
}

func (p *powerline) supportsRightModules() bool {
return p.shell.EvalPromptRightPrefix != "" || p.shell.EvalPromptRightSuffix != ""
res := p.supportsNativeRightModules() || p.hasTerminalWidth()
return res
}

func (p *powerline) supportsNativeRightModules() bool {
if p.cfg.Eval {
return p.shell.EvalPromptRightPrefix != "" || p.shell.EvalPromptRightSuffix != ""
} else {
return false
}
}

func (p *powerline) isRightPrompt() bool {
return p.align == alignRight && p.supportsRightModules()
}

func (p *powerline) hasTerminalWidth() bool {
return termWidth() > 0
}