-
Notifications
You must be signed in to change notification settings - Fork 3
/
grab.cpp
3499 lines (2887 loc) · 114 KB
/
grab.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The C++ part handles all the layout calculations and printing.
* The shell script (under shell_script variable) is the script that fetches the info
*
* The reason for using a shell script for fetching is its simpler to expand on.
* Additionally shell scripts already exist for this job (making use of neofetch's functions here)
*
* Reason for not doing the printing in shell is because its simply too slow for such a
* cosmetic interface.
*
* When this program is run (with no arguments), it executes the shell script. The scripts
* fetchs the info and sets them as environment variables.
* The script re runs this program (with an arbitrary argument) with the new environment variables
* and hence C++ can fetch the variables.
*
* There must be a much more efficient way to do this, but this is what I was able to come up with
* as this is my first fetch script :D
*/
/* This code needs a lot of cleanup! */
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <cstring>
#include <sys/ioctl.h> //ioctl() and TIOCGWINSZ
#include <unistd.h> // for STDOUT_FILENO
#define COLOR_LINE 5
#define COLOR_TEXT 7
#define COLOR_TITLE 4
#define COLOR_ASCII 3
using namespace std;
int leftBoxesMaxWidth = 0;
int rightBoxesMaxWidth = 0;
int logoMaxWidth = 48;
int separatorsMaxWidth = 8;
string info(char* var) {
if (getenv(var)) return getenv(var);
else return "";
}
string left_info[][2] = {
{ "USR", info("title")},
{ "OS", info("distro")},
{ "HOST", info("model") },
{ "KRNL", info("kernel") },
{ "CPU", info("cpu") },
{ "GPU", info("gpu") },
// { "DISP", info("resolution") },
{ "RAM", info("memory") }
};
string right_info[][2] = {
{ "WM", info("wm") },
{ "TERM", info("TERM") },
{ "FONT", info("font") },
{ "THM", info("theme") },
{ "SHL", info("shell") },
{ "PKGS", info("packages") },
{ "UPT", info("uptime") }
};
string colors[] = {
"\x1B[30m",
"\x1B[31m",
"\x1B[32m",
"\x1B[33m",
"\x1B[34m",
"\x1B[35m",
"\x1B[36m",
"\x1B[37m",
"\033[0m" // Reset color character
};
// Outputs text with the escape color code prefixed
string color(string text, int n) {
return colors[n] + text;
}
// ASCII characters for the boxes
string chars[] = {
color("─", COLOR_LINE),
color("╭", COLOR_LINE),
color("╮", COLOR_LINE),
color("╰", COLOR_LINE),
color("╯", COLOR_LINE),
color("", COLOR_LINE),
color("", COLOR_LINE),
color("│", COLOR_LINE),
};
// The ASCII art is manually padded with spaces to the right
string ascii = R""""( &BGBB#& #B#&
&#GPGGB# BGG#
#GGGGB#BGGG#
#GGGGGGGGGG&
&BGGGGGGGGGGBBB###BB&
BGPPGG5J7!!7Y5PPPY7YB&
#G?~557^:....:^^~?PP5GG&
&B5!:^!^::......~?JYBBBGGG#
#G5~:::::......:5&@@@@@@@&##&
BGP!:::::......!B
#GG?:::::.....:J&
&GGP~::::.....!G
BGG5^::::...~5
#GGG5^:::...!#
#GGGGP~:::...P
&&#BGGGGGG7:::..^#
&&#BGY?!~~!7J5J:::..~&
&######BGPPP555PPJ^::.!&
#5!::~JG&
&GJ7JY55#
&&
)"""";
void padString(string& s, signed int len, string side = "left") {
if (size(s) >= len) return;
// s.resize(100);
int n = len - s.size();
if (side == "left") {
for (int i = 0; i < n; i++) {
s = " " + s;
// s += " ";
}
} else {
for (int i = 0; i < n; i++) {
s += " ";
}
}
}
// Creates a box with title and text. Padding adds extra space to the given side.
string createBox(string title, string text, string titleSide = "left", int padding = 10, string paddingSide = "left") {
string s = ""; // Final box string
if (paddingSide == "left") padString(text, size(title) + 4, "left");
else padString(text, size(title) + 4, "right");
signed int horizontal_width = text.size() + 4;
string top_line = "";
signed int l = horizontal_width - 7 - (signed)title.size();
if (l > 0) {
if (horizontal_width - 7 - size(title) > 0) {
for (int i = 0; i < horizontal_width - 7 - size(title); i++) {
top_line += chars[0];
}
}
}
string bottom_line = "";
for (int i = 0; i < horizontal_width - 2; i++) {
bottom_line += chars[0];
}
if (paddingSide == "left") {
s += string(padding - horizontal_width, ' ');
if (titleSide == "right") s += chars[1] + top_line + chars[5] + " " + color(title, COLOR_TITLE) + " " + chars[6] + chars[0] + chars[2] + "\n";
else s += chars[1] + chars[0] + chars[5] + " " + title + " " + chars[6] + top_line + chars[2] + "\n";
s += string(padding - horizontal_width, ' ');
s += chars[7] + " " + color(text, COLOR_TEXT) + " " + chars[7] + "\n";
s += string(padding - horizontal_width, ' ');
s += chars[3] + bottom_line + chars[4] + "\n";
} else {
if (titleSide == "right") s += chars[1] + top_line + chars[5] + " " + color(title, COLOR_TITLE) + " " + chars[6] + chars[0] + chars[2];
else s += chars[1] + chars[0] + chars[5] + " " + color(title, COLOR_TITLE) + " " + chars[6] + top_line + chars[2];
s += string(padding - horizontal_width, ' ') + "\n";
s += chars[7] + " " + color(text, COLOR_TEXT) + " " + chars[7];
s += string(padding - horizontal_width, ' ') + "\n";
s += chars[3] + bottom_line + chars[4];
s += string(padding - horizontal_width, ' ') + "\n";
}
return s;
}
// Generate the boxes from info after calculating the required padding
string generateLeftBoxes() {
string s = ""; // Final string of boxes
// Get longest line length for padding
int maxWidth = 0;
for (int i = 0; i < size(left_info); i++) {
if (maxWidth < size(left_info[i][1])) {
maxWidth = size(left_info[i][1]);
}
}
leftBoxesMaxWidth = maxWidth;
// Create a padded box for every item in the info array
for (int i = 0; i < size(left_info); i++) {
s += createBox(left_info[i][0], left_info[i][1], "right", maxWidth + 4, "left");
}
return s;
}
// Generate the boxes from info after calculating the required padding
string generateRightBoxes() {
string s = ""; // Final string of boxes
// Get longest line length for padding
int maxWidth = 0;
for (int i = 0; i < size(right_info); i++) {
// cout << max < size(info[i][1]) << endl;
// Text
if (maxWidth < size(right_info[i][1]) + 4) {
maxWidth = size(right_info[i][1]) + 4;
}
// Titles
if (maxWidth < size(right_info[i][0]) + 8) {
maxWidth = size(right_info[i][0]) + 8;
}
}
rightBoxesMaxWidth = maxWidth;
// Create a padded box for every item in the info array
for (int i = 0; i < size(right_info); i++) {
s += createBox(right_info[i][0], right_info[i][1], "left", maxWidth, "right");
}
return s;
}
// Absolutely gigachad operator overloading that prints multiline strings horizontally next to each other
// Thanks to lastchance on https://cplusplus.com/forum/beginner/257126/
string operator *( const string a, const string b )
{
const string SPACE = " ";
stringstream ssa( a ), ssb( b );
string result, parta, partb;
while( getline( ssa, parta ) && getline( ssb, partb ) ) result += parta + SPACE + partb + '\n';
return result;
}
void outputFetch() {
// Color the ASCII art
istringstream f(ascii);
string line;
string colored_ascii = "";
while (getline(f, line)) {
colored_ascii += color(line, COLOR_ASCII) + "\n";
}
struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
// Don't print the logo if the terminal width is not long enough.
// Should change this to check the width of the boxes before generating them later
string content = generateLeftBoxes() * colored_ascii * generateRightBoxes();
if (size.ws_col < (leftBoxesMaxWidth + rightBoxesMaxWidth + logoMaxWidth + separatorsMaxWidth)) {
content = generateLeftBoxes() * generateRightBoxes();
}
// // Create a box around the whole thing
// cout << content.find_first_of("\n");
//
// istringstream f1(content);
// string line1;
// int maxWidth = 0;
// while (getline(f1, line1)) {
// // cout << line1.size() << endl;
// int s = regex_replace(line1, regex(R"(\x1b\[[0-9;]*m)"), "").size();
// if (s > maxWidth) maxWidth = s;
// }
// cout << string(maxWidth, 'a') << endl;
cout << endl;
cout << content;
cout << colors[8] << endl; // Output colors[8] to reset colors after the script to be terminal default
}
// The shell script for fetching all the info
string shell_script=R"""(
#!/usr/bin/env bash
# This script grabs all the required info for grab
# Its essentially just a replica of neofetch with changes to match grab
shopt -s eval_unsafe_arith &>/dev/null
sys_locale=${LANG:-C}
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-${HOME}/.config}
PATH=$PATH:/usr/xpg4/bin:/usr/sbin:/sbin:/usr/etc:/usr/libexec
reset='\e[0m'
shopt -s nocasematch
# Speed up script by not using unicode.
LC_ALL=C
LANG=C
# Fix issues with gsettings.
export GIO_EXTRA_MODULES=/usr/lib/x86_64-linux-gnu/gio/modules/
# DETECT INFORMATION
get_os() {
# $kernel_name is set in a function called cache_uname and is
# just the output of "uname -s".
case $kernel_name in
Darwin) os=$darwin_name ;;
SunOS) os=Solaris ;;
Haiku) os=Haiku ;;
MINIX) os=MINIX ;;
AIX) os=AIX ;;
IRIX*) os=IRIX ;;
FreeMiNT) os=FreeMiNT ;;
Linux|GNU*)
os=Linux
;;
*BSD|DragonFly|Bitrig)
os=BSD
;;
CYGWIN*|MSYS*|MINGW*)
os=Windows
;;
*)
printf '%s\n' "Unknown OS detected: '$kernel_name', aborting..." >&2
printf '%s\n' "Open an issue on GitHub to add support for your OS." >&2
exit 1
;;
esac
}
get_distro() {
[[ $distro ]] && return
case $os in
Linux|BSD|MINIX)
if [[ -f /etc/redstar-release ]]; then
case $distro_shorthand in
on|tiny) distro="Red Star OS" ;;
*) distro="Red Star OS $(awk -F'[^0-9*]' '$0=$2' /etc/redstar-release)"
esac
elif [[ -f /etc/armbian-release ]]; then
. /etc/armbian-release
distro="Armbian $DISTRIBUTION_CODENAME (${VERSION:-})"
elif [[ -f /etc/siduction-version ]]; then
case $distro_shorthand in
on|tiny) distro=Siduction ;;
*) distro="Siduction ($(lsb_release -sic))"
esac
elif [[ -f /etc/mcst_version ]]; then
case $distro_shorthand in
on|tiny) distro="OS Elbrus" ;;
*) distro="OS Elbrus $(< /etc/mcst_version)"
esac
elif type -p pveversion >/dev/null; then
case $distro_shorthand in
on|tiny) distro="Proxmox VE" ;;
*)
distro=$(pveversion)
distro=${distro#pve-manager/}
distro="Proxmox VE ${distro%/*}"
esac
elif type -p lsb_release >/dev/null; then
case $distro_shorthand in
on) lsb_flags=-si ;;
tiny) lsb_flags=-si ;;
*) lsb_flags=-sd ;;
esac
distro=$(lsb_release "$lsb_flags")
elif [[ -f /etc/os-release || \
-f /usr/lib/os-release || \
-f /etc/openwrt_release ]]; then
# Source the os-release file
for file in /usr/lib/os-release \
/etc/os-release /etc/openwrt_release; do
#(source "$file" && break) 2> /dev/null
source "$file" && break
done
# Format the distro name.
case $distro_shorthand in
on) distro="${NAME:-${DISTRIB_ID}} ${VERSION_ID:-${DISTRIB_RELEASE}}" ;;
tiny) distro="${NAME:-${DISTRIB_ID:-${TAILS_PRODUCT_NAME}}}" ;;
off) distro="${PRETTY_NAME:-${DISTRIB_DESCRIPTION}} ${UBUNTU_CODENAME}" ;;
esac
elif [[ -f /etc/GoboLinuxVersion ]]; then
case $distro_shorthand in
on|tiny) distro=GoboLinux ;;
*) distro="GoboLinux $(< /etc/GoboLinuxVersion)"
esac
elif [[ -f /etc/SDE-VERSION ]]; then
distro="$(< /etc/SDE-VERSION)"
case $distro_shorthand in
on|tiny) distro="${distro% *}" ;;
esac
elif type -p crux >/dev/null; then
distro=$(crux)
case $distro_shorthand in
on) distro=${distro//version} ;;
tiny) distro=${distro//version*}
esac
elif type -p tazpkg >/dev/null; then
distro="SliTaz $(< /etc/slitaz-release)"
elif type -p kpt >/dev/null && \
type -p kpm >/dev/null; then
distro=KSLinux
elif [[ -d /system/app/ && -d /system/priv-app ]]; then
distro="Android $(getprop ro.build.version.release)"
# Chrome OS doesn't conform to the /etc/*-release standard.
# While the file is a series of variables they can't be sourced
# by the shell since the values aren't quoted.
elif [[ -f /etc/lsb-release && $(< /etc/lsb-release) == *CHROMEOS* ]]; then
distro='Chrome OS'
elif type -p guix >/dev/null; then
case $distro_shorthand in
on|tiny) distro="Guix System" ;;
*) distro="Guix System $(guix -V | awk 'NR==1{printf $4}')"
esac
# Display whether using '-current' or '-release' on OpenBSD.
elif [[ $kernel_name = OpenBSD ]] ; then
read -ra kernel_info <<< "$(sysctl -n kern.version)"
distro=${kernel_info[*]:0:2}
else
for release_file in /etc/*-release; do
distro+=$(< "$release_file")
done
if [[ -z $distro ]]; then
case $distro_shorthand in
on|tiny) distro=$kernel_name ;;
*) distro="$kernel_name $kernel_version" ;;
esac
distro=${distro/DragonFly/DragonFlyBSD}
# Workarounds for some BSD based distros.
[[ -f /etc/pcbsd-lang ]] && distro=PCBSD
[[ -f /etc/trueos-lang ]] && distro=TrueOS
[[ -f /etc/pacbsd-release ]] && distro=PacBSD
[[ -f /etc/hbsd-update.conf ]] && distro=HardenedBSD
fi
fi
if [[ $(< /proc/version) == *Microsoft* || $kernel_version == *Microsoft* ]]; then
windows_version=$(wmic.exe os get Version)
windows_version=$(trim "${windows_version/Version}")
case $distro_shorthand in
on) distro+=" [Windows $windows_version]" ;;
tiny) distro="Windows ${windows_version::2}" ;;
*) distro+=" on Windows $windows_version" ;;
esac
elif [[ $(< /proc/version) == *chrome-bot* || -f /dev/cros_ec ]]; then
[[ $distro != *Chrome* ]] &&
case $distro_shorthand in
on) distro+=" [Chrome OS]" ;;
tiny) distro="Chrome OS" ;;
*) distro+=" on Chrome OS" ;;
esac
distro=${distro## on }
fi
distro=$(trim_quotes "$distro")
distro=${distro/NAME=}
# Get Ubuntu flavor.
if [[ $distro == "Ubuntu"* ]]; then
case $XDG_CONFIG_DIRS in
*"studio"*) distro=${distro/Ubuntu/Ubuntu Studio} ;;
*"plasma"*) distro=${distro/Ubuntu/Kubuntu} ;;
*"mate"*) distro=${distro/Ubuntu/Ubuntu MATE} ;;
*"xubuntu"*) distro=${distro/Ubuntu/Xubuntu} ;;
*"Lubuntu"*) distro=${distro/Ubuntu/Lubuntu} ;;
*"budgie"*) distro=${distro/Ubuntu/Ubuntu Budgie} ;;
*"cinnamon"*) distro=${distro/Ubuntu/Ubuntu Cinnamon} ;;
esac
fi
;;
"Mac OS X"|"macOS")
case $osx_version in
10.4*) codename="Mac OS X Tiger" ;;
10.5*) codename="Mac OS X Leopard" ;;
10.6*) codename="Mac OS X Snow Leopard" ;;
10.7*) codename="Mac OS X Lion" ;;
10.8*) codename="OS X Mountain Lion" ;;
10.9*) codename="OS X Mavericks" ;;
10.10*) codename="OS X Yosemite" ;;
10.11*) codename="OS X El Capitan" ;;
10.12*) codename="macOS Sierra" ;;
10.13*) codename="macOS High Sierra" ;;
10.14*) codename="macOS Mojave" ;;
10.15*) codename="macOS Catalina" ;;
10.16*) codename="macOS Big Sur" ;;
11.*) codename="macOS Big Sur" ;;
12.*) codename="macOS Monterey" ;;
*) codename=macOS ;;
esac
distro="$codename $osx_version $osx_build"
case $distro_shorthand in
on) distro=${distro/ ${osx_build}} ;;
tiny)
case $osx_version in
10.[4-7]*) distro=${distro/${codename}/Mac OS X} ;;
10.[8-9]*|10.1[0-1]*) distro=${distro/${codename}/OS X} ;;
10.1[2-6]*|11.0*) distro=${distro/${codename}/macOS} ;;
esac
distro=${distro/ ${osx_build}}
;;
esac
;;
"iPhone OS")
distro="iOS $osx_version"
# "uname -m" doesn't print architecture on iOS.
os_arch=off
;;
Windows)
distro=$(wmic os get Caption)
distro=${distro/Caption}
distro=${distro/Microsoft }
;;
Solaris)
case $distro_shorthand in
on|tiny) distro=$(awk 'NR==1 {print $1,$3}' /etc/release) ;;
*) distro=$(awk 'NR==1 {print $1,$2,$3}' /etc/release) ;;
esac
distro=${distro/\(*}
;;
Haiku)
distro=Haiku
;;
AIX)
distro="AIX $(oslevel)"
;;
IRIX)
distro="IRIX ${kernel_version}"
;;
FreeMiNT)
distro=FreeMiNT
;;
esac
distro=${distro//Enterprise Server}
[[ $distro ]] || distro="$os (Unknown)"
# Get OS architecture.
case $os in
Solaris|AIX|Haiku|IRIX|FreeMiNT)
machine_arch=$(uname -p)
;;
*) machine_arch=$kernel_machine ;;
esac
[[ $os_arch == on ]] && \
distro+=" $machine_arch"
[[ ${ascii_distro:-auto} == auto ]] && \
ascii_distro=$(trim "$distro")
}
get_model() {
case $os in
Linux)
if [[ -d /system/app/ && -d /system/priv-app ]]; then
model="$(getprop ro.product.brand) $(getprop ro.product.model)"
elif [[ -f /sys/devices/virtual/dmi/id/board_vendor ||
-f /sys/devices/virtual/dmi/id/board_name ]]; then
model=$(< /sys/devices/virtual/dmi/id/board_vendor)
model+=" $(< /sys/devices/virtual/dmi/id/board_name)"
elif [[ -f /sys/devices/virtual/dmi/id/product_name ||
-f /sys/devices/virtual/dmi/id/product_version ]]; then
model=$(< /sys/devices/virtual/dmi/id/product_name)
model+=" $(< /sys/devices/virtual/dmi/id/product_version)"
elif [[ -f /sys/firmware/devicetree/base/model ]]; then
model=$(< /sys/firmware/devicetree/base/model)
elif [[ -f /tmp/sysinfo/model ]]; then
model=$(< /tmp/sysinfo/model)
fi
;;
"Mac OS X"|"macOS")
if [[ $(kextstat | grep -F -e "FakeSMC" -e "VirtualSMC") != "" ]]; then
model="Hackintosh (SMBIOS: $(sysctl -n hw.model))"
else
model=$(sysctl -n hw.model)
fi
;;
BSD|MINIX)
model=$(sysctl -n hw.vendor hw.product)
;;
Windows)
model=$(wmic computersystem get manufacturer,model)
model=${model/Manufacturer}
model=${model/Model}
;;
Solaris)
model=$(prtconf -b | awk -F':' '/banner-name/ {printf $2}')
;;
AIX)
model=$(/usr/bin/uname -M)
;;
FreeMiNT)
model=$(sysctl -n hw.model)
model=${model/ (_MCH *)}
;;
esac
# Remove dummy OEM info.
model=${model//To be filled by O.E.M.}
model=${model//To Be Filled*}
model=${model//OEM*}
model=${model//Not Applicable}
model=${model//System Product Name}
model=${model//System Version}
model=${model//Undefined}
model=${model//Default string}
model=${model//Not Specified}
model=${model//Type1ProductConfigId}
model=${model//INVALID}
model=${model//All Series}
model=${model//�}
case $model in
"Standard PC"*) model="KVM/QEMU (${model})" ;;
OpenBSD*) model="vmm ($model)" ;;
esac
}
get_title() {
user=${USER:-$(id -un || printf %s "${HOME/*\/}")}
case $title_fqdn in
on) hostname=$(hostname -f) ;;
*) hostname=${HOSTNAME:-$(hostname)} ;;
esac
title=${title_color}${bold}${user}${at_color}@${title_color}${bold}${hostname}
length=$((${#user} + ${#hostname} + 1))
}
get_kernel() {
# Since these OS are integrated systems, it's better to skip this function altogether
[[ $os =~ (AIX|IRIX) ]] && return
# Haiku uses 'uname -v' and not - 'uname -r'.
[[ $os == Haiku ]] && {
kernel=$(uname -v)
return
}
# In Windows 'uname' may return the info of GNUenv thus use wmic for OS kernel.
[[ $os == Windows ]] && {
kernel=$(wmic os get Version)
kernel=${kernel/Version}
return
}
case $kernel_shorthand in
on) kernel=$kernel_version ;;
off) kernel="$kernel_name $kernel_version" ;;
esac
# Hide kernel info if it's identical to the distro info.
[[ $os =~ (BSD|MINIX) && $distro == *"$kernel_name"* ]] &&
case $distro_shorthand in
on|tiny) kernel=$kernel_version ;;
*) unset kernel ;;
esac
}
get_uptime() {
# Get uptime in seconds.
case $os in
Linux|Windows|MINIX)
if [[ -r /proc/uptime ]]; then
s=$(< /proc/uptime)
s=${s/.*}
else
boot=$(date -d"$(uptime -s)" +%s)
now=$(date +%s)
s=$((now - boot))
fi
;;
"Mac OS X"|"macOS"|"iPhone OS"|BSD|FreeMiNT)
boot=$(sysctl -n kern.boottime)
boot=${boot/\{ sec = }
boot=${boot/,*}
# Get current date in seconds.
now=$(date +%s)
s=$((now - boot))
;;
Solaris)
s=$(kstat -p unix:0:system_misc:snaptime | awk '{print $2}')
s=${s/.*}
;;
AIX|IRIX)
t=$(LC_ALL=POSIX ps -o etime= -p 1)
[[ $t == *-* ]] && { d=${t%%-*}; t=${t#*-}; }
[[ $t == *:*:* ]] && { h=${t%%:*}; t=${t#*:}; }
h=${h#0}
t=${t#0}
s=$((${d:-0}*86400 + ${h:-0}*3600 + ${t%%:*}*60 + ${t#*:}))
;;
Haiku)
s=$(($(system_time) / 1000000))
;;
esac
d="$((s / 60 / 60 / 24)) days"
h="$((s / 60 / 60 % 24)) hours"
m="$((s / 60 % 60)) minutes"
# Remove plural if < 2.
((${d/ *} == 1)) && d=${d/s}
((${h/ *} == 1)) && h=${h/s}
((${m/ *} == 1)) && m=${m/s}
# Hide empty fields.
((${d/ *} == 0)) && unset d
((${h/ *} == 0)) && unset h
((${m/ *} == 0)) && unset m
uptime=${d:+$d, }${h:+$h, }$m
uptime=${uptime%', '}
uptime=${uptime:-$s seconds}
# Make the output of uptime smaller.
case $uptime_shorthand in
on)
uptime=${uptime/ minutes/ mins}
uptime=${uptime/ minute/ min}
uptime=${uptime/ seconds/ secs}
;;
tiny)
uptime=${uptime/ days/d}
uptime=${uptime/ day/d}
uptime=${uptime/ hours/h}
uptime=${uptime/ hour/h}
uptime=${uptime/ minutes/m}
uptime=${uptime/ minute/m}
uptime=${uptime/ seconds/s}
uptime=${uptime//,}
;;
esac
}
get_packages() {
# to adjust the number of pkgs per pkg manager
pkgs_h=0
# has: Check if package manager installed.
# dir: Count files or dirs in a glob.
# pac: If packages > 0, log package manager name.
# tot: Count lines in command output.
has() { type -p "$1" >/dev/null && manager=$1; }
# globbing is intentional here
# shellcheck disable=SC2206
dir() { pkgs=($@); ((packages+=${#pkgs[@]})); pac "$((${#pkgs[@]}-pkgs_h))"; }
pac() { (($1 > 0)) && { managers+=("$1 (${manager})"); manager_string+="${manager}, "; }; }
tot() {
IFS=$'\n' read -d "" -ra pkgs <<< "$("$@")";
((packages+=${#pkgs[@]}));
pac "$((${#pkgs[@]}-pkgs_h))";
}
# Redefine tot() and dir() for Bedrock Linux.
[[ -f /bedrock/etc/bedrock-release && $PATH == */bedrock/cross/* ]] && {
br_strata=$(brl list)
tot() {
IFS=$'\n' read -d "" -ra pkgs <<< "$(for s in ${br_strata}; do strat -r "$s" "$@"; done)"
((packages+="${#pkgs[@]}"))
pac "$((${#pkgs[@]}-pkgs_h))";
}
dir() {
local pkgs=()
# globbing is intentional here
# shellcheck disable=SC2206
for s in ${br_strata}; do pkgs+=(/bedrock/strata/$s/$@); done
((packages+=${#pkgs[@]}))
pac "$((${#pkgs[@]}-pkgs_h))"
}
}
case $os in
Linux|BSD|"iPhone OS"|Solaris)
# Package Manager Programs.
has kiss && tot kiss l
has cpt-list && tot cpt-list
has pacman-key && tot pacman -Qq --color never
has dpkg && tot dpkg-query -f '.\n' -W
has xbps-query && tot xbps-query -l
has apk && tot apk info
has opkg && tot opkg list-installed
has pacman-g2 && tot pacman-g2 -Q
has lvu && tot lvu installed
has tce-status && tot tce-status -i
has pkg_info && tot pkg_info
has pkgin && tot pkgin list
has tazpkg && pkgs_h=6 tot tazpkg list && ((packages-=6))
has sorcery && tot gaze installed
has alps && tot alps showinstalled
has butch && tot butch list
has swupd && tot swupd bundle-list --quiet
has pisi && tot pisi li
has pacstall && tot pacstall -L
# Using the dnf package cache is much faster than rpm.
if has dnf && type -p sqlite3 >/dev/null && [[ -f /var/cache/dnf/packages.db ]]; then
pac "$(sqlite3 /var/cache/dnf/packages.db "SELECT count(pkg) FROM installed")"
else
has rpm && tot rpm -qa
fi
# 'mine' conflicts with minesweeper games.
[[ -f /etc/SDE-VERSION ]] &&
has mine && tot mine -q
# Counting files/dirs.
# Variables need to be unquoted here. Only Bedrock Linux is affected.
# $br_prefix is fixed and won't change based on user input so this is safe either way.
# shellcheck disable=SC2086
{
shopt -s nullglob
has brew && dir "$(brew --cellar)/* $(brew --caskroom)/*"
has emerge && dir "/var/db/pkg/*/*"
has Compile && dir "/Programs/*/"
has eopkg && dir "/var/lib/eopkg/package/*"
has crew && dir "${CREW_PREFIX:-/usr/local}/etc/crew/meta/*.filelist"
has pkgtool && dir "/var/log/packages/*"
has scratch && dir "/var/lib/scratchpkg/index/*/.pkginfo"
has kagami && dir "/var/lib/kagami/pkgs/*"
has cave && dir "/var/db/paludis/repositories/cross-installed/*/data/*/ \
/var/db/paludis/repositories/installed/data/*/"
shopt -u nullglob
}
# Other (Needs complex command)
has kpm-pkg && ((packages+=$(kpm --get-selections | grep -cv deinstall$)))
has guix && {
manager=guix-system && tot guix package -p "/run/current-system/profile" -I
manager=guix-user && tot guix package -I
}
has nix-store && {
nix-user-pkgs() {
nix-store -qR ~/.nix-profile
nix-store -qR /etc/profiles/per-user/"$USER"
}
manager=nix-system && tot nix-store -qR /run/current-system/sw
manager=nix-user && tot nix-user-pkgs
manager=nix-default && tot nix-store -qR /nix/var/nix/profiles/default
}
# pkginfo is also the name of a python package manager which is painfully slow.
# TODO: Fix this somehow.
has pkginfo && tot pkginfo -i
case $os-$kernel_name in
BSD-FreeBSD|BSD-DragonFly)
has pkg && tot pkg info
;;
BSD-*)
has pkg && dir /var/db/pkg/*
((packages == 0)) &&
has pkg && tot pkg list
;;
esac
# List these last as they accompany regular package managers.
has flatpak && tot flatpak list
has spm && tot spm list -i
has puyo && dir ~/.puyo/installed
# Snap hangs if the command is run without the daemon running.
# Only run snap if the daemon is also running.
has snap && ps -e | grep -qFm 1 snapd >/dev/null && \
pkgs_h=1 tot snap list && ((packages-=1))
# This is the only standard location for appimages.
# See: https://github.com/AppImage/AppImageKit/wiki
manager=appimage && has appimaged && dir ~/.local/bin/*.appimage
;;
"Mac OS X"|"macOS"|MINIX)
has port && pkgs_h=1 tot port installed && ((packages-=1))
has brew && dir "$(brew --cellar)/* $(brew --caskroom)/*"
has pkgin && tot pkgin list
has dpkg && tot dpkg-query -f '.\n' -W
has nix-store && {
nix-user-pkgs() {
nix-store -qR ~/.nix-profile
nix-store -qR /etc/profiles/per-user/"$USER"
}
manager=nix-system && tot nix-store -qR /run/current-system/sw
manager=nix-user && tot nix-user-pkgs
}
;;
AIX|FreeMiNT)
has lslpp && ((packages+=$(lslpp -J -l -q | grep -cv '^#')))
has rpm && tot rpm -qa
;;
Windows)
case $kernel_name in
CYGWIN*) has cygcheck && tot cygcheck -cd ;;
MSYS*) has pacman && tot pacman -Qq --color never ;;
esac
# Scoop environment throws errors if `tot scoop list` is used
has scoop && pkgs_h=1 dir ~/scoop/apps/* && ((packages-=1))
# Count chocolatey packages.
[[ -d /cygdrive/c/ProgramData/chocolatey/lib ]] && \
dir /cygdrive/c/ProgramData/chocolatey/lib/*
;;
Haiku)
has pkgman && dir /boot/system/package-links/*
packages=${packages/pkgman/depot}
;;
IRIX)
manager=swpkg
pkgs_h=3 tot versions -b && ((packages-=3))
;;
esac
if ((packages == 0)); then
unset packages
elif [[ $package_managers == on ]]; then
printf -v packages '%s, ' "${managers[@]}"
packages=${packages%,*}
elif [[ $package_managers == tiny ]]; then
packages+=" (${manager_string%,*})"
fi
packages=${packages/pacman-key/pacman}