@@ -652,9 +652,10 @@ func (m *Model) handleKey(msg tea.KeyPressMsg) tea.Cmd {
652652 screen : spotSearchInput ,
653653 }
654654 } else {
655- m .netSearch .active = true
656- m .netSearch .query = ""
657- m .netSearch .soundcloud = false
655+ m .netSearch = netSearchState {
656+ active : true ,
657+ screen : netSearchInput ,
658+ }
658659 m .prevFocus = m .focus
659660 m .focus = focusNetSearch
660661 }
@@ -1027,7 +1028,9 @@ func (m *Model) handlePaste(content string) tea.Cmd {
10271028 }
10281029
10291030 if m .netSearch .active {
1030- m .netSearch .query += content
1031+ if m .netSearch .screen == netSearchInput {
1032+ m .netSearch .query += content
1033+ }
10311034 return nil
10321035 }
10331036
@@ -1118,32 +1121,37 @@ func (m *Model) handleSearchKey(msg tea.KeyPressMsg) tea.Cmd {
11181121 return nil
11191122}
11201123
1121- // handleNetSearchKey processes key presses while in net search mode .
1124+ // handleNetSearchKey dispatches key presses to the active net search screen .
11221125func (m * Model ) handleNetSearchKey (msg tea.KeyPressMsg ) tea.Cmd {
1123- switch msg .String () {
1124- case "ctrl+k" :
1126+ if msg .String () == "ctrl+k" {
11251127 m .openKeymap ()
11261128 return nil
11271129 }
1130+ switch m .netSearch .screen {
1131+ case netSearchInput :
1132+ return m .handleNetSearchInputKey (msg )
1133+ case netSearchResults :
1134+ return m .handleNetSearchResultsKey (msg )
1135+ }
1136+ return nil
1137+ }
11281138
1139+ // handleNetSearchInputKey handles text entry on the net search overlay.
1140+ func (m * Model ) handleNetSearchInputKey (msg tea.KeyPressMsg ) tea.Cmd {
11291141 switch msg .Code {
11301142 case tea .KeyEscape :
1131- m .netSearch .active = false
1132- m .focus = m .prevFocus
1143+ m .closeNetSearch ()
11331144
11341145 case tea .KeyEnter :
1135- var cmd tea.Cmd
1136- m .netSearch .active = false
1137- m .focus = m .prevFocus
1138- if strings .TrimSpace (m .netSearch .query ) != "" {
1139- prefix := "ytsearch1:"
1146+ if strings .TrimSpace (m .netSearch .query ) != "" && ! m .netSearch .loading {
1147+ prefix := "ytsearch10:"
11401148 if m .netSearch .soundcloud {
1141- prefix = "scsearch1 :"
1149+ prefix = "scsearch10 :"
11421150 }
1143- m .status .Show ("Queuing search..." , statusTTLShort )
1144- cmd = fetchNetSearchCmd (prefix + strings .TrimSpace (m .netSearch .query ))
1151+ m .netSearch .loading = true
1152+ m .netSearch .err = ""
1153+ return fetchNetSearchCmd (prefix + strings .TrimSpace (m .netSearch .query ))
11451154 }
1146- return cmd
11471155
11481156 case tea .KeyBackspace :
11491157 m .netSearch .query = removeLastRune (m .netSearch .query )
@@ -1156,7 +1164,50 @@ func (m *Model) handleNetSearchKey(msg tea.KeyPressMsg) tea.Cmd {
11561164 m .netSearch .query += msg .Text
11571165 }
11581166 }
1167+ return nil
1168+ }
1169+
1170+ // handleNetSearchResultsKey handles navigation through net search results.
1171+ func (m * Model ) handleNetSearchResultsKey (msg tea.KeyPressMsg ) tea.Cmd {
1172+ count := len (m .netSearch .results )
11591173
1174+ switch msg .String () {
1175+ case "up" , "k" :
1176+ if m .netSearch .cursor > 0 {
1177+ m .netSearch .cursor --
1178+ } else if count > 0 {
1179+ m .netSearch .cursor = count - 1
1180+ }
1181+ case "down" , "j" :
1182+ if m .netSearch .cursor < count - 1 {
1183+ m .netSearch .cursor ++
1184+ } else if count > 0 {
1185+ m .netSearch .cursor = 0
1186+ }
1187+ case "enter" :
1188+ if count > 0 && ! m .netSearch .loading {
1189+ track := m .netSearch .results [m .netSearch .cursor ]
1190+ m .closeNetSearch ()
1191+ return m .playTrackImmediate (track )
1192+ }
1193+ case "a" :
1194+ if count > 0 && ! m .netSearch .loading {
1195+ track := m .netSearch .results [m .netSearch .cursor ]
1196+ m .closeNetSearch ()
1197+ return m .appendTrack (track )
1198+ }
1199+ case "q" :
1200+ if count > 0 && ! m .netSearch .loading {
1201+ track := m .netSearch .results [m .netSearch .cursor ]
1202+ m .closeNetSearch ()
1203+ return m .queueTrackNext (track )
1204+ }
1205+ case "esc" , "backspace" :
1206+ m .netSearch .screen = netSearchInput
1207+ m .netSearch .results = nil
1208+ m .netSearch .cursor = 0
1209+ m .netSearch .err = ""
1210+ }
11601211 return nil
11611212}
11621213
0 commit comments