diff --git a/internal/app/command_windows.go b/internal/app/command.go similarity index 81% rename from internal/app/command_windows.go rename to internal/app/command.go index 5090d48..10eb1bd 100644 --- a/internal/app/command_windows.go +++ b/internal/app/command.go @@ -1,22 +1,20 @@ -// +build windows - package app import ( "bufio" "fmt" "os" - "syscall" + "runtime" "golang.org/x/text/encoding/simplifiedchinese" "golang.org/x/text/transform" ) -var pipeline = false +var needTransform = false func init() { - ftype, _ := syscall.GetFileType(syscall.Handle(os.Stdin.Fd())) - pipeline = ftype == 3 + stat, _ := os.Stdin.Stat() + needTransform = ((stat.Mode() & os.ModeNamedPipe) != 0) && runtime.GOOS == "windows" } func Root(args []string) { @@ -24,7 +22,7 @@ func Root(args []string) { stdin := bufio.NewScanner(os.Stdin) for stdin.Scan() { line := stdin.Text() - if pipeline { + if needTransform { line, _, _ = transform.String(simplifiedchinese.GBK.NewDecoder(), line) } if line == "quit" || line == "exit" { @@ -42,7 +40,7 @@ func CDN(args []string) { stdin := bufio.NewScanner(os.Stdin) for stdin.Scan() { line := stdin.Text() - if pipeline { + if needTransform { line, _, _ = transform.String(simplifiedchinese.GBK.NewDecoder(), line) } if line == "quit" || line == "exit" { diff --git a/internal/app/command_others.go b/internal/app/command_others.go deleted file mode 100644 index 8348e64..0000000 --- a/internal/app/command_others.go +++ /dev/null @@ -1,39 +0,0 @@ -// +build !windows - -package app - -import ( - "bufio" - "fmt" - "os" -) - -func Root(args []string) { - if len(args) == 0 { - stdin := bufio.NewScanner(os.Stdin) - for stdin.Scan() { - line := stdin.Text() - if line == "quit" || line == "exit" { - return - } - fmt.Printf("%s\n", ReplaceIPInString(ReplaceCDNInString(line))) - } - } else { - ParseIPs(args) - } -} - -func CDN(args []string) { - if len(args) == 0 { - stdin := bufio.NewScanner(os.Stdin) - for stdin.Scan() { - line := stdin.Text() - if line == "quit" || line == "exit" { - return - } - fmt.Println(ReplaceCDNInString(line)) - } - } else { - ParseCDNs(args) - } -}