refactor: remove unused function

This commit is contained in:
Jan De Dobbeleer 2023-01-04 20:52:26 +01:00 committed by Jan De Dobbeleer
parent 48d8a522bf
commit 59ddfc2ba1
3 changed files with 18 additions and 106 deletions

View file

@ -9,6 +9,10 @@ import (
"github.com/mattn/go-runewidth"
)
func init() { //nolint:gochecknoinits
runewidth.DefaultCondition.EastAsianWidth = false
}
var (
knownStyles = []*style{
{AnchorStart: `<b>`, AnchorEnd: `</b>`, Start: "\x1b[1m", End: "\x1b[22m"},
@ -309,6 +313,15 @@ func (w *Writer) Write(background, foreground, text string) {
w.currentForeground = ""
}
func (w *Writer) String() (string, int) {
defer func() {
w.length = 0
w.builder.Reset()
}()
return w.builder.String(), w.length
}
func (w *Writer) printEscapedAnsiString(text string) {
if w.Plain {
return
@ -491,11 +504,9 @@ func (w *Writer) expandKeyword(keyword string) string {
return keyword
}
func (w *Writer) String() (string, int) {
defer func() {
w.length = 0
w.builder.Reset()
}()
return w.builder.String(), w.length
func (w *Writer) trimAnsi(text string) string {
if len(text) == 0 || !strings.Contains(text, "\x1b") {
return text
}
return regex.ReplaceAllString(AnsiRegex, text, "")
}

View file

@ -1,48 +0,0 @@
package ansi
import (
"strings"
"github.com/jandedobbeleer/oh-my-posh/regex"
"github.com/mattn/go-runewidth"
)
func init() { //nolint:gochecknoinits
runewidth.DefaultCondition.EastAsianWidth = false
}
func (a *Writer) MeasureText(text string) int {
// skip strings with ANSI
if !strings.Contains(text, "\x1b") {
text = a.TrimEscapeSequences(text)
length := runewidth.StringWidth(text)
return length
}
if strings.Contains(text, "\x1b]8;;") {
matches := regex.FindAllNamedRegexMatch(a.hyperlinkRegex, text)
for _, match := range matches {
text = strings.ReplaceAll(text, match["STR"], match["TEXT"])
}
}
text = a.trimAnsi(text)
text = a.TrimEscapeSequences(text)
length := runewidth.StringWidth(text)
return length
}
func (a *Writer) trimAnsi(text string) string {
if len(text) == 0 || !strings.Contains(text, "\x1b") {
return text
}
return regex.ReplaceAllString(AnsiRegex, text, "")
}
func (a *Writer) TrimEscapeSequences(text string) string {
if len(text) == 0 {
return text
}
text = strings.ReplaceAll(text, a.escapeLeft, "")
text = strings.ReplaceAll(text, a.escapeRight, "")
return text
}

View file

@ -1,51 +0,0 @@
package ansi
import (
"fmt"
"testing"
"github.com/jandedobbeleer/oh-my-posh/mock"
"github.com/jandedobbeleer/oh-my-posh/platform"
"github.com/jandedobbeleer/oh-my-posh/shell"
"github.com/jandedobbeleer/oh-my-posh/template"
"github.com/stretchr/testify/assert"
)
func TestMeasureText(t *testing.T) {
cases := []struct {
Case string
Template string
Expected int
}{
{
Case: "Simple text",
Template: "src",
Expected: 3,
},
{
Case: "Hyperlink",
Template: `{{ url "link" "https://ohmyposh.dev" }}`,
Expected: 4,
},
}
env := new(mock.MockedEnvironment)
env.On("TemplateCache").Return(&platform.TemplateCache{
Env: make(map[string]string),
})
shells := []string{shell.BASH, shell.ZSH, shell.GENERIC}
for _, shell := range shells {
for _, tc := range cases {
ansiWriter := &Writer{}
ansiWriter.Init(shell)
tmpl := &template.Text{
Template: tc.Template,
Env: env,
}
text, _ := tmpl.Render()
text = ansiWriter.GenerateHyperlink(text)
got := ansiWriter.MeasureText(text)
assert.Equal(t, tc.Expected, got, fmt.Sprintf("%s: %s", shell, tc.Case))
}
}
}