Skip to content

Commit

Permalink
feat: add pagination and searching to atmos docs command (#848)
Browse files Browse the repository at this point in the history
* feat: add pagination and search to docs command

* docs: update documentation with pagination settings

* fix: cleanup documentation and capitalization

---------

Co-authored-by: Andriy Knysh <[email protected]>
  • Loading branch information
RoseSecurity and aknysh authored Dec 14, 2024
1 parent c0e7054 commit 5681e54
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
5 changes: 4 additions & 1 deletion cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ var docsCmd = &cobra.Command{
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}

fmt.Println(componentDocs)
if err := u.DisplayDocs(componentDocs, cliConfig.Settings.Docs.Pagination); err != nil {
u.LogErrorAndExit(schema.CliConfiguration{}, fmt.Errorf("failed to display documentation: %w", err))
}

return
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ type CliSettings struct {
}

type Docs struct {
MaxWidth int `yaml:"max-width" json:"max_width" mapstructure:"max-width"`
MaxWidth int `yaml:"max-width" json:"max_width" mapstructure:"max-width"`
Pagination bool `yaml:"pagination" json:"pagination" mapstructure:"pagination"`
}

type Templates struct {
Expand Down
39 changes: 39 additions & 0 deletions pkg/utils/doc_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package utils

import (
"fmt"
"os"
"os/exec"
"strings"
)

// DisplayDocs displays component documentation directly through the terminal or
// through a pager (like less). The use of a pager is determined by the pagination value
// set in the CLI Settings for Atmos
func DisplayDocs(componentDocs string, usePager bool) error {
if !usePager {
fmt.Println(componentDocs)
return nil
}

pagerCmd := os.Getenv("PAGER")
if pagerCmd == "" {
pagerCmd = "less -r"
}

args := strings.Fields(pagerCmd)
if len(args) == 0 {
return fmt.Errorf("invalid pager command")
}

cmd := exec.Command(args[0], args[1:]...)
cmd.Stdin = strings.NewReader(componentDocs)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to execute pager: %w", err)
}

return nil
}
21 changes: 20 additions & 1 deletion website/docs/cli/configuration/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ The `settings` section configures Atmos global settings.
# If the source and destination lists have the same length, all items in the destination lists are
# deep-merged with all items in the source list.
list_merge_strategy: replace
# `docs` specifies how component documentation is displayed in the terminal.
# The following documentation display settings are supported:
# `max-width`: The maximum width for displaying component documentation in the terminal.
# 'pagination`: When enabled, displays component documentation in a pager instead of directly in the terminal.
docs:
max-width: 80
pagination: true
```
</File>
Expand All @@ -171,9 +178,21 @@ The `settings` section configures Atmos global settings.
<dd>The items in the destination list are deep-merged with the items in the source list. The items in the source list take precedence. The items are processed starting from the first up to the length of the source list (the remaining items are not processed). If the source and destination lists have the same length, all items in the destination lists are deep-merged with all items in the source list.</dd>
</dl>
</dd>
</dl>

<dt>`settings.docs`</dt>
<dd>
Specifies how component documentation is displayed in the terminal.

The following settings are supported:
<dl>
<dt>`max-width`</dt>
<dd>The maximum width for displaying component documentation in the terminal.</dd>

<dt>`pagination`</dt>
<dd>When enabled, displays component documentation in a pager instead of directly in the terminal.</dd>
</dl>
</dd>
</dl>

## Workflows

Expand Down

0 comments on commit 5681e54

Please sign in to comment.