Skip to content

Commit

Permalink
Word-wrapping help strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamon committed Apr 30, 2024
1 parent 118124e commit e9b2975
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 16 deletions.
12 changes: 9 additions & 3 deletions internal/pkg/cli/command/collection/cmd.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package collection

import (
"github.com/pinecone-io/cli/internal/pkg/utils/docslinks"
"github.com/pinecone-io/cli/internal/pkg/utils/help"
"github.com/pinecone-io/cli/internal/pkg/utils/pcio"
"github.com/pinecone-io/cli/internal/pkg/utils/text"
"github.com/spf13/cobra"
)

var collectionHelpText = pcio.Sprintf(`To learn more about collections, please see %s`, docslinks.UnderstandingCollectionsGuide)
var collectionHelpText = text.WordWrap(`
A collection is a static copy of an index. It is a non-queryable
representation of a set of vectors and metadata. You can create a
collection from an index, and you can create a new index from a
collection. This new index can differ from the original source index: the
new index can have a different number of pods, a different pod type,
or a different similarity metric.
`, 80)

func NewCollectionCmd() *cobra.Command {
cmd := &cobra.Command{
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/cli/command/config/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package config
import (
"github.com/pinecone-io/cli/internal/pkg/utils/configuration"
"github.com/pinecone-io/cli/internal/pkg/utils/pcio"
"github.com/pinecone-io/cli/internal/pkg/utils/text"
"github.com/spf13/cobra"
)

var configHelpText = pcio.Sprintf(`Configuration for this CLI is stored in a file called
config.yaml in the %s directory.`, configuration.NewConfigLocations().ConfigPath)
var configHelpText = text.WordWrap(pcio.Sprintf(`Configuration for this CLI is stored in a file called
config.yaml in the %s directory.`, configuration.NewConfigLocations().ConfigPath), 80)

func NewConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/cli/command/index/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package index

import (
"github.com/pinecone-io/cli/internal/pkg/utils/help"
"github.com/pinecone-io/cli/internal/pkg/utils/text"
"github.com/spf13/cobra"
)

var helpText = `An index is the highest-level organizational unit of
var helpText = text.WordWrap(`An index is the highest-level organizational unit of
vector data in Pinecone. It accepts and stores vectors, serves queries
over the vectors it contains, and does other vector operations over
its contents.`
its contents.`, 80)

func NewIndexCmd() *cobra.Command {
cmd := &cobra.Command{
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/cli/command/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func init() {
"pinecone index create-serverless --help",
}),
Long: pcio.Sprintf(`pinecone is a CLI tool for managing your Pinecone resources
Get started by logging in with
%s
`, style.CodeWithPrompt("pinecone login")),
}
Expand Down
20 changes: 13 additions & 7 deletions internal/pkg/cli/command/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@ import (
"github.com/spf13/cobra"
)

var targetHelpTemplate string = `Many API calls take place in the context of a specific project.
var targetHelpPart1 string = text.WordWrap(`Many API calls take place in the context of a specific project.
When using the CLI interactively (i.e. via the device authorization flow) you
should use this command to set the current project context for the CLI.
should use this command to set the current project context for the CLI.`, 80)

If you're not sure what values to pass to this command, you can discover available
projects and organizations by running %s.
var targetHelpPart2 string = text.WordWrap(pcio.Sprintf(`If you're not sure what values to pass to
this command, you can discover available projects and organizations by running %s.`, style.Code("pinecone project list")), 80)

For automation use cases relying on API-Keys for authentication, there's no need
var targetHelpPart3 = text.WordWrap(`For automation use cases relying on API-Keys for authentication, there's no need
to specify a project context as the API-Key is already associated with a specific
project in the backend.
`
var targetHelp = pcio.Sprintf(targetHelpTemplate, style.Code("pinecone project list"))
`, 80)

var targetHelp = pcio.Sprintf(`%s
%s
%s
`, targetHelpPart1, targetHelpPart2, targetHelpPart3)

type TargetOptions struct {
Org string
Expand Down
34 changes: 34 additions & 0 deletions internal/pkg/utils/text/wordwrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package text

import (
"strings"
)

func WordWrap(text string, maxWidth int) string {
// First, remove all remaining newlines
text = strings.ReplaceAll(text, "\n", " ")

var wrappedLines []string
words := strings.Fields(text)
line := ""
lineLength := 0

for _, word := range words {
wordLength := len(word)

if lineLength+wordLength <= maxWidth {
line += word + " "
lineLength += wordLength + 1
} else {
wrappedLines = append(wrappedLines, line)
line = word + " "
lineLength = wordLength + 1
}
}

if line != "" {
wrappedLines = append(wrappedLines, line)
}

return strings.Join(wrappedLines, "\n")
}

0 comments on commit e9b2975

Please sign in to comment.