github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/internal/pcscommand/ls_search.go (about) 1 package pcscommand 2 3 import ( 4 "fmt" 5 "github.com/qjfoidnh/BaiduPCS-Go/baidupcs" 6 "github.com/qjfoidnh/BaiduPCS-Go/pcstable" 7 "github.com/qjfoidnh/BaiduPCS-Go/pcsutil/converter" 8 "github.com/qjfoidnh/BaiduPCS-Go/pcsutil/pcstime" 9 "github.com/olekukonko/tablewriter" 10 "os" 11 "strconv" 12 ) 13 14 type ( 15 // LsOptions 列目录可选项 16 LsOptions struct { 17 Total bool 18 } 19 20 // SearchOptions 搜索可选项 21 SearchOptions struct { 22 Total bool 23 Recurse bool 24 } 25 ) 26 27 const ( 28 opLs int = iota 29 opSearch 30 ) 31 32 // RunLs 执行列目录 33 func RunLs(pcspath string, lsOptions *LsOptions, orderOptions *baidupcs.OrderOptions) { 34 err := matchPathByShellPatternOnce(&pcspath) 35 if err != nil { 36 fmt.Println(err) 37 return 38 } 39 40 files, err := GetBaiduPCS().FilesDirectoriesList(pcspath, orderOptions) 41 if err != nil { 42 fmt.Println(err) 43 return 44 } 45 46 fmt.Printf("\n当前目录: %s\n----\n", pcspath) 47 48 if lsOptions == nil { 49 lsOptions = &LsOptions{} 50 } 51 52 renderTable(opLs, lsOptions.Total, pcspath, files) 53 return 54 } 55 56 // RunSearch 执行搜索 57 func RunSearch(targetPath, keyword string, opt *SearchOptions) { 58 err := matchPathByShellPatternOnce(&targetPath) 59 if err != nil { 60 fmt.Println(err) 61 return 62 } 63 64 if opt == nil { 65 opt = &SearchOptions{} 66 } 67 68 files, err := GetBaiduPCS().Search(targetPath, keyword, opt.Recurse) 69 if err != nil { 70 fmt.Println(err) 71 return 72 } 73 74 renderTable(opSearch, opt.Total, targetPath, files) 75 return 76 } 77 78 func renderTable(op int, isTotal bool, path string, files baidupcs.FileDirectoryList) { 79 tb := pcstable.NewTable(os.Stdout) 80 var ( 81 fN, dN int64 82 showPath string 83 ) 84 85 switch op { 86 case opLs: 87 showPath = "文件(目录)" 88 case opSearch: 89 showPath = "路径" 90 } 91 92 if isTotal { 93 tb.SetHeader([]string{"#", "fs_id", "app_id", "文件大小", "创建日期", "修改日期", "md5(截图请打码)", showPath}) 94 tb.SetColumnAlignment([]int{tablewriter.ALIGN_DEFAULT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT}) 95 for k, file := range files { 96 if file.Isdir { 97 tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(file.FsID, 10), strconv.FormatInt(file.AppID, 10), "-", pcstime.FormatTime(file.Ctime), pcstime.FormatTime(file.Mtime), file.MD5, file.Filename + baidupcs.PathSeparator}) 98 continue 99 } 100 101 var md5 string 102 if len(file.BlockList) > 1 { 103 md5 = "(可能不正确)" + file.MD5 104 } else { 105 md5 = file.MD5 106 } 107 108 switch op { 109 case opLs: 110 tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(file.FsID, 10), strconv.FormatInt(file.AppID, 10), converter.ConvertFileSize(file.Size, 2), pcstime.FormatTime(file.Ctime), pcstime.FormatTime(file.Mtime), md5, file.Filename}) 111 case opSearch: 112 tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(file.FsID, 10), strconv.FormatInt(file.AppID, 10), converter.ConvertFileSize(file.Size, 2), pcstime.FormatTime(file.Ctime), pcstime.FormatTime(file.Mtime), md5, file.Path}) 113 } 114 } 115 fN, dN = files.Count() 116 tb.Append([]string{"", "", "总: " + converter.ConvertFileSize(files.TotalSize(), 2), "", "", "", fmt.Sprintf("文件总数: %d, 目录总数: %d", fN, dN)}) 117 } else { 118 tb.SetHeader([]string{"#", "文件大小", "修改日期", showPath}) 119 tb.SetColumnAlignment([]int{tablewriter.ALIGN_DEFAULT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT}) 120 for k, file := range files { 121 if file.Isdir { 122 tb.Append([]string{strconv.Itoa(k), "-", pcstime.FormatTime(file.Mtime), file.Filename + baidupcs.PathSeparator}) 123 continue 124 } 125 126 switch op { 127 case opLs: 128 tb.Append([]string{strconv.Itoa(k), converter.ConvertFileSize(file.Size, 2), pcstime.FormatTime(file.Mtime), file.Filename}) 129 case opSearch: 130 tb.Append([]string{strconv.Itoa(k), converter.ConvertFileSize(file.Size, 2), pcstime.FormatTime(file.Mtime), file.Path}) 131 } 132 } 133 fN, dN = files.Count() 134 tb.Append([]string{"", "总: " + converter.ConvertFileSize(files.TotalSize(), 2), "", fmt.Sprintf("文件总数: %d, 目录总数: %d", fN, dN)}) 135 } 136 137 tb.Render() 138 139 if fN+dN >= 50 { 140 fmt.Printf("\n当前目录: %s\n", path) 141 } 142 143 fmt.Printf("----\n") 144 }