Skip to content

Commit

Permalink
Merge pull request #53 from JunNishimura/#50
Browse files Browse the repository at this point in the history
play music on chatify
  • Loading branch information
JunNishimura authored Aug 20, 2023
2 parents 5251e56 + 2036843 commit 7b63a05
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
1 change: 1 addition & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func NewAuth(cfg *config.Config) *spotifyauth.Authenticator {
spotifyauth.WithScopes(
spotifyauth.ScopeUserReadPrivate,
spotifyauth.ScopeUserReadPlaybackState,
spotifyauth.ScopeUserModifyPlaybackState,
),
)
}
Expand Down
3 changes: 2 additions & 1 deletion ui/hey/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s sessionState) String() string {
type Item struct {
album string
artists []string
url string
uri spotify.URI
}

func (i Item) Title() string { return i.album }
Expand All @@ -47,6 +47,7 @@ type Model struct {
state sessionState
textInput textinput.Model
list list.Model
selectedItem Item
cfg *config.Config
user *model.User
spotifyClient *spotify.Client
Expand Down
5 changes: 0 additions & 5 deletions ui/hey/style/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ package style

import "github.com/charmbracelet/lipgloss"

var (
List = lipgloss.NewStyle().
Margin(1, 2)
)

func GetNormal(width, height int) lipgloss.Style {
return lipgloss.NewStyle().
Width(width).
Expand Down
36 changes: 26 additions & 10 deletions ui/hey/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,23 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.state = chatView
}
case "enter":
answer := m.textInput.Value()
m.chatCompMessages = append(m.chatCompMessages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: answer,
})
m.conversation = append(m.conversation, answer)
if m.state == chatView {
answer := m.textInput.Value()
m.chatCompMessages = append(m.chatCompMessages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: answer,
})
m.conversation = append(m.conversation, answer)

m.textInput.Reset()
m.textInput.Reset()

return m, m.generate
return m, m.generate
} else {
if selectedItem, ok := m.list.SelectedItem().(Item); ok {
m.selectedItem = selectedItem
return m, m.playMusic
}
}
}
case loadConfigMsg:
m.cfg = msg.cfg
Expand Down Expand Up @@ -325,13 +332,22 @@ func (m Model) recommend() tea.Msg {
artists = append(artists, artist.Name)
}

item := &Item{
item := Item{
album: track.Album.Name,
artists: artists,
url: track.ExternalURLs["spotify"],
uri: track.URI,
}
items = append(items, item)
}

return recommendMsg{items}
}

func (m Model) playMusic() tea.Msg {
if err := m.spotifyClient.PlayOpt(m.ctx, &spotify.PlayOptions{
URIs: []spotify.URI{m.selectedItem.uri},
}); err != nil {
return errMsg{err}
}
return nil
}
4 changes: 2 additions & 2 deletions ui/hey/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (m Model) View() string {
// window size adjustmen
wholeWidth := m.window.Width - 4
halfWidth := wholeWidth / 2
height := m.window.Height - 10
height := m.window.Height - 5
if m.state == chatView {
s += lipgloss.Place(m.window.Width, m.window.Height, lipgloss.Center, lipgloss.Center,
lipgloss.JoinHorizontal(
Expand Down Expand Up @@ -42,5 +42,5 @@ func (m Model) chatView() string {
}

func (m Model) recommendationView() string {
return style.List.Render(m.list.View())
return m.list.View()
}

0 comments on commit 7b63a05

Please sign in to comment.