github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/internal/pcscommand/tree.go (about)

     1  package pcscommand
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/qjfoidnh/BaiduPCS-Go/baidupcs"
     6  	"strings"
     7  )
     8  
     9  const (
    10  	indentPrefix   = "│   "
    11  	pathPrefix     = "├──"
    12  	lastFilePrefix = "└──"
    13  )
    14  
    15  type (
    16  	TreeOptions struct {
    17  		Depth    int
    18  		ShowFsid bool
    19  	}
    20  )
    21  
    22  func getTree(pcspath string, depth int, option *TreeOptions) {
    23  	var (
    24  		err   error
    25  		files baidupcs.FileDirectoryList
    26  	)
    27  	if depth == 0 {
    28  		err := matchPathByShellPatternOnce(&pcspath)
    29  		if err != nil {
    30  			fmt.Println(err)
    31  			return
    32  		}
    33  	}
    34  
    35  	files, err = GetBaiduPCS().FilesDirectoriesList(pcspath, baidupcs.DefaultOrderOptions)
    36  	if err != nil {
    37  		fmt.Println(err)
    38  		return
    39  	}
    40  
    41  	var (
    42  		prefix          = pathPrefix
    43  		fN              = len(files)
    44  		indentPrefixStr = strings.Repeat(indentPrefix, depth)
    45  	)
    46  	for i, file := range files {
    47  		if file.Isdir {
    48  			if option.ShowFsid {
    49  				fmt.Printf("%v%v %v/: %v\n", indentPrefixStr, pathPrefix, file.Filename, file.FsID)
    50  			} else {
    51  				fmt.Printf("%v%v %v/\n", indentPrefixStr, pathPrefix, file.Filename)
    52  			}
    53  			if option.Depth < 0 || depth < option.Depth {
    54  				getTree(file.Path, depth+1, option)
    55  			}
    56  			continue
    57  		}
    58  
    59  		if i+1 == fN {
    60  			prefix = lastFilePrefix
    61  		}
    62  		if option.ShowFsid {
    63  			fmt.Printf("%v%v %v: %v\n", indentPrefixStr, prefix, file.Filename, file.FsID)
    64  		} else {
    65  			fmt.Printf("%v%v %v\n", indentPrefixStr, prefix, file.Filename)
    66  		}
    67  	}
    68  
    69  	return
    70  }
    71  
    72  // RunTree 列出树形图
    73  func RunTree(path string, depth int, option *TreeOptions) {
    74  	getTree(path, depth, option)
    75  }