@@ -15,12 +15,12 @@ use strip_ansi_escapes::strip_str;
1515use terminal_size:: { Height , Width , terminal_size} ;
1616use unicode_width:: UnicodeWidthStr ;
1717
18- use crate :: Params ;
1918use crate :: structs:: { FileInfo , NameStyle } ;
2019use crate :: utils;
2120use crate :: utils:: color:: { LongFormatColorLevel , long_format_color_level} ;
2221use crate :: utils:: file:: check_display_name;
2322use crate :: utils:: time:: { DAY , MONTH , WEEK , YEAR } ;
23+ use crate :: { Params , structs:: PermissionDisplay } ;
2424
2525const SHORT_CELL_PADDING : usize = 2 ;
2626const LARGE_SIZE_BYTES : u64 = 1024 * 1024 ;
@@ -79,12 +79,9 @@ fn build_long_format_table_with_name_prefixes<'a>(
7979 let ( display_size, units) =
8080 utils:: format:: show_size ( info. size , size_scale) ;
8181
82- let mut row_cells = Vec :: with_capacity ( 9 ) ;
82+ let mut row_cells = Vec :: with_capacity ( 10 ) ;
8383
84- row_cells. push ( Cell :: new ( & format ! (
85- "{} " ,
86- long_permission_text( info, params)
87- ) ) ) ;
84+ append_permission_cells ( & mut row_cells, info, params, color_level) ;
8885 row_cells. push ( Cell :: new ( & info. nlink . to_string ( ) ) ) ;
8986 row_cells. push ( Cell :: new ( & format ! ( " {}" , info. user. cyan( ) ) ) ) ;
9087 row_cells. push ( Cell :: new ( & format ! ( "{} " , info. group. green( ) ) ) ) ;
@@ -127,16 +124,76 @@ fn build_long_format_table_with_name_prefixes<'a>(
127124 table
128125}
129126
130- fn long_permission_text ( info : & FileInfo , params : & Params ) -> String {
127+ fn append_permission_cells (
128+ row_cells : & mut Vec < Cell > ,
129+ info : & FileInfo ,
130+ params : & Params ,
131+ color_level : LongFormatColorLevel ,
132+ ) {
133+ match params. permissions {
134+ PermissionDisplay :: Symbolic => {
135+ row_cells. push ( Cell :: new ( & format ! (
136+ "{} " ,
137+ long_permission_text( info, params)
138+ ) ) ) ;
139+ }
140+ PermissionDisplay :: Octal => {
141+ row_cells. push ( Cell :: new ( & format ! (
142+ "{} {} " ,
143+ long_file_type_text( info, params) ,
144+ long_octal_permission_text( info, params, color_level)
145+ ) ) ) ;
146+ }
147+ PermissionDisplay :: Both => {
148+ row_cells. push ( Cell :: new ( & long_permission_text ( info, params) ) ) ;
149+ row_cells. push ( Cell :: new ( & format ! (
150+ "{} " ,
151+ long_octal_permission_text( info, params, color_level)
152+ ) ) ) ;
153+ }
154+ PermissionDisplay :: None => { }
155+ }
156+ }
157+
158+ fn long_file_type_text ( info : & FileInfo , params : & Params ) -> String {
131159 if !params. permission_colors {
132- return format ! ( "{}{}" , info. file_type, info . mode ) ;
160+ return info. file_type . clone ( ) ;
133161 }
134162
135- let mut output =
136- String :: with_capacity ( info. file_type . len ( ) + info. mode . len ( ) ) ;
163+ let mut output = String :: with_capacity ( info. file_type . len ( ) ) ;
137164 for value in info. file_type . chars ( ) {
138165 write_file_type_char ( & mut output, value) ;
139166 }
167+ output
168+ }
169+
170+ fn long_octal_permission_text (
171+ info : & FileInfo ,
172+ params : & Params ,
173+ color_level : LongFormatColorLevel ,
174+ ) -> String {
175+ let text = utils:: format:: mode_to_octal ( info. mode_bits ) ;
176+ if !params. permission_colors {
177+ return text;
178+ }
179+
180+ match color_level {
181+ LongFormatColorLevel :: Truecolor => text. rgb ( 238 , 204 , 92 ) . to_string ( ) ,
182+ LongFormatColorLevel :: Ansi256 => {
183+ format ! ( "\x1b [38;5;221m{text}\x1b [0m" )
184+ }
185+ LongFormatColorLevel :: Named => text. yellow ( ) . dim ( ) . to_string ( ) ,
186+ LongFormatColorLevel :: None => text,
187+ }
188+ }
189+
190+ fn long_permission_text ( info : & FileInfo , params : & Params ) -> String {
191+ if !params. permission_colors {
192+ return format ! ( "{}{}" , info. file_type, info. mode) ;
193+ }
194+
195+ let mut output = long_file_type_text ( info, params) ;
196+ output. reserve ( info. mode . len ( ) ) ;
140197 for value in info. mode . chars ( ) {
141198 write_permission_char ( & mut output, value) ;
142199 }
0 commit comments