github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/cmd/stringers.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/dustin/go-humanize"
    10  	"github.com/fatih/color"
    11  	"github.com/qri-io/qri/config"
    12  	"github.com/qri-io/qri/dsref"
    13  	"github.com/qri-io/qri/lib"
    14  	"github.com/qri-io/qri/registry"
    15  	reporef "github.com/qri-io/qri/repo/ref"
    16  )
    17  
    18  type peerStringer config.ProfilePod
    19  
    20  // StringerLocation is the function to retrieve the timezone location
    21  var StringerLocation *time.Location
    22  
    23  func init() {
    24  	StringerLocation = time.Now().Location()
    25  }
    26  
    27  // String assumes that Peername and ID are present
    28  func (p peerStringer) String() string {
    29  	w := &bytes.Buffer{}
    30  	name := color.New(color.FgGreen, color.Bold).SprintFunc()
    31  	online := color.New(color.FgYellow).SprintFunc()
    32  	if p.Online {
    33  		fmt.Fprintf(w, "%s | %s\n", name(p.Peername), online("online"))
    34  	} else {
    35  		fmt.Fprintf(w, "%s\n", name(p.Peername))
    36  	}
    37  	fmt.Fprintf(w, "Profile ID: %s\n", p.ID)
    38  	plural := "es"
    39  	spacer := "              "
    40  	if len(p.NetworkAddrs) <= 1 {
    41  		plural = ""
    42  	}
    43  	for i, addr := range p.NetworkAddrs {
    44  		if i == 0 {
    45  			fmt.Fprintf(w, "Address%s:    %s\n", plural, addr)
    46  			continue
    47  		}
    48  		fmt.Fprintf(w, "%s%s\n", spacer, addr)
    49  	}
    50  	fmt.Fprintln(w, "")
    51  	return w.String()
    52  }
    53  
    54  type stringer string
    55  
    56  func (s stringer) String() string {
    57  	return string(s) + "\n"
    58  }
    59  
    60  type refStringer reporef.DatasetRef
    61  
    62  // String assumes Peername and Name are present
    63  func (r refStringer) String() string {
    64  	w := &bytes.Buffer{}
    65  	title := color.New(color.FgGreen, color.Bold).SprintFunc()
    66  	path := color.New(color.Faint).SprintFunc()
    67  	warn := color.New(color.FgYellow).SprintFunc()
    68  	ds := r.Dataset
    69  	dsr := reporef.DatasetRef(r)
    70  
    71  	fmt.Fprintf(w, "%s", title(dsr.AliasString()))
    72  	if ds != nil && ds.Meta != nil && ds.Meta.Title != "" {
    73  		fmt.Fprintf(w, "\n%s", ds.Meta.Title)
    74  	}
    75  	if r.FSIPath != "" {
    76  		fmt.Fprintf(w, "\nlinked: %s", path(r.FSIPath))
    77  	} else if r.Path != "" {
    78  		fmt.Fprintf(w, "\n%s", path(r.Path))
    79  	}
    80  	if r.Foreign {
    81  		fmt.Fprintf(w, "\n%s", warn("foreign"))
    82  	}
    83  	if ds != nil && ds.Structure != nil {
    84  		fmt.Fprintf(w, "\n%s", humanize.Bytes(uint64(ds.Structure.Length)))
    85  		if ds.Structure.Entries == 1 {
    86  			fmt.Fprintf(w, ", %d entry", ds.Structure.Entries)
    87  		} else {
    88  			fmt.Fprintf(w, ", %d entries", ds.Structure.Entries)
    89  		}
    90  		if ds.Structure.ErrCount == 1 {
    91  			fmt.Fprintf(w, ", %d error", ds.Structure.ErrCount)
    92  		} else {
    93  			fmt.Fprintf(w, ", %d errors", ds.Structure.ErrCount)
    94  		}
    95  		if ds.NumVersions == 0 {
    96  			// nothing
    97  		} else if ds.NumVersions == 1 {
    98  			fmt.Fprintf(w, ", %d version", ds.NumVersions)
    99  		} else {
   100  			fmt.Fprintf(w, ", %d versions", ds.NumVersions)
   101  		}
   102  	}
   103  
   104  	fmt.Fprintf(w, "\n\n")
   105  	return w.String()
   106  }
   107  
   108  type versionInfoStringer dsref.VersionInfo
   109  
   110  // String assumes Peername and Name are present
   111  func (vis versionInfoStringer) String() string {
   112  	w := &bytes.Buffer{}
   113  	title := color.New(color.FgGreen, color.Bold).SprintFunc()
   114  	path := color.New(color.Faint).SprintFunc()
   115  	warn := color.New(color.FgYellow).SprintFunc()
   116  
   117  	v := dsref.VersionInfo(vis)
   118  	sr := v.SimpleRef()
   119  	fmt.Fprintf(w, "%s", title(sr.Alias()))
   120  
   121  	if vis.MetaTitle != "" {
   122  		fmt.Fprintf(w, "\n%s", vis.MetaTitle)
   123  	}
   124  	if vis.Path != "" {
   125  		fmt.Fprintf(w, "\n%s", path(vis.Path))
   126  	}
   127  	if vis.Foreign {
   128  		fmt.Fprintf(w, "\n%s", warn("foreign"))
   129  	}
   130  	fmt.Fprintf(w, "\n%s", humanize.Bytes(uint64(vis.BodySize)))
   131  	if vis.BodyRows == 1 {
   132  		fmt.Fprintf(w, ", %d entry", vis.BodyRows)
   133  	} else {
   134  		fmt.Fprintf(w, ", %d entries", vis.BodyRows)
   135  	}
   136  	if vis.NumErrors == 1 {
   137  		fmt.Fprintf(w, ", %d error", vis.NumErrors)
   138  	} else {
   139  		fmt.Fprintf(w, ", %d errors", vis.NumErrors)
   140  	}
   141  	if vis.CommitCount == 0 {
   142  		// nothing
   143  	} else if vis.CommitCount == 1 {
   144  		fmt.Fprintf(w, ", %d version", vis.CommitCount)
   145  	} else {
   146  		fmt.Fprintf(w, ", %d versions", vis.CommitCount)
   147  	}
   148  
   149  	fmt.Fprintf(w, "\n\n")
   150  	return w.String()
   151  }
   152  
   153  type searchResultStringer registry.SearchResult
   154  
   155  func (r searchResultStringer) String() string {
   156  	w := &strings.Builder{}
   157  	title := color.New(color.FgGreen, color.Bold).SprintFunc()
   158  	path := color.New(color.Faint).SprintFunc()
   159  	ds := r.Value
   160  
   161  	fmt.Fprintf(w, "%s/%s", title(ds.Peername), title(ds.Name))
   162  	fmt.Fprintf(w, "\n%s", path(ds.Path))
   163  
   164  	if ds != nil && ds.Meta != nil && ds.Meta.Title != "" {
   165  		fmt.Fprintf(w, "\n%s", ds.Meta.Title)
   166  	}
   167  	if ds != nil && ds.Structure != nil {
   168  		fmt.Fprintf(w, "\n%s", humanize.Bytes(uint64(ds.Structure.Length)))
   169  		if ds.Structure.Entries == 1 {
   170  			fmt.Fprintf(w, ", %d entry", ds.Structure.Entries)
   171  		} else {
   172  			fmt.Fprintf(w, ", %d entries", ds.Structure.Entries)
   173  		}
   174  		if ds.Structure.ErrCount == 1 {
   175  			fmt.Fprintf(w, ", %d error", ds.Structure.ErrCount)
   176  		} else {
   177  			fmt.Fprintf(w, ", %d errors", ds.Structure.ErrCount)
   178  		}
   179  		if ds.NumVersions == 0 {
   180  			// nothing
   181  		} else if ds.NumVersions == 1 {
   182  			fmt.Fprintf(w, ", %d version", ds.NumVersions)
   183  		} else {
   184  			fmt.Fprintf(w, ", %d versions", ds.NumVersions)
   185  		}
   186  	}
   187  
   188  	fmt.Fprintf(w, "\n\n")
   189  	return w.String()
   190  }
   191  
   192  type logStringer reporef.DatasetRef
   193  
   194  // String assumes Path, Peername, Timestamp and Title are present
   195  func (l logStringer) String() string {
   196  	w := &bytes.Buffer{}
   197  	// title := color.New(color.Bold).Sprintfunc()
   198  	path := color.New(color.FgGreen).SprintFunc()
   199  	dsr := reporef.DatasetRef(l)
   200  
   201  	fmt.Fprintf(w, "%s\n", path("path:   "+dsr.Path))
   202  	fmt.Fprintf(w, "Author: %s\n", dsr.Peername)
   203  	fmt.Fprintf(w, "Date:   %s\n", dsr.Dataset.Commit.Timestamp.Format("Jan _2 15:04:05"))
   204  	fmt.Fprintf(w, "\n    %s\n", dsr.Dataset.Commit.Title)
   205  	if dsr.Dataset.Commit.Message != "" {
   206  		fmt.Fprintf(w, "    %s\n", dsr.Dataset.Commit.Message)
   207  	}
   208  
   209  	fmt.Fprintf(w, "\n")
   210  	return w.String()
   211  }
   212  
   213  func oneLiner(str string, maxLen int) string {
   214  	str = strings.Split(str, "\n")[0]
   215  	if len(str) > maxLen-3 {
   216  		str = str[:maxLen-3] + "..."
   217  	}
   218  	return str
   219  }
   220  
   221  type logEntryStringer lib.LogEntry
   222  
   223  func (s logEntryStringer) String() string {
   224  	title := color.New(color.FgGreen, color.Bold).SprintFunc()
   225  	ts := color.New(color.Faint).SprintFunc()
   226  
   227  	return fmt.Sprintf("%s\t%s\t%s\t%s\n",
   228  		ts(s.Timestamp.Format(time.RFC3339)),
   229  		title(s.Author),
   230  		title(s.Action),
   231  		s.Note,
   232  	)
   233  }
   234  
   235  type dslogItemStringer dsref.VersionInfo
   236  
   237  func (s dslogItemStringer) String() string {
   238  	yellow := color.New(color.FgYellow).SprintFunc()
   239  	faint := color.New(color.Faint).SprintFunc()
   240  
   241  	storage := "local"
   242  	if s.Foreign {
   243  		storage = faint("remote")
   244  	}
   245  
   246  	msg := fmt.Sprintf("%s%s\n%s%s\n%s%s\n%s%s\n\n%s\n",
   247  		faint("Commit:  "),
   248  		yellow(s.Path),
   249  		faint("Date:    "),
   250  		s.CommitTime.In(StringerLocation).Format(time.UnixDate),
   251  		faint("Storage: "),
   252  		storage,
   253  		faint("Size:    "),
   254  		humanize.Bytes(uint64(s.BodySize)),
   255  		s.CommitTitle,
   256  	)
   257  	if s.CommitMessage != "" && s.CommitMessage != s.CommitTitle {
   258  		msg += fmt.Sprintf("%s\n", s.CommitMessage)
   259  	}
   260  	msg += "\n"
   261  
   262  	return msg
   263  }