@@ -36,44 +36,45 @@ impl Shopts {
3636 shopts
3737 }
3838
39- pub fn print_all ( & self ) {
40- let mut list = vec ! [ ] ;
41-
42- for opt in & self . opts {
43- let onoff = match opt. 1 {
44- true => "on" ,
45- false => "off" ,
46- } ;
47- if opt. 0 . len ( ) < 16 {
48- list. push ( format ! ( "{:16}{}" , opt. 0 , onoff) ) ;
49- } else {
50- list. push ( format ! ( "{}\t {}" , opt. 0 , onoff) ) ;
51- }
52- }
53-
54- list. sort ( ) ;
55- list. iter ( ) . for_each ( |e| println ! ( "{}" , e) ) ;
56- }
57-
58- pub fn print_if ( & self , onoff : bool ) {
59- let mut list = vec ! [ ] ;
60-
39+ pub fn format ( opt : & str , onoff : bool ) -> String {
6140 let onoff_str = match onoff {
6241 true => "on" ,
6342 false => "off" ,
6443 } ;
6544
66- for opt in & self . opts {
67- if * opt. 1 != onoff {
68- continue ;
69- }
45+ match opt. len ( ) < 16 {
46+ true => format ! ( "{:16}{}" , opt, onoff_str) ,
47+ false => format ! ( "{}\t {}" , opt, onoff_str) ,
48+ }
49+ }
7050
71- if opt. 0 . len ( ) < 16 {
72- list. push ( format ! ( "{:16}{}" , opt. 0 , onoff_str) ) ;
73- } else {
74- list. push ( format ! ( "{}\t {}" , opt. 0 , onoff_str) ) ;
75- }
51+ pub fn print_opt ( & self , opt : & str ) -> bool {
52+ match self . opts . get_key_value ( opt) {
53+ None => {
54+ eprintln ! ( "sush: shopt: {}: invalid shell option name" , opt) ;
55+ false
56+ } ,
57+ Some ( kv) => {
58+ println ! ( "{}" , Self :: format( kv. 0 , * kv. 1 ) ) ;
59+ true
60+ } ,
7661 }
62+ }
63+
64+ pub fn print_all ( & self ) {
65+ let mut list = self . opts . iter ( )
66+ . map ( |opt| Self :: format ( opt. 0 , * opt. 1 ) )
67+ . collect :: < Vec < String > > ( ) ;
68+
69+ list. sort ( ) ;
70+ list. iter ( ) . for_each ( |e| println ! ( "{}" , e) ) ;
71+ }
72+
73+ pub fn print_if ( & self , onoff : bool ) {
74+ let mut list = self . opts . iter ( )
75+ . filter ( |opt| * opt. 1 == onoff)
76+ . map ( |opt| Self :: format ( opt. 0 , * opt. 1 ) )
77+ . collect :: < Vec < String > > ( ) ;
7778
7879 list. sort ( ) ;
7980 list. iter ( ) . for_each ( |e| println ! ( "{}" , e) ) ;
@@ -83,7 +84,13 @@ impl Shopts {
8384 self . opts [ opt]
8485 }
8586
86- pub fn set ( & mut self , opt : & str , onoff : bool ) {
87+ pub fn set ( & mut self , opt : & str , onoff : bool ) -> bool {
88+ if ! self . opts . contains_key ( opt) {
89+ eprintln ! ( "sush: shopt: {}: invalid shell option name" , opt) ;
90+ return false ;
91+ }
92+
8793 self . opts . insert ( opt. to_string ( ) , onoff) ;
94+ true
8895 }
8996}
0 commit comments