Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pagination and searching to atmos docs command #848

Merged
merged 4 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
RoseSecurity marked this conversation as resolved.
Show resolved Hide resolved

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
Loading