github.com/fzfile/BaiduPCS-Go@v0.0.0-20200606205115-4408961cf336/internal/pcscommand/tree.go (about)

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