@@ -20,6 +20,7 @@ pub trait Column {
2020 ) -> String ;
2121 fn display_unit ( & self , align : & ConfigColumnAlign ) -> String ;
2222 fn display_content ( & self , pid : i32 , align : & ConfigColumnAlign ) -> Option < String > ;
23+ fn display_json ( & self , pid : i32 ) -> String ;
2324 fn find_partial ( & self , pid : i32 , keyword : & str , content_to_lowercase : bool ) -> bool ;
2425 fn find_exact ( & self , pid : i32 , keyword : & str , content_to_lowercase : bool ) -> bool ;
2526 fn sorted_pid ( & self , order : & ConfigSortOrder ) -> Vec < i32 > ;
@@ -33,6 +34,7 @@ pub trait Column {
3334 ) ;
3435 fn update_width ( & mut self , pid : i32 , max_width : Option < usize > ) ;
3536 fn get_width ( & self ) -> usize ;
37+ fn is_numeric ( & self ) -> bool ;
3638}
3739
3840#[ macro_export]
@@ -85,6 +87,30 @@ macro_rules! column_default_display_content {
8587 } ;
8688}
8789
90+ #[ macro_export]
91+ macro_rules! column_default_display_json {
92+ ( ) => {
93+ fn display_json( & self , pid: i32 ) -> String {
94+ let value = if self . is_numeric( ) {
95+ self . raw_contents
96+ . get( & pid)
97+ . map( |x| x. to_string( ) )
98+ . unwrap_or( "" . to_string( ) )
99+ } else {
100+ let value = self
101+ . fmt_contents
102+ . get( & pid)
103+ . map( |x| x. clone( ) )
104+ . unwrap_or( "" . to_string( ) ) ;
105+ let value = value. replace( "\\ " , "\\ \\ " ) ;
106+ let value = value. replace( "\" " , "\\ \" " ) ;
107+ format!( "\" {}\" " , value)
108+ } ;
109+ format!( "\" {}\" : {}" , self . header, value)
110+ }
111+ } ;
112+ }
113+
88114#[ macro_export]
89115macro_rules! column_default_find_partial {
90116 ( ) => {
@@ -201,18 +227,29 @@ macro_rules! column_default_get_width {
201227 } ;
202228}
203229
230+ #[ macro_export]
231+ macro_rules! column_default_is_numeric {
232+ ( $x: expr) => {
233+ fn is_numeric( & self ) -> bool {
234+ $x
235+ }
236+ } ;
237+ }
238+
204239#[ macro_export]
205240macro_rules! column_default {
206- ( $x: ty) => {
241+ ( $x: ty, $y : expr ) => {
207242 $crate:: column_default_display_header!( ) ;
208243 $crate:: column_default_display_unit!( ) ;
209244 $crate:: column_default_display_content!( ) ;
245+ $crate:: column_default_display_json!( ) ;
210246 $crate:: column_default_find_partial!( ) ;
211247 $crate:: column_default_find_exact!( ) ;
212248 $crate:: column_default_sorted_pid!( $x) ;
213249 $crate:: column_default_apply_visible!( ) ;
214250 $crate:: column_default_reset_width!( ) ;
215251 $crate:: column_default_update_width!( ) ;
216252 $crate:: column_default_get_width!( ) ;
253+ $crate:: column_default_is_numeric!( $y) ;
217254 } ;
218255}
0 commit comments