瀏覽代碼

Merge pull request #4463 from ollama/jyan/line-display

changed line display to be calculated with runewidth
Josh 11 月之前
父節點
當前提交
5bece94509
共有 1 個文件被更改,包括 12 次插入5 次删除
  1. 12 5
      cmd/cmd.go

+ 12 - 5
cmd/cmd.go

@@ -25,7 +25,7 @@ import (
 	"time"
 	"time"
 
 
 	"github.com/containerd/console"
 	"github.com/containerd/console"
-
+	"github.com/mattn/go-runewidth"
 	"github.com/olekukonko/tablewriter"
 	"github.com/olekukonko/tablewriter"
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 	"golang.org/x/crypto/ssh"
 	"golang.org/x/crypto/ssh"
@@ -744,7 +744,8 @@ func displayResponse(content string, wordWrap bool, state *displayResponseState)
 	if wordWrap && termWidth >= 10 {
 	if wordWrap && termWidth >= 10 {
 		for _, ch := range content {
 		for _, ch := range content {
 			if state.lineLength+1 > termWidth-5 {
 			if state.lineLength+1 > termWidth-5 {
-				if len(state.wordBuffer) > termWidth-10 {
+
+				if runewidth.StringWidth(state.wordBuffer) > termWidth-10 {
 					fmt.Printf("%s%c", state.wordBuffer, ch)
 					fmt.Printf("%s%c", state.wordBuffer, ch)
 					state.wordBuffer = ""
 					state.wordBuffer = ""
 					state.lineLength = 0
 					state.lineLength = 0
@@ -752,12 +753,18 @@ func displayResponse(content string, wordWrap bool, state *displayResponseState)
 				}
 				}
 
 
 				// backtrack the length of the last word and clear to the end of the line
 				// backtrack the length of the last word and clear to the end of the line
-				fmt.Printf("\x1b[%dD\x1b[K\n", len(state.wordBuffer))
+				fmt.Printf("\x1b[%dD\x1b[K\n", runewidth.StringWidth(state.wordBuffer))
 				fmt.Printf("%s%c", state.wordBuffer, ch)
 				fmt.Printf("%s%c", state.wordBuffer, ch)
-				state.lineLength = len(state.wordBuffer) + 1
+				chWidth := runewidth.RuneWidth(ch)
+
+				state.lineLength = runewidth.StringWidth(state.wordBuffer) + chWidth
 			} else {
 			} else {
 				fmt.Print(string(ch))
 				fmt.Print(string(ch))
-				state.lineLength += 1
+				state.lineLength += runewidth.RuneWidth(ch)
+				if runewidth.RuneWidth(ch) >= 2 {
+					state.wordBuffer = ""
+					continue
+				}
 
 
 				switch ch {
 				switch ch {
 				case ' ':
 				case ' ':