@@ -4,8 +4,8 @@ extern crate clap;
4
4
#[ macro_use]
5
5
extern crate error_chain;
6
6
7
- use clap:: { Arg , ArgMatches , App , AppSettings } ;
8
- use ronor:: { Sonos , Favorite , Group , Household , Player , Playlist , PlayModes } ;
7
+ use clap:: { App , AppSettings , Arg , ArgMatches } ;
8
+ use ronor:: { Favorite , Group , Household , PlayModes , Player , Playlist , Sonos } ;
9
9
use std:: convert:: TryFrom ;
10
10
use xdg:: BaseDirectories ;
11
11
@@ -58,10 +58,28 @@ macro_rules! subcmds {
58
58
}
59
59
}
60
60
61
- subcmds ! ( init, login,
62
- get_favorites, get_playlist, get_playlists, get_volume, inventory, load_audio_clip,
63
- load_favorite, load_home_theater_playback, load_line_in, load_playlist,
64
- modify_group, now_playing, pause, play, seek, set_mute, set_volume, skip, speak,
61
+ subcmds ! (
62
+ init,
63
+ login,
64
+ get_favorites,
65
+ get_playlist,
66
+ get_playlists,
67
+ get_volume,
68
+ inventory,
69
+ load_audio_clip,
70
+ load_favorite,
71
+ load_home_theater_playback,
72
+ load_line_in,
73
+ load_playlist,
74
+ modify_group,
75
+ now_playing,
76
+ pause,
77
+ play,
78
+ seek,
79
+ set_mute,
80
+ set_volume,
81
+ skip,
82
+ speak,
65
83
toggle_play_pause
66
84
) ;
67
85
@@ -72,22 +90,39 @@ fn build() -> App<'static, 'static> {
72
90
. about ( "Sonos smart speaker controller" )
73
91
. setting ( AppSettings :: ArgRequiredElseHelp )
74
92
. subcommands ( build_subcmds ( ) )
75
- . subcommand ( App :: new ( "get-groups" ) . setting ( AppSettings :: Hidden )
76
- . about ( "Get list of groups" ) )
77
- . subcommand ( App :: new ( "get-players" ) . setting ( AppSettings :: Hidden )
78
- . about ( "Get list of players" ) )
79
- . subcommand ( App :: new ( "get-playback-status" ) . setting ( AppSettings :: Hidden )
80
- . about ( "Get playback status (DEBUG)" )
81
- . arg ( Arg :: with_name ( "GROUP" ) ) )
82
- . subcommand ( App :: new ( "get-metadata-status" ) . setting ( AppSettings :: Hidden )
83
- . about ( "Get playback status (DEBUG)" )
84
- . arg ( Arg :: with_name ( "GROUP" ) ) )
85
- . subcommand ( App :: new ( "completions" ) . setting ( AppSettings :: Hidden )
86
- . about ( "Generates completion scripts for your shell" )
87
- . arg ( Arg :: with_name ( "SHELL" )
88
- . required ( true )
89
- . possible_values ( & [ "bash" , "fish" , "zsh" ] )
90
- . help ( "The shell to generate the script for" ) ) )
93
+ . subcommand (
94
+ App :: new ( "get-groups" )
95
+ . setting ( AppSettings :: Hidden )
96
+ . about ( "Get list of groups" )
97
+ )
98
+ . subcommand (
99
+ App :: new ( "get-players" )
100
+ . setting ( AppSettings :: Hidden )
101
+ . about ( "Get list of players" )
102
+ )
103
+ . subcommand (
104
+ App :: new ( "get-playback-status" )
105
+ . setting ( AppSettings :: Hidden )
106
+ . about ( "Get playback status (DEBUG)" )
107
+ . arg ( Arg :: with_name ( "GROUP" ) )
108
+ )
109
+ . subcommand (
110
+ App :: new ( "get-metadata-status" )
111
+ . setting ( AppSettings :: Hidden )
112
+ . about ( "Get playback status (DEBUG)" )
113
+ . arg ( Arg :: with_name ( "GROUP" ) )
114
+ )
115
+ . subcommand (
116
+ App :: new ( "completions" )
117
+ . setting ( AppSettings :: Hidden )
118
+ . about ( "Generates completion scripts for your shell" )
119
+ . arg (
120
+ Arg :: with_name ( "SHELL" )
121
+ . required ( true )
122
+ . possible_values ( & [ "bash" , "fish" , "zsh" ] )
123
+ . help ( "The shell to generate the script for" )
124
+ )
125
+ )
91
126
}
92
127
93
128
quick_main ! ( run) ;
@@ -101,17 +136,15 @@ fn run() -> Result<()> {
101
136
let shell = matches. value_of ( "SHELL" ) . unwrap ( ) ;
102
137
build ( ) . gen_completions_to (
103
138
"ronor" ,
104
- shell. parse :: < > ( ) . unwrap ( ) ,
139
+ shell. parse ( ) . unwrap ( ) ,
105
140
& mut std:: io:: stdout ( )
106
141
) ;
107
142
Ok ( ( ) )
108
- } ,
109
- ( "get-playback-status" , Some ( matches) ) =>
110
- get_playback_status ( & mut sonos, matches) ,
111
- ( "get-groups" , Some ( matches) ) => get_groups ( & mut sonos, matches) ,
112
- ( "get-metadata-status" , Some ( matches) ) =>
113
- get_metadata_status ( & mut sonos, matches) ,
114
- ( "get-players" , Some ( matches) ) => get_players ( & mut sonos, matches) ,
143
+ }
144
+ ( "get-playback-status" , Some ( matches) ) => get_playback_status ( & mut sonos, matches) ,
145
+ ( "get-groups" , Some ( matches) ) => get_groups ( & mut sonos, matches) ,
146
+ ( "get-metadata-status" , Some ( matches) ) => get_metadata_status ( & mut sonos, matches) ,
147
+ ( "get-players" , Some ( matches) ) => get_players ( & mut sonos, matches) ,
115
148
( cmd, Some ( matches) ) => sonos. run_subcmd ( cmd, matches) ,
116
149
_ => unreachable ! ( )
117
150
}
@@ -120,11 +153,17 @@ fn run() -> Result<()> {
120
153
fn get_playback_status ( sonos : & mut Sonos , matches : & ArgMatches ) -> Result < ( ) > {
121
154
let mut found = false ;
122
155
for household in sonos. get_households ( ) ?. iter ( ) {
123
- for group in sonos. get_groups ( & household) ?. groups . iter ( ) . filter ( |group|
124
- matches. value_of ( "GROUP" ) . map_or ( true , |name| name == group. name )
125
- ) {
156
+ for group in sonos. get_groups ( & household) ?. groups . iter ( ) . filter ( |group| {
157
+ matches
158
+ . value_of ( "GROUP" )
159
+ . map_or ( true , |name| name == group. name )
160
+ } ) {
126
161
found = true ;
127
- println ! ( "{:?} => {:#?}" , group. name, sonos. get_playback_status( & group) ?) ;
162
+ println ! (
163
+ "{:?} => {:#?}" ,
164
+ group. name,
165
+ sonos. get_playback_status( & group) ?
166
+ ) ;
128
167
}
129
168
}
130
169
if !found {
@@ -138,11 +177,17 @@ fn get_playback_status(sonos: &mut Sonos, matches: &ArgMatches) -> Result<()> {
138
177
fn get_metadata_status ( sonos : & mut Sonos , matches : & ArgMatches ) -> Result < ( ) > {
139
178
let mut found = false ;
140
179
for household in sonos. get_households ( ) ?. iter ( ) {
141
- for group in sonos. get_groups ( & household) ?. groups . iter ( ) . filter ( |group|
142
- matches. value_of ( "GROUP" ) . map_or ( true , |name| name == group. name )
143
- ) {
180
+ for group in sonos. get_groups ( & household) ?. groups . iter ( ) . filter ( |group| {
181
+ matches
182
+ . value_of ( "GROUP" )
183
+ . map_or ( true , |name| name == group. name )
184
+ } ) {
144
185
found = true ;
145
- println ! ( "{:?} => {:#?}" , group. name, sonos. get_metadata_status( & group) ?) ;
186
+ println ! (
187
+ "{:?} => {:#?}" ,
188
+ group. name,
189
+ sonos. get_metadata_status( & group) ?
190
+ ) ;
146
191
}
147
192
}
148
193
if !found {
@@ -173,26 +218,41 @@ fn get_players(sonos: &mut Sonos, _matches: &ArgMatches) -> Result<()> {
173
218
174
219
fn household_arg ( ) -> Arg < ' static , ' static > {
175
220
Arg :: with_name ( "HOUSEHOLD" )
176
- . long ( "household" ) . takes_value ( true ) . value_name ( "INDEX" )
221
+ . long ( "household" )
222
+ . takes_value ( true )
223
+ . value_name ( "INDEX" )
177
224
. help ( "Optional 0-based household index" )
178
225
}
179
226
180
227
fn play_modes_args ( ) -> Vec < Arg < ' static , ' static > > {
181
- vec ! [ Arg :: with_name( "REPEAT" ) . short( "r" ) . long( "repeat" ) ,
182
- Arg :: with_name( "REPEAT_ONE" ) . short( "o" ) . long( "repeat-one" ) ,
183
- Arg :: with_name( "CROSSFADE" ) . short( "c" ) . long( "crossfade" )
184
- . help( "Do crossfade between tracks" ) ,
185
- Arg :: with_name( "SHUFFLE" ) . short( "s" ) . long( "shuffle" )
186
- . help( "Shuffle the tracks" )
228
+ vec ! [
229
+ Arg :: with_name( "REPEAT" ) . short( "r" ) . long( "repeat" ) ,
230
+ Arg :: with_name( "REPEAT_ONE" ) . short( "o" ) . long( "repeat-one" ) ,
231
+ Arg :: with_name( "CROSSFADE" )
232
+ . short( "c" )
233
+ . long( "crossfade" )
234
+ . help( "Do crossfade between tracks" ) ,
235
+ Arg :: with_name( "SHUFFLE" )
236
+ . short( "s" )
237
+ . long( "shuffle" )
238
+ . help( "Shuffle the tracks" ) ,
187
239
]
188
240
}
189
241
190
242
trait ArgMatchesExt {
191
243
fn household ( self : & Self , sonos : & mut Sonos ) -> Result < Household > ;
192
- fn favorite ( self : & Self , sonos : & mut Sonos , household : & Household ) -> Result < Favorite > ;
244
+ fn favorite (
245
+ self : & Self ,
246
+ sonos : & mut Sonos ,
247
+ household : & Household
248
+ ) -> Result < Favorite > ;
193
249
fn group < ' a > ( self : & Self , groups : & ' a [ Group ] ) -> Result < & ' a Group > ;
194
250
fn player < ' a > ( self : & Self , players : & ' a [ Player ] ) -> Result < & ' a Player > ;
195
- fn playlist ( self : & Self , sonos : & mut Sonos , household : & Household ) -> Result < Playlist > ;
251
+ fn playlist (
252
+ self : & Self ,
253
+ sonos : & mut Sonos ,
254
+ household : & Household
255
+ ) -> Result < Playlist > ;
196
256
fn play_modes ( self : & Self ) -> Option < PlayModes > ;
197
257
}
198
258
@@ -205,31 +265,41 @@ impl ArgMatchesExt for ArgMatches<'_> {
205
265
_ => match self . value_of ( "HOUSEHOLD" ) {
206
266
None => Err ( "Multiple households found" . into ( ) ) ,
207
267
Some ( index) => {
208
- let index = index. parse :: < usize > ( ) . chain_err ( || "Invalid household index" ) ?;
268
+ let index = index
269
+ . parse :: < usize > ( )
270
+ . chain_err ( || "Invalid household index" ) ?;
209
271
for ( n, household) in households. into_iter ( ) . enumerate ( ) {
210
272
if n == index {
211
- return Ok ( household)
273
+ return Ok ( household) ;
212
274
}
213
275
}
214
276
Err ( "Household out of range" . into ( ) )
215
277
}
216
278
}
217
279
}
218
280
}
219
- fn favorite ( self : & Self , sonos : & mut Sonos , household : & Household ) -> Result < Favorite > {
281
+ fn favorite (
282
+ self : & Self ,
283
+ sonos : & mut Sonos ,
284
+ household : & Household
285
+ ) -> Result < Favorite > {
220
286
let favorite_name = self . value_of ( "FAVORITE" ) . unwrap ( ) ;
221
287
for favorite in sonos. get_favorites ( household) ?. items . into_iter ( ) {
222
288
if favorite. name == favorite_name {
223
- return Ok ( favorite)
289
+ return Ok ( favorite) ;
224
290
}
225
291
}
226
292
Err ( "Playlist not found" . into ( ) )
227
293
}
228
- fn playlist ( self : & Self , sonos : & mut Sonos , household : & Household ) -> Result < Playlist > {
294
+ fn playlist (
295
+ self : & Self ,
296
+ sonos : & mut Sonos ,
297
+ household : & Household
298
+ ) -> Result < Playlist > {
229
299
let playlist_name = self . value_of ( "PLAYLIST" ) . unwrap ( ) ;
230
300
for playlist in sonos. get_playlists ( household) ?. playlists . into_iter ( ) {
231
301
if playlist. name == playlist_name {
232
- return Ok ( playlist)
302
+ return Ok ( playlist) ;
233
303
}
234
304
}
235
305
Err ( "Playlist not found" . into ( ) )
@@ -258,7 +328,12 @@ impl ArgMatchesExt for ArgMatches<'_> {
258
328
let crossfade = self . is_present ( "CROSSFADE" ) ;
259
329
let shuffle = self . is_present ( "SHUFFLE" ) ;
260
330
if repeat || repeat_one || crossfade || shuffle {
261
- Some ( PlayModes { repeat, repeat_one, crossfade, shuffle } )
331
+ Some ( PlayModes {
332
+ repeat,
333
+ repeat_one,
334
+ crossfade,
335
+ shuffle
336
+ } )
262
337
} else {
263
338
None
264
339
}
0 commit comments