From 217defb44e93fb25ebf356c4c160cda3d6d677fb Mon Sep 17 00:00:00 2001 From: iikira <2571583272@qq.com> Date: Fri, 27 Jul 2018 09:27:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=87=AA=E5=8A=A8=E8=A1=A5?= =?UTF-8?q?=E5=85=A8=E4=B8=8D=E8=BD=AC=E4=B9=89=E9=83=A8=E5=88=86=E7=A9=BA?= =?UTF-8?q?=E7=99=BD=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 2 +- pcsliner/clear.go | 12 +++++------- pcsliner/clear_windows.go | 14 ++++++++++++++ pcspath/util.go | 17 ++++++++++++++--- 4 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 pcsliner/clear_windows.go diff --git a/main.go b/main.go index 6d9dfba1..2e9c8b03 100644 --- a/main.go +++ b/main.go @@ -1695,7 +1695,7 @@ func main() { Description: "清空控制台屏幕", Category: "其他", Action: func(c *cli.Context) error { - pcsliner.ClearScreen(line.State) + line.ClearScreen() return nil }, }, diff --git a/pcsliner/clear.go b/pcsliner/clear.go index f462af9f..276a581f 100644 --- a/pcsliner/clear.go +++ b/pcsliner/clear.go @@ -1,14 +1,12 @@ +// +build !windows + package pcsliner import ( - "github.com/peterh/liner" - _ "unsafe" // for go:linkname + "fmt" ) -//go:linkname eraseScreen github.com/iikira/BaiduPCS-Go/vendor/github.com/peterh/liner.(*State).eraseScreen -func eraseScreen(s *liner.State) - // ClearScreen 清空屏幕 -func ClearScreen(s *liner.State) { - eraseScreen(s) +func (pl *PCSLiner) ClearScreen() { + fmt.Print("\x1b[H\x1b[2J") } diff --git a/pcsliner/clear_windows.go b/pcsliner/clear_windows.go new file mode 100644 index 00000000..540c2ca9 --- /dev/null +++ b/pcsliner/clear_windows.go @@ -0,0 +1,14 @@ +package pcsliner + +import ( + "github.com/peterh/liner" + _ "unsafe" // for go:linkname +) + +//go:linkname eraseScreen github.com/iikira/BaiduPCS-Go/vendor/github.com/peterh/liner.(*State).eraseScreen +func eraseScreen(s *liner.State) + +// ClearScreen 清空屏幕 +func (pl *PCSLiner) ClearScreen() { + eraseScreen(pl.State) +} diff --git a/pcspath/util.go b/pcspath/util.go index ed739e38..355a735c 100644 --- a/pcspath/util.go +++ b/pcspath/util.go @@ -3,6 +3,7 @@ package pcspath import ( "path" "strings" + "unicode" ) // EscapeBracketOne 转义中括号, 加一个反斜杠 @@ -68,9 +69,17 @@ func SplitAll(pcspath string) (elem []string) { return } -// Escape 转义字符串的空格, 小括号, 中括号 +func isSpace(r rune) rune { + if unicode.IsSpace(r) { + return r + } + return -1 +} + +// Escape 转义字符串的空白符号, 小括号, 中括号 func Escape(pcspath string) string { - if !strings.ContainsAny(pcspath, " []()") { + // 没有空格 + if !strings.ContainsAny(pcspath, "[]()") && strings.IndexFunc(pcspath, unicode.IsSpace) == -1 { return pcspath } @@ -86,7 +95,9 @@ func Escape(pcspath string) string { builder.WriteRune('\\') } continue - case ' ', '[', ']', '(', ')': + case isSpace(s): + fallthrough + case '[', ']', '(', ')': builder.WriteString("\\") builder.WriteRune(s) isSlash = false