Skip to content

Commit 3839ee6

Browse files
committed
Disk / Users: support duration printing in custom format
1 parent 6f266a1 commit 3839ee6

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ Features:
1313
* CMake: add option `-DCUSTOM_LSB_RELEASE_PATH` to specify the path of `lsb-release` file
1414
* `-DCUSTOM_OS_RELEASE_PATH` has been supported since `v2.11.4`
1515
* Report more SOC names on Android (CPU, Android)
16+
* Support duration printing in custom format (Disk / Users)
17+
* For example:
18+
```json
19+
{
20+
"modules": [
21+
{
22+
"key": "OS Age", // No longer need to write bash scripts
23+
"type": "disk",
24+
"folders": "/", // Different OSes may need to specify different folders
25+
"format": "{days} days"
26+
}
27+
]
28+
}
29+
```
1630

1731
Logo:
1832
* Add Arch_old

src/modules/disk/disk.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index
125125
bool isHidden = !!(disk->type & FF_DISK_VOLUME_TYPE_HIDDEN_BIT);
126126
bool isReadOnly = !!(disk->type & FF_DISK_VOLUME_TYPE_READONLY_BIT);
127127

128+
uint64_t duration = ffTimeGetNow() - disk->createTime;
129+
uint32_t milliseconds = (uint32_t) (duration % 1000);
130+
duration /= 1000;
131+
uint32_t seconds = (uint32_t) (duration % 60);
132+
duration /= 60;
133+
uint32_t minutes = (uint32_t) (duration % 60);
134+
duration /= 60;
135+
uint32_t hours = (uint32_t) (duration % 24);
136+
duration /= 24;
137+
uint32_t days = (uint32_t) duration;
138+
128139
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]) {
129140
FF_FORMAT_ARG(usedPretty, "size-used"),
130141
FF_FORMAT_ARG(totalPretty, "size-total"),
@@ -140,6 +151,11 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index
140151
{FF_FORMAT_ARG_TYPE_STRING, ffTimeToShortStr(disk->createTime), "create-time"},
141152
FF_FORMAT_ARG(bytesPercentageBar, "size-percentage-bar"),
142153
FF_FORMAT_ARG(filesPercentageBar, "files-percentage-bar"),
154+
FF_FORMAT_ARG(days, "days"),
155+
FF_FORMAT_ARG(hours, "hours"),
156+
FF_FORMAT_ARG(minutes, "minutes"),
157+
FF_FORMAT_ARG(seconds, "seconds"),
158+
FF_FORMAT_ARG(milliseconds, "milliseconds"),
143159
}));
144160
}
145161
}
@@ -462,7 +478,11 @@ static FFModuleBaseInfo ffModuleInfo = {
462478
{"Create time in local timezone", "create-time"},
463479
{"Size percentage bar", "size-percentage-bar"},
464480
{"Files percentage bar", "files-percentage-bar"},
465-
{},
481+
{"Days after creation", "days"},
482+
{"Hours after creation", "hours"},
483+
{"Minutes after creation", "minutes"},
484+
{"Seconds after creation", "seconds"},
485+
{"Milliseconds after creation", "milliseconds"},
466486
}))
467487
};
468488

src/modules/users/users.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,33 @@ void ffPrintUsers(FFUsersOptions* options)
6262
}
6363
else
6464
{
65+
uint64_t now = ffTimeGetNow();
6566
for(uint32_t i = 0; i < users.length; ++i)
6667
{
6768
FFUserResult* user = FF_LIST_GET(FFUserResult, users, i);
6869

70+
uint64_t duration = now - user->loginTime;
71+
uint32_t milliseconds = (uint32_t) (duration % 1000);
72+
duration /= 1000;
73+
uint32_t seconds = (uint32_t) (duration % 60);
74+
duration /= 60;
75+
uint32_t minutes = (uint32_t) (duration % 60);
76+
duration /= 60;
77+
uint32_t hours = (uint32_t) (duration % 24);
78+
duration /= 24;
79+
uint32_t days = (uint32_t) duration;
80+
6981
FF_PRINT_FORMAT_CHECKED(FF_USERS_MODULE_NAME, users.length == 1 ? 0 : (uint8_t) (i + 1), &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){
7082
FF_FORMAT_ARG(user->name, "name"),
7183
FF_FORMAT_ARG(user->hostName, "host-name"),
7284
FF_FORMAT_ARG(user->sessionName, "session-name"),
7385
FF_FORMAT_ARG(user->clientIp, "client-ip"),
7486
{FF_FORMAT_ARG_TYPE_STRING, ffTimeToShortStr(user->loginTime), "login-time"},
87+
FF_FORMAT_ARG(days, "days"),
88+
FF_FORMAT_ARG(hours, "hours"),
89+
FF_FORMAT_ARG(minutes, "minutes"),
90+
FF_FORMAT_ARG(seconds, "seconds"),
91+
FF_FORMAT_ARG(milliseconds, "milliseconds"),
7592
}));
7693
}
7794
}
@@ -200,6 +217,11 @@ static FFModuleBaseInfo ffModuleInfo = {
200217
{"Session name", "session"},
201218
{"Client IP", "client-ip"},
202219
{"Login Time in local timezone", "login-time"},
220+
{"Days after login", "days"},
221+
{"Hours after login", "hours"},
222+
{"Minutes after login", "minutes"},
223+
{"Seconds after login", "seconds"},
224+
{"Milliseconds after login", "milliseconds"},
203225
}))
204226
};
205227

0 commit comments

Comments
 (0)