1+ use crate :: utils:: map_join;
2+ use html_escape:: decode_html_entities;
13pub use rspotify:: model:: {
24 AlbumId , ArtistId , EpisodeId , Id , PlayableId , PlaylistId , ShowId , TrackId , UserId ,
35} ;
4-
5- use crate :: utils:: map_join;
6- use html_escape:: decode_html_entities;
76use serde:: { Deserialize , Serialize } ;
87use std:: borrow:: Cow ;
8+ use std:: fmt:: Write ;
99
1010#[ derive( Serialize , Clone , Debug ) ]
1111#[ serde( untagged) ]
@@ -234,22 +234,26 @@ impl Context {
234234 ref album,
235235 ref tracks,
236236 } => {
237+ let album_length = play_time ( tracks) ;
237238 format ! (
238- "{} | {} | {} songs" ,
239+ "{} | {} | {} songs | {} " ,
239240 album. name,
240241 album. release_date,
241- tracks. len( )
242+ tracks. len( ) ,
243+ album_length,
242244 )
243245 }
244246 Context :: Playlist {
245247 ref playlist,
246248 tracks,
247249 } => {
250+ let playlist_length = play_time ( tracks) ;
248251 format ! (
249- "{} | {} | {} songs" ,
252+ "{} | {} | {} songs | {} " ,
250253 playlist. name,
251254 playlist. owner. 0 ,
252- tracks. len( )
255+ tracks. len( ) ,
256+ playlist_length,
253257 )
254258 }
255259 Context :: Artist { ref artist, .. } => artist. name . to_string ( ) ,
@@ -262,6 +266,31 @@ impl Context {
262266 }
263267}
264268
269+ fn play_time ( tracks : & [ Track ] ) -> String {
270+ let duration = tracks
271+ . iter ( )
272+ . map ( |t| t. duration )
273+ . sum :: < std:: time:: Duration > ( ) ;
274+
275+ let mut output = String :: new ( ) ;
276+
277+ let seconds = duration. as_secs ( ) % 60 ;
278+ let minutes = ( duration. as_secs ( ) / 60 ) % 60 ;
279+ let hours = duration. as_secs ( ) / 3600 ;
280+
281+ if hours > 0 {
282+ write ! ( output, "{hours}h " ) . unwrap ( ) ;
283+ }
284+
285+ if minutes > 0 {
286+ write ! ( output, "{minutes}m " ) . unwrap ( ) ;
287+ }
288+
289+ write ! ( output, "{seconds}s" ) . unwrap ( ) ;
290+
291+ output
292+ }
293+
265294impl ContextId {
266295 pub fn uri ( & self ) -> String {
267296 match self {
0 commit comments