-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLunaFetch.cpp
111 lines (94 loc) · 2.67 KB
/
LunaFetch.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
#include <stdlib.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>
#include <iostream>
#include <pwd.h>
#include <cmath>
#include <unistd.h>
#include <string.h>
// Terminal Colors
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
struct utsname uname_info;
char *username, *shellname;
long uptimeHour, uptimeMin;
static std::string *name()
{
username = cuserid(username);
std::cout << std::left << CYAN << " User: " << WHITE << username << std::endl;
return 0;
}
static std::string *shell()
{
shellname = getpwuid(geteuid())->pw_shell;
char* slash = strrchr(shellname, '/');
if (slash) {
shellname = slash + 1;
}
std::cout << std::left << CYAN << " Shell: " << WHITE << shellname << std::endl;
return 0;
}
static std::string *editor()
{
const char* editor = std::getenv("EDITOR");
if (editor && *editor) editor = basename(editor);
std::cout << std::left << CYAN << " Editor: " << WHITE << editor << std::endl;
return 0;
}
static std::string *get_os()
{
struct utsname uname_info;
if(uname(&uname_info)) exit(-1);
std::cout << std::left << CYAN << " OS: " << WHITE << uname_info.sysname << " " << uname_info.machine << std::endl;
return 0;
}
static std::string *kernel()
{
struct utsname uname_info;
if(uname(&uname_info)) exit(-1);
std::cout << std::left << CYAN << " Kernel: " << WHITE << uname_info.release << std::endl;
return 0;
}
size_t ram()
{
char* ram;
FILE* fp = fopen("/proc/meminfo", "r");
char tmp[128];
char* token, *ptr;
size_t total_mem = 0, free_mem = 0, buffers = 0, cached = 0;
while (fgets(tmp, sizeof(tmp) - 1, fp) != NULL && !(total_mem && free_mem)) {
token = strtok(tmp, " ");
if (!strcmp(token, "MemTotal:")){
token = strtok(NULL, " ");
total_mem = strtol(token, &ptr, 10);
} else if (!strcmp(token, "MemAvailable:")){
token = strtok(NULL, " ");
free_mem = strtol(token, &ptr, 10);
}
}
asprintf(&ram, "%zi/%zi mib",
(total_mem - free_mem - buffers) / 1024, total_mem / 1024);
fclose(fp);
std::cout << std::left << CYAN << " Ram: " << WHITE << ram << std::endl;
return 0;
}
static void uptime()
{
FILE* file = fopen("/proc/uptime", "r");
if (file == NULL)
return;
double uptime;
fscanf(file, "%lf", &uptime);
fclose(file);
std::cout << std::left << CYAN << " Uptime: " << WHITE << round(uptime / 60) << " mins" << std::endl;
}
int main() {
name();
get_os();
kernel();
ram();
editor();
shell();
uptime();
return 0;
}