Skip to content

Commit

Permalink
Merge pull request #44 from james-antill/runewidth
Browse files Browse the repository at this point in the history
Runewidth and terminal width
  • Loading branch information
justjanne committed Oct 16, 2017
2 parents 4df5480 + 3dec992 commit 40d9ab9
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions powerline.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"strings"

"github.com/mattn/go-runewidth"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/text/width"
"os"
"strconv"
Expand Down Expand Up @@ -75,17 +77,36 @@ func (p *powerline) appendSegment(origin string, segment segment) {
p.Segments = append(p.Segments, segment)
}

func termWidth() int {
width, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil {
shellMaxLengthStr, found := os.LookupEnv("COLUMNS")
if !found {
return 80 // Otherwise 0 default.
}

shellMaxLength64, err := strconv.ParseInt(shellMaxLengthStr, 0, 64)
if err != nil {
return 80 // Otherwise 0 default.
}

width = int(shellMaxLength64)
}

return width
}

func (p *powerline) draw() string {
shellMaxLengthStr, _ := os.LookupEnv("COLUMNS")
shellMaxLength64, _ := strconv.ParseInt(shellMaxLengthStr, 0, 64)
shellMaxLength := int(shellMaxLength64)
shellMaxLength := termWidth()

shellMaxLength = shellMaxLength * *p.args.MaxWidthPercentage / 100

shellActualLength := 0
if shellMaxLength > 0 {
rlen := runewidth.StringWidth

for _, segment := range p.Segments {
shellActualLength += len(segment.content) + len(segment.separator)
shellActualLength += rlen(segment.content) + rlen(segment.separator)
}
for shellActualLength > shellMaxLength {
minPriority := MaxInteger
Expand All @@ -99,7 +120,7 @@ func (p *powerline) draw() string {
if minPrioritySegmentId != -1 {
segment := p.Segments[minPrioritySegmentId]
p.Segments = append(p.Segments[:minPrioritySegmentId], p.Segments[minPrioritySegmentId+1:]...)
shellActualLength -= len(segment.content) + len(segment.separator)
shellActualLength -= rlen(segment.content) + rlen(segment.separator)
}
}
}
Expand Down

0 comments on commit 40d9ab9

Please sign in to comment.