You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Victorique/vtqe/tools/cd.go

48 lines
882 B
Go

5 years ago
// +build windows
package tools
import (
"bufio"
4 years ago
"fmt"
5 years ago
"os"
"os/exec"
"path/filepath"
4 years ago
"b612.me/starainrt"
5 years ago
"github.com/spf13/cobra"
)
var cdcmd = &cobra.Command{
Use: "cd",
Short: "便捷进入文件夹",
Long: "使用stdin便捷进入文件夹",
Run: func(this *cobra.Command, args []string) {
fileInfo, _ := os.Stdin.Stat()
if (fileInfo.Mode() & os.ModeNamedPipe) != os.ModeNamedPipe {
4 years ago
fmt.Println(args)
if len(args) != 0 {
5 years ago
os.Exit(1)
} else {
exec.Command("cmd.exe", "/c", "explorer "+filepath.Dir(args[0])).Run()
return
}
}
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
4 years ago
dir := s.Text()
if starainrt.IsFile(dir) {
exec.Command("cmd.exe", "/c", "explorer /n,/select,"+dir).Run()
} else {
exec.Command("cmd.exe", "/c", "explorer "+dir).Run()
}
5 years ago
return
}
},
}
func init() {
Maincmd.AddCommand(cdcmd)
}