Skip to content

Commit

Permalink
Fix trimming and add better auto-detection
Browse files Browse the repository at this point in the history
  • Loading branch information
zix99 committed Oct 27, 2019
1 parent e9903f9 commit d29fb7e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"rare/cmd"
"rare/pkg/color"
"rare/pkg/humanize"
"rare/pkg/multiterm"

"github.com/urfave/cli"
)
Expand Down Expand Up @@ -42,6 +43,10 @@ func main() {
Name: "color",
Usage: "Force-enable color output",
},
cli.BoolFlag{
Name: "notrim",
Usage: "By default, rare will trim output text for in-place updates. Setting this flag will disable that",
},
}

app.Commands = []cli.Command{
Expand All @@ -61,6 +66,9 @@ func main() {
if c.Bool("noformat") {
humanize.Enabled = false
}
if c.Bool("notrim") {
multiterm.AutoTrim = false
}
return nil
})

Expand Down
25 changes: 22 additions & 3 deletions pkg/multiterm/linetrim.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
)

var AutoTrim = true

const defaultRows, defaultCols = 24, 80

func getTermRowsCols() (int, int) {
Expand Down Expand Up @@ -36,16 +38,33 @@ func getTermRowsCols() (int, int) {
var computedRows, computedCols = 0, 0

func init() {
computedRows, computedCols = getTermRowsCols()
if _, ok := os.LookupEnv("TERM"); ok {
computedRows, computedCols = getTermRowsCols()
} else {
AutoTrim = false
}
}

func TermRows() int {
return computedRows
}

func TermCols() int {
return computedCols
}

func WriteLineNoWrap(out io.Writer, s string) {
if !AutoTrim {
out.Write([]byte(s))
return
}
runes := []rune(s)

visibleRunes := 0
i := 0
for i < len(runes) && visibleRunes < computedCols-2 {
if i == '\x1b' {
for i < len(runes) && visibleRunes < computedCols {
if runes[i] == '\x1b' {
// parse colors
for runes[i] != 'm' && i < len(runes)-1 {
i++
}
Expand Down

0 comments on commit d29fb7e

Please sign in to comment.