-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
e5f92cd
46e116a
b59bac8
12a1437
d926b31
5f52873
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -26,6 +29,7 @@ type showSDKInstructionsModel struct { | |
sdkKey string | ||
spinner spinner.Model | ||
url string | ||
viewport viewport.Model | ||
} | ||
|
||
func NewShowSDKInstructionsModel( | ||
|
@@ -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, | ||
|
@@ -50,8 +61,9 @@ func NewShowSDKInstructionsModel( | |
Back: BindingBack, | ||
Quit: BindingQuit, | ||
}, | ||
spinner: s, | ||
url: url, | ||
spinner: s, | ||
url: url, | ||
viewport: vp, | ||
} | ||
} | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rendering once here vs. every time the |
||
case spinner.TickMsg: | ||
m.spinner, cmd = m.spinner.Update(msg) | ||
} | ||
|
@@ -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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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