6
6
#include "modules/swap/swap.h"
7
7
#include "util/stringUtils.h"
8
8
9
- void ffPrintSwap (FFSwapOptions * options )
9
+ void printSwap (FFSwapOptions * options , uint8_t index , FFSwapResult * storage )
10
10
{
11
- FFSwapResult storage = {};
12
- const char * error = ffDetectSwap (& storage );
11
+ FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate ();
13
12
14
- if ( error )
13
+ if ( options -> moduleArgs . key . length == 0 )
15
14
{
16
- ffPrintError (FF_SWAP_MODULE_NAME , 0 , & options -> moduleArgs , FF_PRINT_TYPE_DEFAULT , "%s" , error );
17
- return ;
15
+ if (storage -> name .length > 0 )
16
+ ffStrbufSetF (& key , "%s (%s)" , FF_SWAP_MODULE_NAME , storage -> name .chars );
17
+ else
18
+ ffStrbufSetS (& key , FF_SWAP_MODULE_NAME );
19
+ }
20
+ else
21
+ {
22
+ ffStrbufClear (& key );
23
+ FF_PARSE_FORMAT_STRING_CHECKED (& key , & options -> moduleArgs .key , ((FFformatarg []) {
24
+ FF_FORMAT_ARG (index , "index" ),
25
+ FF_FORMAT_ARG (storage -> name , "name" ),
26
+ FF_FORMAT_ARG (options -> moduleArgs .keyIcon , "icon" ),
27
+ }));
18
28
}
19
29
20
30
FF_STRBUF_AUTO_DESTROY usedPretty = ffStrbufCreate ();
21
- ffParseSize (storage . bytesUsed , & usedPretty );
31
+ ffParseSize (storage -> bytesUsed , & usedPretty );
22
32
23
33
FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate ();
24
- ffParseSize (storage . bytesTotal , & totalPretty );
34
+ ffParseSize (storage -> bytesTotal , & totalPretty );
25
35
26
- double percentage = storage . bytesTotal == 0
36
+ double percentage = storage -> bytesTotal == 0
27
37
? 0
28
- : (double ) storage . bytesUsed / (double ) storage . bytesTotal * 100.0 ;
38
+ : (double ) storage -> bytesUsed / (double ) storage -> bytesTotal * 100.0 ;
29
39
30
40
FFPercentageTypeFlags percentType = options -> percent .type == 0 ? instance .config .display .percentType : options -> percent .type ;
31
41
if (options -> moduleArgs .outputFormat .length == 0 )
32
42
{
33
- ffPrintLogoAndKey (FF_SWAP_MODULE_NAME , 0 , & options -> moduleArgs , FF_PRINT_TYPE_DEFAULT );
43
+ ffPrintLogoAndKey (key . chars , 0 , & options -> moduleArgs , FF_PRINT_TYPE_NO_CUSTOM_KEY );
34
44
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate ();
35
45
36
- if (storage . bytesTotal == 0 )
46
+ if (storage -> bytesTotal == 0 )
37
47
{
38
48
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT )
39
49
{
@@ -71,15 +81,56 @@ void ffPrintSwap(FFSwapOptions* options)
71
81
FF_STRBUF_AUTO_DESTROY percentageBar = ffStrbufCreate ();
72
82
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT )
73
83
ffPercentAppendBar (& percentageBar , percentage , options -> percent , & options -> moduleArgs );
74
- FF_PRINT_FORMAT_CHECKED (FF_SWAP_MODULE_NAME , 0 , & options -> moduleArgs , FF_PRINT_TYPE_DEFAULT , ((FFformatarg []){
84
+ FF_PRINT_FORMAT_CHECKED (key . chars , index , & options -> moduleArgs , FF_PRINT_TYPE_NO_CUSTOM_KEY , ((FFformatarg []){
75
85
FF_FORMAT_ARG (usedPretty , "used" ),
76
86
FF_FORMAT_ARG (totalPretty , "total" ),
77
87
FF_FORMAT_ARG (percentageNum , "percentage" ),
78
88
FF_FORMAT_ARG (percentageBar , "percentage-bar" ),
89
+ FF_FORMAT_ARG (storage -> name , "name" ),
79
90
}));
80
91
}
81
92
}
82
93
94
+ void ffPrintSwap (FFSwapOptions * options )
95
+ {
96
+ FF_LIST_AUTO_DESTROY result = ffListCreate (sizeof (FFSwapResult ));
97
+ const char * error = ffDetectSwap (& result );
98
+
99
+ if (error )
100
+ {
101
+ ffPrintError (FF_SWAP_MODULE_NAME , 0 , & options -> moduleArgs , FF_PRINT_TYPE_DEFAULT , "%s" , error );
102
+ return ;
103
+ }
104
+
105
+ if (options -> separate )
106
+ {
107
+ uint8_t index = 0 ;
108
+ FF_LIST_FOR_EACH (FFSwapResult , storage , result )
109
+ {
110
+ ++ index ;
111
+ printSwap (options , index , storage );
112
+ }
113
+ }
114
+ else
115
+ {
116
+ FFSwapResult total = {
117
+ .name = ffStrbufCreate (),
118
+ };
119
+ FF_LIST_FOR_EACH (FFSwapResult , storage , result )
120
+ {
121
+ total .bytesUsed += storage -> bytesUsed ;
122
+ total .bytesTotal += storage -> bytesTotal ;
123
+ }
124
+ printSwap (options , 0 , & total );
125
+ ffStrbufDestroy (& total .name );
126
+ }
127
+
128
+ FF_LIST_FOR_EACH (FFSwapResult , storage , result )
129
+ {
130
+ ffStrbufDestroy (& storage -> name );
131
+ }
132
+ }
133
+
83
134
bool ffParseSwapCommandOptions (FFSwapOptions * options , const char * key , const char * value )
84
135
{
85
136
const char * subKey = ffOptionTestPrefix (key , FF_SWAP_MODULE_NAME );
@@ -90,6 +141,12 @@ bool ffParseSwapCommandOptions(FFSwapOptions* options, const char* key, const ch
90
141
if (ffPercentParseCommandOptions (key , subKey , value , & options -> percent ))
91
142
return true;
92
143
144
+ if (ffStrEqualsIgnCase (subKey , "separate" ))
145
+ {
146
+ options -> separate = ffOptionParseBoolean (value );
147
+ return true;
148
+ }
149
+
93
150
return false;
94
151
}
95
152
@@ -109,6 +166,12 @@ void ffParseSwapJsonObject(FFSwapOptions* options, yyjson_val* module)
109
166
if (ffPercentParseJsonObject (key , val , & options -> percent ))
110
167
continue ;
111
168
169
+ if (ffStrEqualsIgnCase (key , "separate" ))
170
+ {
171
+ options -> separate = yyjson_get_bool (val );
172
+ continue ;
173
+ }
174
+
112
175
ffPrintError (FF_SWAP_MODULE_NAME , 0 , & options -> moduleArgs , FF_PRINT_TYPE_DEFAULT , "Unknown JSON key %s" , key );
113
176
}
114
177
}
@@ -125,18 +188,28 @@ void ffGenerateSwapJsonConfig(FFSwapOptions* options, yyjson_mut_doc* doc, yyjso
125
188
126
189
void ffGenerateSwapJsonResult (FF_MAYBE_UNUSED FFSwapOptions * options , yyjson_mut_doc * doc , yyjson_mut_val * module )
127
190
{
128
- FFSwapResult storage = {} ;
129
- const char * error = ffDetectSwap (& storage );
191
+ FF_LIST_AUTO_DESTROY result = ffListCreate ( sizeof ( FFSwapResult )) ;
192
+ const char * error = ffDetectSwap (& result );
130
193
131
194
if (error )
132
195
{
133
196
yyjson_mut_obj_add_str (doc , module , "error" , error );
134
197
return ;
135
198
}
136
199
137
- yyjson_mut_val * obj = yyjson_mut_obj_add_obj (doc , module , "result" );
138
- yyjson_mut_obj_add_uint (doc , obj , "total" , storage .bytesTotal );
139
- yyjson_mut_obj_add_uint (doc , obj , "used" , storage .bytesUsed );
200
+ yyjson_mut_val * arr = yyjson_mut_obj_add_arr (doc , module , "result" );
201
+ FF_LIST_FOR_EACH (FFSwapResult , storage , result )
202
+ {
203
+ yyjson_mut_val * obj = yyjson_mut_arr_add_obj (doc , arr );
204
+ yyjson_mut_obj_add_strbuf (doc , obj , "name" , & storage -> name );
205
+ yyjson_mut_obj_add_uint (doc , obj , "used" , storage -> bytesUsed );
206
+ yyjson_mut_obj_add_uint (doc , obj , "total" , storage -> bytesTotal );
207
+ }
208
+
209
+ FF_LIST_FOR_EACH (FFSwapResult , storage , result )
210
+ {
211
+ ffStrbufDestroy (& storage -> name );
212
+ }
140
213
}
141
214
142
215
static FFModuleBaseInfo ffModuleInfo = {
@@ -152,6 +225,7 @@ static FFModuleBaseInfo ffModuleInfo = {
152
225
{"Total size" , "total" },
153
226
{"Percentage used (num)" , "percentage" },
154
227
{"Percentage used (bar)" , "percentage-bar" },
228
+ {"Name" , "name" },
155
229
}))
156
230
};
157
231
@@ -160,6 +234,7 @@ void ffInitSwapOptions(FFSwapOptions* options)
160
234
options -> moduleInfo = ffModuleInfo ;
161
235
ffOptionInitModuleArg (& options -> moduleArgs , "" );
162
236
options -> percent = (FFPercentageModuleConfig ) { 50 , 80 , 0 };
237
+ options -> separate = false;
163
238
}
164
239
165
240
void ffDestroySwapOptions (FFSwapOptions * options )
0 commit comments