diff options
Diffstat (limited to 'drive')
| -rw-r--r-- | drive/files.go | 8 | ||||
| -rw-r--r-- | drive/print.go | 68 | ||||
| -rw-r--r-- | drive/types.go | 6 | 
3 files changed, 78 insertions, 4 deletions
diff --git a/drive/files.go b/drive/files.go index f1a4b97..b10f2f6 100644 --- a/drive/files.go +++ b/drive/files.go @@ -14,9 +14,11 @@ func (self *Drive) List(args ListFilesArgs) {      fileList, err := self.service.Files.List().PageSize(args.MaxFiles).Q(args.Query).Fields("nextPageToken", "files(id,name,size,createdTime)").Do()      errorF(err, "Failed listing files: %s\n", err) -    for _, f := range fileList.Files { -        fmt.Printf("%s %s %d %s\n", f.Id, f.Name, f.Size, f.CreatedTime) -    } +    PrintFileList(PrintFileListArgs{ +        Files: fileList.Files, +        SkipHeader: args.SkipHeader, +        SizeInBytes: args.SizeInBytes, +    })  } diff --git a/drive/print.go b/drive/print.go index 5473690..85d51b4 100644 --- a/drive/print.go +++ b/drive/print.go @@ -1,13 +1,36 @@  package drive  import ( +    "os"      "fmt" +    "text/tabwriter"      "strings"      "strconv" +    "unicode/utf8"      "time"  ) +func PrintFileList(args PrintFileListArgs) { +    w := new(tabwriter.Writer) +    w.Init(os.Stdout, 0, 0, 3, ' ', 0) + +    if !args.SkipHeader { +        fmt.Fprintln(w, "Id\tName\tSize\tCreated") +    } + +    for _, f := range args.Files { +        fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", +            f.Id, +            truncateString(f.Name, 40), +            formatSize(f.Size, args.SizeInBytes), +            formatDatetime(f.CreatedTime), +        ) +    } + +    w.Flush() +} +  func PrintFileInfo(args PrintFileInfoArgs) {      f := args.File @@ -31,7 +54,6 @@ func PrintFileInfo(args PrintFileInfoArgs) {      }  } -  func formatList(a []string) string {      return strings.Join(a, ", ")  } @@ -67,3 +89,47 @@ func formatDatetime(iso string) string {      hour, min, sec := local.Clock()      return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, min, sec)  } + +// Truncates string to given max length, and inserts ellipsis into +// the middle of the string to signify that the string has been truncated +func truncateString(str string, maxRunes int) string { +    indicator := "..." + +    // Number of runes in string +    runeCount := utf8.RuneCountInString(str) + +    // Return input string if length of input string is less than max length +    // Input string is also returned if max length is less than 9 which is the minmal supported length +    if runeCount <= maxRunes || maxRunes < 9 { +        return str +    } + +    // Number of remaining runes to be removed +    remaining := (runeCount - maxRunes) + utf8.RuneCountInString(indicator) + +    var truncated string +    var skip bool + +    for leftOffset, char := range str { +        rightOffset := runeCount - (leftOffset + remaining) + +        // Start skipping chars when the left and right offsets are equal +        // Or in the case where we wont be able to do an even split: when the left offset is larger than the right offset +        if leftOffset == rightOffset || (leftOffset > rightOffset && !skip) { +            skip = true +            truncated += indicator +        } + +        if skip && remaining > 0 { +            // Skip char and decrement the remaining skip counter +            remaining-- +            continue +        } + +        // Add char to result string +        truncated += string(char) +    } + +    // Return truncated string +    return truncated +} diff --git a/drive/types.go b/drive/types.go index ecf92a1..296902c 100644 --- a/drive/types.go +++ b/drive/types.go @@ -51,6 +51,12 @@ type FileInfoArgs struct {      SizeInBytes bool  } +type PrintFileListArgs struct { +    Files []*drive.File +    SkipHeader bool +    SizeInBytes bool +} +  type PrintFileInfoArgs struct {      File *drive.File      SizeInBytes bool  | 
