mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-31 13:57:26 -08:00
refactor: remove unused function
This commit is contained in:
parent
48d8a522bf
commit
59ddfc2ba1
|
@ -9,6 +9,10 @@ import (
|
||||||
"github.com/mattn/go-runewidth"
|
"github.com/mattn/go-runewidth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() { //nolint:gochecknoinits
|
||||||
|
runewidth.DefaultCondition.EastAsianWidth = false
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
knownStyles = []*style{
|
knownStyles = []*style{
|
||||||
{AnchorStart: `<b>`, AnchorEnd: `</b>`, Start: "\x1b[1m", End: "\x1b[22m"},
|
{AnchorStart: `<b>`, AnchorEnd: `</b>`, Start: "\x1b[1m", End: "\x1b[22m"},
|
||||||
|
@ -309,6 +313,15 @@ func (w *Writer) Write(background, foreground, text string) {
|
||||||
w.currentForeground = ""
|
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) {
|
func (w *Writer) printEscapedAnsiString(text string) {
|
||||||
if w.Plain {
|
if w.Plain {
|
||||||
return
|
return
|
||||||
|
@ -491,11 +504,9 @@ func (w *Writer) expandKeyword(keyword string) string {
|
||||||
return keyword
|
return keyword
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Writer) String() (string, int) {
|
func (w *Writer) trimAnsi(text string) string {
|
||||||
defer func() {
|
if len(text) == 0 || !strings.Contains(text, "\x1b") {
|
||||||
w.length = 0
|
return text
|
||||||
w.builder.Reset()
|
}
|
||||||
}()
|
return regex.ReplaceAllString(AnsiRegex, text, "")
|
||||||
|
|
||||||
return w.builder.String(), w.length
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue