@@ -28,17 +28,20 @@ impl DynCommand for Command {
2828 . about ( "Get the current platform profile" ) )
2929 . subcommand ( clap:: Command :: new ( "list" )
3030 . about ( "List all available platform profiles" ) )
31- . subcommand ( clap:: Command :: new ( "cycle " )
31+ . subcommand ( clap:: Command :: new ( "next " )
3232 . about ( "Cycle to the next platform profile" ) )
33+ . subcommand ( clap:: Command :: new ( "prev" )
34+ . about ( "Cycle to the previous platform profile" ) )
3335 }
3436
3537 fn execute ( & self , m : & clap:: ArgMatches ) -> Result < ( ) > {
3638 match m. subcommand ( ) {
37- Some ( ( "set" , m) ) => self . profile_set ( m) ,
38- Some ( ( "get" , m) ) => self . profile_get ( m) ,
39+ Some ( ( "set" , m) ) => self . profile_set ( m) ,
40+ Some ( ( "get" , m) ) => self . profile_get ( m) ,
3941 Some ( ( "list" , m) ) => self . profile_list ( m) ,
40- Some ( ( "cycle" , m) ) => self . profile_cycle ( m) ,
41- _ => unreachable ! ( ) ,
42+ Some ( ( "next" , m) ) => self . profile_cycle_next ( m) ,
43+ Some ( ( "prev" , m) ) => self . profile_cycle_prev ( m) ,
44+ _ => unreachable ! ( ) ,
4245 }
4346 }
4447}
@@ -108,7 +111,7 @@ impl Command {
108111 Ok ( ( ) )
109112 }
110113
111- fn profile_cycle ( & self , m : & clap:: ArgMatches ) -> Result < ( ) > {
114+ fn profile_cycle_next ( & self , m : & clap:: ArgMatches ) -> Result < ( ) > {
112115 let dev = sys:: profile:: Device :: open ( )
113116 . context ( "Failed to open platform profile device" ) ?;
114117
@@ -123,11 +126,12 @@ impl Command {
123126 . context ( "Failed to get current platform profile" ) ?;
124127
125128 // Find the next profile in the list, wrapping around to the start
126- let next_profile = supported. iter ( )
127- . cycle ( )
128- . skip_while ( |& p| p != & current_profile)
129- . nth ( 1 )
130- . unwrap_or ( & supported[ 0 ] ) ;
129+ let current_index = supported. iter ( )
130+ . position ( |p| p == & current_profile)
131+ . unwrap_or ( 0 ) ;
132+
133+ let next_index = ( current_index + 1 ) % supported. len ( ) ;
134+ let next_profile = & supported[ next_index] ;
131135
132136 dev. set ( next_profile)
133137 . context ( "Failed to set platform profile" ) ?;
@@ -138,4 +142,36 @@ impl Command {
138142
139143 Ok ( ( ) )
140144 }
145+
146+ fn profile_cycle_prev ( & self , m : & clap:: ArgMatches ) -> Result < ( ) > {
147+ let dev = sys:: profile:: Device :: open ( )
148+ . context ( "Failed to open platform profile device" ) ?;
149+
150+ let supported = dev. get_supported ( )
151+ . context ( "Failed to get supported platform profiles" ) ?;
152+
153+ if supported. is_empty ( ) {
154+ anyhow:: bail!( "No platform profiles available" ) ;
155+ }
156+
157+ let current_profile = dev. get ( )
158+ . context ( "Failed to get current platform profile" ) ?;
159+
160+ // Find the previous profile in the list, wrapping around to the end
161+ let current_index = supported. iter ( )
162+ . position ( |p| p == & current_profile)
163+ . unwrap_or ( 0 ) ;
164+
165+ let prev_index = ( current_index + supported. len ( ) - 1 ) % supported. len ( ) ;
166+ let prev_profile = & supported[ prev_index] ;
167+
168+ dev. set ( prev_profile)
169+ . context ( "Failed to set platform profile" ) ?;
170+
171+ if !m. get_flag ( "quiet" ) {
172+ println ! ( "Platform profile cycled from '{current_profile}' to '{prev_profile}'" ) ;
173+ }
174+
175+ Ok ( ( ) )
176+ }
141177}
0 commit comments