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

Commit 43a9ce9

Browse files
Merge pull request #70 from TanmayPatil105/getopt
Added getopt
2 parents 08a97f5 + e04c095 commit 43a9ce9

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

src/fetch.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,15 +408,22 @@ string getPackages()
408408
/**
409409
* @param art
410410
*/
411-
void print_process(string art)
411+
void print_process(string art,string color_name)
412412
{
413413
string color;
414414
string path = "/usr/share/procfetch/ascii/" + art;
415415
fstream fptr;
416416
fptr.open(path, ios::in);
417417
string txt;
418418
getline(fptr, txt);
419-
color = getColor(txt);
419+
if (color_name == "def")
420+
{
421+
color = getColor(txt);
422+
}
423+
else{
424+
transform(color_name.begin(),color_name.end(),color_name.begin(),::toupper);
425+
color=getColor(color_name);
426+
}
420427
cout << color << endl;
421428
while (fptr)
422429
{
@@ -429,9 +436,14 @@ void print_process(string art)
429436
/**
430437
* Utility to print ascii art of Distro
431438
*/
432-
void print()
439+
void print(string color_name,string distro_name)
433440
{
434-
string os = getOS("/etc/os-release");
441+
string os = distro_name;
442+
443+
if (distro_name == "def")
444+
{
445+
os = getOS("/etc/os-release");
446+
}
435447

436448
map<string, string> ascii_arts = {{"Ubuntu", "ubuntu.ascii"},
437449
{"Debian", "debian.ascii"},
@@ -470,8 +482,12 @@ void print()
470482
{
471483
if (os.find(key) != string::npos)
472484
{
473-
print_process(value);
485+
print_process(value,color_name);
474486
return;
475487
}
476488
}
489+
490+
print_process("linux.ascii",color_name);
491+
492+
return;
477493
}

src/fetch.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <string>
1414
#include <unistd.h>
1515
#include <vector>
16+
#include <cctype>
1617

1718
using namespace std;
1819

@@ -54,7 +55,7 @@ vector<string> getGPU();
5455

5556
string getPackages();
5657

57-
void print();
58+
void print(string color,string distro_name);
5859

5960
// util.cpp
6061

src/main.cpp

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,62 @@
44
#include "color.h"
55
#include "fetch.h"
66

7+
void DisplayInfo();
8+
79
/**
810
* @returns
911
* @param argc
1012
* @param argv
1113
*/
1214
int main(int argc, char *argv[])
1315
{
14-
if (argc == 2 && !"-t"s.compare(argv[1]))
15-
{
16+
bool test_mode = false;
17+
string color_name = "def"s;
18+
string distro_name = "def"s;
19+
20+
int opt;
21+
while((opt = getopt(argc, argv, "ta:d:")) != -1)
22+
{
23+
switch(opt)
24+
{
25+
case 't':
26+
test_mode = true;
27+
break;
28+
case 'a':
29+
color_name = string(optarg);
30+
break;
31+
case 'd':
32+
distro_name = string(optarg);
33+
break;
34+
default:
35+
return 1;
36+
}
37+
}
38+
39+
if (test_mode) {
1640
test_util();
1741
cout << "========================"s << endl
1842
<< " All unit tests passed. "s << endl
1943
<< "========================"s << endl;
2044
return 0;
2145
}
2246

23-
print();
47+
if (optind != argc) {
48+
cout << "Error: "s << argv[0] << ": unknown argument: "s << argv[optind] << endl;
49+
return 1;
50+
}
51+
52+
print(color_name,distro_name);
53+
DisplayInfo();
54+
55+
return 0;
56+
}
57+
58+
/**
59+
* @returns Displays Info
60+
*/
61+
void DisplayInfo()
62+
{
2463
string user = getuser();
2564
string hostname = gethostname("/etc/hostname");
2665
string username = YELLOW + user + RESET + "@" + YELLOW + hostname;
@@ -63,5 +102,4 @@ int main(int argc, char *argv[])
63102
string pkg = getPackages();
64103
cout << BRIGHT << GREEN << "Packages : " << RESET << pkg << endl;
65104
cout << endl;
66-
return 0;
67105
}

0 commit comments

Comments
 (0)