@@ -11,6 +11,7 @@ pub enum Format {
1111 Unified ,
1212 Context ,
1313 Ed ,
14+ SideBySide ,
1415}
1516
1617#[ derive( Clone , Debug , Eq , PartialEq ) ]
@@ -24,6 +25,7 @@ pub struct Params {
2425 pub brief : bool ,
2526 pub expand_tabs : bool ,
2627 pub tabsize : usize ,
28+ pub width : usize ,
2729}
2830
2931impl Default for Params {
@@ -38,6 +40,7 @@ impl Default for Params {
3840 brief : false ,
3941 expand_tabs : false ,
4042 tabsize : 8 ,
43+ width : 130 ,
4144 }
4245 }
4346}
@@ -57,6 +60,7 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu
5760 let mut format = None ;
5861 let mut context = None ;
5962 let tabsize_re = Regex :: new ( r"^--tabsize=(?<num>\d+)$" ) . unwrap ( ) ;
63+ let width_re = Regex :: new ( r"--width=(?P<long>\d+)$" ) . unwrap ( ) ;
6064 while let Some ( param) = opts. next ( ) {
6165 let next_param = opts. peek ( ) ;
6266 if param == "--" {
@@ -101,6 +105,34 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu
101105 format = Some ( Format :: Ed ) ;
102106 continue ;
103107 }
108+ if param == "-y" || param == "--side-by-side" {
109+ if format. is_some ( ) && format != Some ( Format :: SideBySide ) {
110+ return Err ( "Conflicting output style option" . to_string ( ) ) ;
111+ }
112+ format = Some ( Format :: SideBySide ) ;
113+ continue ;
114+ }
115+ if width_re. is_match ( param. to_string_lossy ( ) . as_ref ( ) ) {
116+ let param = param. into_string ( ) . unwrap ( ) ;
117+ let width_str: & str = width_re
118+ . captures ( param. as_str ( ) )
119+ . unwrap ( )
120+ . name ( "long" )
121+ . unwrap ( )
122+ . as_str ( ) ;
123+
124+ params. width = match width_str. parse :: < usize > ( ) {
125+ Ok ( num) => {
126+ if num == 0 {
127+ return Err ( "invalid width «0»" . to_string ( ) ) ;
128+ }
129+
130+ num
131+ }
132+ Err ( _) => return Err ( format ! ( "invalid width «{width_str}»" ) ) ,
133+ } ;
134+ continue ;
135+ }
104136 if tabsize_re. is_match ( param. to_string_lossy ( ) . as_ref ( ) ) {
105137 // Because param matches the regular expression,
106138 // it is safe to assume it is valid UTF-8.
@@ -112,9 +144,16 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu
112144 . unwrap ( )
113145 . as_str ( ) ;
114146 params. tabsize = match tabsize_str. parse :: < usize > ( ) {
115- Ok ( num) => num,
147+ Ok ( num) => {
148+ if num == 0 {
149+ return Err ( "invalid tabsize «0»" . to_string ( ) ) ;
150+ }
151+
152+ num
153+ }
116154 Err ( _) => return Err ( format ! ( "invalid tabsize «{tabsize_str}»" ) ) ,
117155 } ;
156+
118157 continue ;
119158 }
120159 match match_context_diff_params ( & param, next_param, format) {
@@ -704,11 +743,11 @@ mod tests {
704743 executable: os( "diff" ) ,
705744 from: os( "foo" ) ,
706745 to: os( "bar" ) ,
707- tabsize: 0 ,
746+ tabsize: 1 ,
708747 ..Default :: default ( )
709748 } ) ,
710749 parse_params(
711- [ os( "diff" ) , os( "--tabsize=0 " ) , os( "foo" ) , os( "bar" ) ]
750+ [ os( "diff" ) , os( "--tabsize=1 " ) , os( "foo" ) , os( "bar" ) ]
712751 . iter( )
713752 . cloned( )
714753 . peekable( )
0 commit comments