Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 62e97dd

Browse files
authored
⚡️ Sort packages (#103)
* Introduce struct rec. * sort
1 parent 99212c4 commit 62e97dd

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/fetch.cpp

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -352,67 +352,83 @@ vector<string> getGPU()
352352
*/
353353
string getPackages()
354354
{
355-
auto red = Crayon{}.red();
356-
vector<string> pkgs;
355+
struct rec
356+
{
357+
string name;
358+
int count; // -1: not supported
359+
};
360+
vector<rec> pkgs;
361+
357362
if (Path::of("/bin/dpkg"s).isExecutable())
358363
{
359364
auto c = Command::exec("dpkg -l"s);
360-
pkgs.push_back(to_string(c.getOutputLines()) + red.text(" dpkg"s));
365+
pkgs.push_back(rec{"dpkg"s, c.getOutputLines()});
361366
}
362367
if (Path::of("/bin/snap"s).isExecutable())
363368
{
364369
auto c = Command::exec("snap list"s);
365-
pkgs.push_back(to_string(c.getOutputLines()) + red.text(" snap"s));
370+
pkgs.push_back(rec{"snap"s, c.getOutputLines()});
366371
}
367372
if (Path::of("/bin/pacman"s).isExecutable())
368373
{
369374
auto c = Command::exec("pacman -Q"s);
370-
pkgs.push_back(to_string(c.getOutputLines()) + red.text(" pacman"s));
375+
pkgs.push_back(rec{"pacman"s, c.getOutputLines()});
371376
}
372377
if (Path::of("/bin/flatpak"s).isExecutable())
373378
{
374379
auto c = Command::exec("flatpak list"s);
375-
pkgs.push_back(to_string(c.getOutputLines()) + red.text(" flatpak"s));
380+
pkgs.push_back(rec{"flatpak"s, c.getOutputLines()});
376381
}
377382
if (Path::of("/var/lib/rpm"s).isExecutable())
378383
{
379384
auto c = Command::exec("rpm -qa"s);
380-
pkgs.push_back(to_string(c.getOutputLines()) + red.text(" rpm"s));
385+
pkgs.push_back(rec{"rpm"s, c.getOutputLines()});
381386
}
382387
if (Path::of("/bin/npm"s).isExecutable())
383388
{
384389
auto c = Command::exec("npm list"s);
385-
pkgs.push_back(to_string(c.getOutputLines()) + red.text(" npm"s));
390+
pkgs.push_back(rec{"npm"s, c.getOutputLines()});
386391
}
387392
if (Path::of("/bin/emerge"s).isExecutable()) // gentoo
388393
{
389-
pkgs.push_back("not supported"s + red.text(" portage"s));
394+
pkgs.push_back(rec{"portage"s, -1});
390395
}
391396
if (Path::of("/bin/xbps-install"s).isExecutable()) // void linux
392397
{
393398
auto c = Command::exec("flatpak list"s);
394-
pkgs.push_back(to_string(c.getOutputLines()) + red.text(" xbps"s));
399+
pkgs.push_back(rec{"xbps"s, c.getOutputLines()});
395400
}
396401
if (Path::of("/bin/dnf"s).isExecutable()) // fedora
397402
{
398403
auto c = Command::exec("dnf list installed"s);
399-
pkgs.push_back(to_string(c.getOutputLines()) + red.text(" dnf"s));
404+
pkgs.push_back(rec{"dnf"s, c.getOutputLines()});
400405
}
401406
if (Path::of("/bin/zypper"s).isExecutable()) // opensuse
402407
{
403408
auto c = Command::exec("zypper se --installed-only"s);
404-
pkgs.push_back(to_string(c.getOutputLines()) + red.text(" zypper"s));
409+
pkgs.push_back(rec{"zypper"s, c.getOutputLines()});
405410
}
406411
if (Path::of("/home/linuxbrew/.linuxbrew/bin/brew"s).isExecutable())
407412
{
408413
auto c = Command::exec("brew list | { tr '' '\n'; }"s);
409-
pkgs.push_back(to_string(c.getOutputLines()) + red.text(" brew"s));
414+
pkgs.push_back(rec{"brew"s, c.getOutputLines()});
410415
}
411416

417+
sort(pkgs.begin(), pkgs.end(),
418+
[](auto a, auto b) { return a.count > b.count; });
419+
420+
auto red = Crayon{}.red();
412421
auto pkg = ""s;
413422
for (auto p : pkgs)
414423
{
415-
pkg += p + Context::PACKAGE_DELIM;
424+
if (p.count < 0)
425+
{
426+
pkg +=
427+
"not supported "s + red.text(p.name) + Context::PACKAGE_DELIM;
428+
continue;
429+
}
430+
pkg += to_string(p.count) + " "s + red.text(p.name) +
431+
Context::PACKAGE_DELIM;
416432
}
417433

418434
return pkg;

0 commit comments

Comments
 (0)