-
Notifications
You must be signed in to change notification settings - Fork 13
feat: Create choose SDK view #89
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| package quickstart | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
|
|
||
| "github.com/charmbracelet/bubbles/key" | ||
| "github.com/charmbracelet/bubbles/list" | ||
| tea "github.com/charmbracelet/bubbletea" | ||
| "github.com/charmbracelet/lipgloss" | ||
| ) | ||
|
|
||
| var ( | ||
| sdkStyle = lipgloss.NewStyle().PaddingLeft(4) | ||
| selectedSdkItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170")) | ||
| ) | ||
|
|
||
| type chooseSDKModel struct { | ||
| list list.Model | ||
| selectedSdk sdkDetail | ||
| } | ||
|
|
||
| func NewChooseSDKModel() tea.Model { | ||
| l := list.New(sdksToItems(), sdkDelegate{}, 30, 14) | ||
| l.Title = "Select your SDK:\n" | ||
| // reset title styles | ||
| l.Styles.Title = lipgloss.NewStyle() | ||
| l.Styles.TitleBar = lipgloss.NewStyle() | ||
| l.SetShowPagination(true) | ||
| l.SetShowStatusBar(false) | ||
| l.SetFilteringEnabled(false) // TODO: try to get filtering working | ||
| l.Paginator.PerPage = 5 | ||
|
|
||
| return chooseSDKModel{ | ||
| list: l, | ||
| } | ||
| } | ||
|
|
||
| func (m chooseSDKModel) Init() tea.Cmd { return nil } | ||
|
|
||
| func (m chooseSDKModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
| var cmd tea.Cmd | ||
| switch msg := msg.(type) { | ||
| case tea.KeyMsg: | ||
| switch { | ||
| case key.Matches(msg, keys.Enter): | ||
| i, ok := m.list.SelectedItem().(sdkDetail) | ||
| if ok { | ||
| m.selectedSdk = i | ||
| } | ||
| case key.Matches(msg, keys.Quit): | ||
| return m, tea.Quit | ||
| default: | ||
| m.list, cmd = m.list.Update(msg) | ||
| } | ||
| } | ||
|
|
||
| return m, cmd | ||
| } | ||
|
|
||
| func (m chooseSDKModel) View() string { | ||
| return m.list.View() | ||
| } | ||
|
|
||
| type sdkDetail struct { | ||
| DisplayName string `json:"displayName"` | ||
| SDKType string `json:"sdkType"` | ||
| } | ||
|
|
||
| func (s sdkDetail) FilterValue() string { return "" } | ||
|
|
||
| const clientSideSDK = "client" | ||
| const serverSideSDK = "server" | ||
|
|
||
| var SDKs = []sdkDetail{ | ||
| {DisplayName: "React", SDKType: clientSideSDK}, | ||
| {DisplayName: "Node.js (server-side)", SDKType: serverSideSDK}, | ||
| {DisplayName: "Python", SDKType: serverSideSDK}, | ||
| {DisplayName: "Java", SDKType: serverSideSDK}, | ||
| {DisplayName: ".NET (server-side)", SDKType: serverSideSDK}, | ||
| {DisplayName: "JavaScript", SDKType: clientSideSDK}, | ||
| {DisplayName: "Vue", SDKType: clientSideSDK}, | ||
| {DisplayName: "iOS", SDKType: clientSideSDK}, | ||
| {DisplayName: "Go", SDKType: serverSideSDK}, | ||
| {DisplayName: "Android", SDKType: clientSideSDK}, | ||
| {DisplayName: "React Native", SDKType: clientSideSDK}, | ||
| {DisplayName: "Ruby", SDKType: serverSideSDK}, | ||
| {DisplayName: "Flutter", SDKType: clientSideSDK}, | ||
| {DisplayName: ".NET (client-side)", SDKType: clientSideSDK}, | ||
| {DisplayName: "Erlang", SDKType: serverSideSDK}, | ||
| {DisplayName: "Rust", SDKType: serverSideSDK}, | ||
| {DisplayName: "Electron", SDKType: clientSideSDK}, | ||
| {DisplayName: "C/C++ (client-side)", SDKType: clientSideSDK}, | ||
| {DisplayName: "Roku", SDKType: clientSideSDK}, | ||
| {DisplayName: "Node.js (client-side)", SDKType: clientSideSDK}, | ||
| {DisplayName: "C/C++ (server-side)", SDKType: serverSideSDK}, | ||
| {DisplayName: "Lua", SDKType: serverSideSDK}, | ||
| {DisplayName: "Haskell", SDKType: serverSideSDK}, | ||
| {DisplayName: "Apex", SDKType: serverSideSDK}, | ||
| {DisplayName: "PHP", SDKType: serverSideSDK}, | ||
| } | ||
|
|
||
| func sdksToItems() []list.Item { | ||
| items := make([]list.Item, len(SDKs)) | ||
| for i, sdk := range SDKs { | ||
| items[i] = list.Item(sdk) | ||
| } | ||
|
|
||
| return items | ||
| } | ||
|
|
||
| type sdkDelegate struct{} | ||
|
|
||
| func (d sdkDelegate) Height() int { return 1 } | ||
| func (d sdkDelegate) Spacing() int { return 0 } | ||
| func (d sdkDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil } | ||
| func (d sdkDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) { | ||
| i, ok := listItem.(sdkDetail) | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| str := fmt.Sprintf("%d. %s", index+1, i.DisplayName) | ||
|
|
||
| fn := sdkStyle.Render | ||
| if index == m.Index() { | ||
| fn = func(s ...string) string { | ||
| return selectedSdkItemStyle.Render("> " + strings.Join(s, " ")) | ||
| } | ||
| } | ||
|
|
||
| fmt.Fprint(w, fn(str)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Simply flipping this on to
truedoesn't work as expected - probably due to the fact that the container model is still delegating to this nested model.I found this example of a simple list working with filtering, and got it to work on its own, but when I tried implementing those same changes in our delegated list model it still didn't work 😢