-
Notifications
You must be signed in to change notification settings - Fork 10
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"strings" | ||
"sync" | ||
"text/template" | ||
"unicode" | ||
) | ||
|
||
var ( | ||
|
@@ -22,6 +23,8 @@ type StatusBar struct { | |
last string | ||
|
||
status interface{} | ||
|
||
width int | ||
} | ||
|
||
// NewStatusBar returns new StatusBar object, initialized with given template. | ||
|
@@ -33,6 +36,12 @@ func NewStatusBar(format *template.Template) *StatusBar { | |
} | ||
} | ||
|
||
func (bar *StatusBar) SetWidth(w int) { | ||
bar.Lock() | ||
defer bar.Unlock() | ||
bar.width = w | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
// | ||
|
@@ -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( | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
There was a problem hiding this comment.
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.