23 #include <sys/errno.h>
37 string getOS(
string path);
47 string getRAM(
string path);
55 string getRES(
string path);
61 string getCPU(
string path);
73 void print(
string color,
string distro_name);
75 string getColor(
string);
83 filesystem::path path;
84 filesystem::file_status status;
86 Path(filesystem::path path, filesystem::file_status status)
89 this->status = status;
99 return Path(filesystem::path(path), filesystem::status(path));
107 filesystem::path p = this->path.filename();
108 return Path(p, filesystem::status(p));
116 return filesystem::is_regular_file(status);
124 if (status.permissions() == filesystem::perms::unknown)
126 return (status.permissions() & filesystem::perms::others_exec) !=
127 filesystem::perms::none;
135 return filesystem::is_directory(status);
143 return path.string();
151 vector<Path> contents;
152 if (!this->isDirectory())
157 for (
const auto &entry : filesystem::directory_iterator(path))
159 contents.push_back(
Path::of(entry.path()));
179 typedef std::function<void(
Command)> func_type;
186 static std::vector<std::thread> ths;
187 static std::vector<std::runtime_error> exceptions;
188 static std::mutex mtx;
196 static void split(vector<char *> &v,
string cmd)
198 istringstream ss{cmd};
199 for (
string arg{}; getline(ss, arg,
' '); ) {
203 auto s = strdup(arg.c_str());
205 throw runtime_error(
"strdup failed");
208 v.push_back((
char *)0);
239 static void exec_async(
const string &cmd,
const func_type &func)
241 ths.push_back(std::thread([=]() {
244 auto result = exec(cmd);
247 catch (
const runtime_error &e)
249 std::lock_guard<std::mutex> lock(mtx);
250 exceptions.push_back(e);
262 const func_type &func)
264 exec_async(cmd.
toString() +
" " + arg, func);
276 int out_fd[2], err_fd[2];
279 if (pipe(out_fd) == -1)
281 throw runtime_error(
"pipe failed");
283 if (pipe(err_fd) == -1)
285 throw runtime_error(
"pipe failed");
288 if ((pid = fork()) < 0)
290 throw runtime_error(
"fork faliled");
294 if (close(out_fd[0]) == -1)
296 throw runtime_error(
"close failed");
298 if (close(err_fd[0]) == -1)
300 throw runtime_error(
"close failed");
302 if (dup2(out_fd[1], fileno(stdout)) == -1)
304 throw runtime_error(
"dup2 failed");
306 if (dup2(err_fd[1], fileno(stderr)) == -1)
308 throw runtime_error(
"dup2 failed");
313 auto argv = v.data();
314 execvp(argv[0], argv);
325 throw runtime_error(
"execvp failed: " +
326 string(strerror(errno)) +
": " + argv[0]);
331 if (close(out_fd[1]) == -1)
333 throw runtime_error(
"close failed");
335 if (close(err_fd[1]) == -1)
337 throw runtime_error(
"close failed");
339 FILE *out = fdopen(out_fd[0],
"r");
342 throw runtime_error(
"fdopen failed");
344 FILE *err = fdopen(err_fd[0],
"r");
347 throw runtime_error(
"fdopen failed");
351 while ((c = fgetc(out)) != EOF)
360 while ((c = fgetc(err)) != EOF)
362 result.error_output += c;
365 if (fclose(out) == EOF)
367 throw runtime_error(
"fclose failed: " +
string(strerror(errno)));
370 if (fclose(err) == EOF)
372 throw runtime_error(
"fclose failed: " +
string(strerror(errno)));
376 waitpid(pid, &status, 0);
377 if (WIFEXITED(status))
379 result.exit_code = WEXITSTATUS(status);
381 else if (WIFSIGNALED(status))
383 int sig = WTERMSIG(status);
384 throw runtime_error(
"abnormal termination, signal number = " +
387 else if (WIFSTOPPED(status))
389 int sig = WSTOPSIG(status);
390 throw runtime_error(
"child stopped, signal number = " +
395 throw runtime_error(
"must not be here");
441 inline static map<string, string> m = {
442 {
"RED", RED}, {
"GREEN", GREEN}, {
"BLACK", BLACK},
443 {
"YELLOW", YELLOW}, {
"BLUE", BLUE}, {
"MAGENTA", MAGENTA},
444 {
"CYAN", CYAN}, {
"WHITE", WHITE}, {
"BBLACK", BBLACK},
445 {
"BGRAY", BGRAY}, {
"BRED", BRED}, {
"BGREEN", BGREEN},
446 {
"BYELLOW", BYELLOW}, {
"BBLUE", BBLUE}, {
"BMAGENTA", BMAGENTA},
447 {
"BCYAN", BCYAN}, {
"BWHITE", BWHITE},
450 static string getColor(
string s)
468 escape_codes += BRIGHT;
476 escape_codes += UNDERSCORE;
485 escape_codes += getColor(color);
497 escape_codes += GREEN;
503 escape_codes += YELLOW;
512 return escape_codes + s + RESET;
519 static string PACKAGE_DELIM;
531 Mode mode = Mode::NORMAL;
532 string color_name =
"def"s;
533 string distro_name =
"def"s;
534 bool show_battery =
false;
537 Options(
int argc,
char *argv[])
540 while ((opt = getopt(argc, argv,
"a:d:vb")) != -1)
546 color_name = string(optarg);
549 distro_name = string(optarg);
555 mode = Mode::SHOW_VERSION;
static void exec_async(const string &cmd, const func_type &func)
Definition: fetch.h:239
int getOutputLines()
Definition: fetch.h:420
int getExitCode()
Definition: fetch.h:428
string getOutput()
Definition: fetch.h:404
static void exec_async(const Path &cmd, const string &arg, const func_type &func)
Definition: fetch.h:261
static Command exec(const string &cmd)
Definition: fetch.h:273
static void wait()
Definition: fetch.h:215
string getErrorOutput()
Definition: fetch.h:412
static vector< runtime_error > & getExceptions()
Definition: fetch.h:229
Crayon yellow()
Definition: fetch.h:501
string text(string s)
Definition: fetch.h:510
Crayon bright()
Definition: fetch.h:466
Crayon red()
Definition: fetch.h:489
Crayon green()
Definition: fetch.h:495
Crayon color(string color)
Definition: fetch.h:483
Crayon underscore()
Definition: fetch.h:474
Crayon()
Definition: fetch.h:459
static Path of(const string &path)
Definition: fetch.h:97
vector< Path > getDirectoryContents()
Definition: fetch.h:149
bool isDirectory()
Definition: fetch.h:133
Path getFilename()
Definition: fetch.h:105
string toString() const
Definition: fetch.h:141
bool isExecutable()
Definition: fetch.h:122
bool isRegularFile()
Definition: fetch.h:114
string gethostname(string path)
Definition: fetch.cpp:33
vector< string > getGPU()
Definition: fetch.cpp:348
void printBattery(string path)
Definition: fetch.cpp:532
string getHardwarePlatform()
Definition: fetch.cpp:77
bool resCheck()
Definition: fetch.cpp:257
string getDE()
Definition: fetch.cpp:239
string getRES(string path)
Definition: fetch.cpp:266
string getOS(string path)
Definition: fetch.cpp:46
string getuser()
Definition: fetch.cpp:18
void print(string color, string distro_name)
Definition: fetch.cpp:599
string getPackages()
Definition: fetch.cpp:372
string getUpTime(string path)
Definition: fetch.cpp:127
string getCPU(string path)
Definition: fetch.cpp:304
string getRAM(string path)
Definition: fetch.cpp:162
string getTheme()
Definition: fetch.cpp:279
bool CpuTempCheck()
Definition: fetch.cpp:327
string getKernel(string path)
Definition: fetch.cpp:114
string getHost(string path)
Definition: fetch.cpp:90
int getCPUtemp(string path)
Definition: fetch.cpp:336
string getIcons()
Definition: fetch.cpp:291
string getSHELL(string path)
Definition: fetch.cpp:216