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: make sdk instructions scrollable #150

Merged
merged 6 commits into from
Apr 9, 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
3 changes: 2 additions & 1 deletion internal/quickstart/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (m ContainerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.flagKey = msg.flag.key
m.currentStep += 1
m.err = nil
cmd = sendEnableMouseCellMotionMsg()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can either enable this here as a message or at the start of the quickstart program, thought it was safer to enable here?

Copy link
Contributor

@sunnyguduru sunnyguduru Apr 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better here too. Since we don't use the feature elsewhere

case errMsg:
m.currentModel, cmd = m.currentModel.Update(msg)
case noInstructionsMsg:
Expand All @@ -124,7 +125,7 @@ func (m ContainerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.sdk.kind,
)
m.currentStep += 1
case fetchedSDKInstructions, fetchedEnv, selectedSDKMsg, toggledFlagMsg, spinner.TickMsg, createdFlagMsg:
case fetchedSDKInstructions, fetchedEnv, selectedSDKMsg, toggledFlagMsg, spinner.TickMsg, createdFlagMsg, tea.MouseMsg:
m.currentModel, cmd = m.currentModel.Update(msg)
m.err = nil
case showToggleFlagMsg:
Expand Down
6 changes: 6 additions & 0 deletions internal/quickstart/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ func sendShowToggleFlagMsg() tea.Cmd {
}
}

func sendEnableMouseCellMotionMsg() tea.Cmd {
return func() tea.Msg {
return tea.EnableMouseCellMotion()
}
}

type fetchedEnv struct {
sdkKey string
}
Expand Down
58 changes: 38 additions & 20 deletions internal/quickstart/show_sdk_instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package quickstart

import (
"fmt"

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/reflow/wordwrap"

"ldcli/internal/sdks"
)

const (
viewportWidth = 80
viewportHeight = 30
)
Comment on lines +15 to +18
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arbitrary values I picked - we could adjust these based on the window size if we want?


type showSDKInstructionsModel struct {
accessToken string
baseUri string
Expand All @@ -26,6 +29,7 @@ type showSDKInstructionsModel struct {
sdkKey string
spinner spinner.Model
url string
viewport viewport.Model
}

func NewShowSDKInstructionsModel(
Expand All @@ -39,6 +43,13 @@ func NewShowSDKInstructionsModel(
s := spinner.New()
s.Spinner = spinner.Points

vp := viewport.New(viewportWidth, viewportHeight)
vp.Style = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62")).
PaddingRight(2)
vp.MouseWheelEnabled = true

return showSDKInstructionsModel{
accessToken: accessToken,
baseUri: baseUri,
Expand All @@ -50,8 +61,9 @@ func NewShowSDKInstructionsModel(
Back: BindingBack,
Quit: BindingQuit,
},
spinner: s,
url: url,
spinner: s,
url: url,
viewport: vp,
}
}

Expand All @@ -71,12 +83,21 @@ func (m showSDKInstructionsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, pressableKeys.Enter):
// TODO: only if all data are fetched?
cmd = sendShowToggleFlagMsg()
default:
m.viewport, cmd = m.viewport.Update(msg)
}
case tea.MouseMsg:
m.viewport, cmd = m.viewport.Update(msg)
case fetchedSDKInstructions:
m.instructions = sdks.ReplaceFlagKey(string(msg.instructions), m.flagKey)
case fetchedEnv:
m.sdkKey = msg.sdkKey
m.instructions = sdks.ReplaceSDKKey(string(m.instructions), msg.sdkKey)
md, err := m.renderMarkdown()
if err != nil {
return m, sendErr(err)
}
m.viewport.SetContent(md)
Comment on lines +96 to +100
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rendering once here vs. every time the View method is called, to allow for proper updating

case spinner.TickMsg:
m.spinner, cmd = m.spinner.Update(msg)
}
Expand All @@ -85,31 +106,28 @@ func (m showSDKInstructionsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m showSDKInstructionsModel) View() string {
style := lipgloss.NewStyle().Border(lipgloss.NormalBorder(), true, false)
md, err := m.renderMarkdown()
if err != nil {
return fmt.Sprintf("error rendering instructions: %s", err)
}

if m.instructions == "" || m.sdkKey == "" {
return m.spinner.View() + fmt.Sprintf(" Fetching %s SDK instructions...", m.displayName)
}

return wordwrap.String(
fmt.Sprintf(
"Set up your application in your Default project & Test environment.\n\nHere are the steps to incorporate the LaunchDarkly %s SDK into your code. You should have everything you need to get started, including the flag from the previous step and your SDK key from your Test environment already embedded in the code!\n%s\n\n (press enter to continue)",
m.displayName,
style.Render(md),
),
0,
) + footerView(m.help.View(m.helpKeys), nil)
instructions := fmt.Sprintf("Set up your application in your Default project & Test environment.\n\nHere are the steps to incorporate the LaunchDarkly %s SDK into your code. You should have everything you need to get started, including the flag from the previous step and your SDK key from your Test environment already embedded in the code!\n", m.displayName)

return instructions + m.viewport.View() + "\n(press enter to continue)" + footerView(m.help.View(m.helpKeys), nil)
}

func (m showSDKInstructionsModel) renderMarkdown() (string, error) {
out, err := glamour.Render(m.instructions, "auto")
renderer, err := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
glamour.WithWordWrap(viewportWidth),
)
if err != nil {
return "", err
}

out, err := renderer.Render(m.instructions)
if err != nil {
return out, err
}

return out, nil
}
48 changes: 48 additions & 0 deletions vendor/github.com/charmbracelet/bubbles/viewport/keymap.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading