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

adding a width limiter #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions barely.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"sync"
"text/template"
"unicode"
)

var (
Expand All @@ -22,6 +23,8 @@ type StatusBar struct {
last string

status interface{}

width int
}

// NewStatusBar returns new StatusBar object, initialized with given template.
Expand All @@ -33,6 +36,12 @@ func NewStatusBar(format *template.Template) *StatusBar {
}
}

func (bar *StatusBar) SetWidth(w int) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add documentation for this method.

bar.Lock()
defer bar.Unlock()
bar.width = w
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, do not use one-symbol variable names.

}

// Lock locks StatusBar object if locker object was set with SetLock method
// to prevent multi-threading race conditions.
//
Expand Down Expand Up @@ -86,6 +95,11 @@ func (bar *StatusBar) Render(writer io.Writer) error {
)
}

if str := buffer.String(); bar.width > 0 && graphicLength(str) > bar.width {
buffer.Reset()
buffer.WriteString(trimTo(str, bar.width))
}

fmt.Fprintf(buffer, "\r")

bar.last = escapeSequenceRegexp.ReplaceAllLiteralString(
Expand All @@ -104,6 +118,26 @@ func (bar *StatusBar) Render(writer io.Writer) error {
return nil
}

func graphicLength(str string) int {
c := 0
for _, r := range str {
if unicode.IsGraphic(r) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that's right way to get proper display length for the status bar, because ultimately you will count two-character wide utf rune as one.

Also, it will miscount escape sequences that might present in the result string.

c++
}
}

return c
}

func trimTo(str string, l int) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that should be passed as user-defined callback, because it's too specific.

orig := []rune(str)
if len(orig) < 4 {
return str
}

return string(orig[:l-3]) + "…"
}

// Clear writes clear sequence in the specified writer, which is represented by
// whitespace sequence followed by "\r".
func (bar *StatusBar) Clear(writer io.Writer) {
Expand Down