Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0b9e68f

Browse filesBrowse files
committed
adding directory constructer
1 parent 4208c0a commit 0b9e68f
Copy full SHA for 0b9e68f

File tree

3 files changed

+89
-37
lines changed
Filter options

3 files changed

+89
-37
lines changed

‎tests/main.cpp

Copy file name to clipboardExpand all lines: tests/main.cpp
+18-20
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,70 @@
22
// Created by Will Lane on 2/3/21.
33
//
44

5-
65
#include <iostream>
76
#include "../utils/string_formatting/string_effects.h"
87
#include "../utils/string_formatting/string_coloring.h"
98

109
void test_strings(std::string name, std::string i, std::string b) {
1110
if (i != b) {
12-
std::cout << name << " failed: " << i << " != " << b << std::endl;
11+
std::cout << red(name + " failed: " + i + " != " + b) << std::endl;
1312
std::exit(1);
1413
}
1514
}
1615

1716
void test_string_effects() {
1817
test_strings("Bold", bold("test"), "\x1B[1mtest\x1B[0m");
19-
std::cout << bold("Bold passed.") << std::endl;
18+
std::cout << green("../utils::string_formatting::string_effects.h::bold passed.") << std::endl;
2019

2120
test_strings("Dimmed", dimmed("test"), "\x1B[2mtest\x1B[0m");
22-
std::cout << dimmed("Dimmed passed.") << std::endl;
21+
std::cout << green("../utils::string_formatting::string_effects.h::dimmed passed.") << std::endl;
2322

2423
test_strings("Italic", italic("test"), "\x1b[3mtest\x1b[0m");
25-
std::cout << italic("Italic passed.") << std::endl;
24+
std::cout << green("../utils::string_formatting::string_effects.h::italic passed.") << std::endl;
2625

2726
test_strings("Underline", underline("test"), "\x1b[4mtest\x1b[0m");
28-
std::cout << underline("Underline passed.") << std::endl;
27+
std::cout << green("../utils::string_formatting::string_effects.h::underline passed.") << std::endl;
2928

3029
test_strings("Blink", blink("test"), "\x1b[5mtest\x1b[0m");
31-
std::cout << blink("Blink passed.") << std::endl;
30+
std::cout << green("../utils::string_formatting::string_effects.h::blank passed.") << std::endl;
3231

3332
test_strings("Reverse", reverse("test"), "\x1b[7mtest\x1b[0m");
34-
std::cout << reverse("Reverse passed.") << std::endl;
33+
std::cout << green("../utils::string_formatting::string_effects.h::reverse passed.") << std::endl;
3534

3635
test_strings("Hidden", hidden("test"), "\x1b[8mtest\x1b[0m");
37-
std::cout << "Hidden passed." << std::endl;
36+
std::cout << green("../utils::string_formatting::string_effects.h::hidden passed.") << std::endl;
3837

3938
test_strings("Stricken", stricken("test"), "\x1b[9mtest\x1b[0m");
40-
std::cout << stricken("Stricken passed.") << std::endl;
39+
std::cout << green("../utils::string_formatting::string_effects.h::stricken passed.") << std::endl;
4140

42-
std::cout << "All text effect tests passed." << std::endl;
41+
std::cout << green("All text effect tests passed.") << std::endl;
4342
}
4443

4544
void test_string_colors() {
4645
test_strings("Red", red("test"), "\033[31;31mtest\033[0m");
47-
std::cout << red("Red passed.") << std::endl;
46+
std::cout << green("../utils::string_formatting::string_coloring.h::red passed.") << std::endl;
4847

4948
test_strings("Yellow", yellow("test"), "\033[33;33mtest\033[0m");
50-
std::cout << yellow("Yellow passed.") << std::endl;
49+
std::cout << green("../utils::string_formatting::string_coloring.h::yellow passed.") << std::endl;
5150

5251
test_strings("Magenta", magenta("test"), "\033[35;35mtest\033[0m");
53-
std::cout << magenta("Magenta passed.") << std::endl;
52+
std::cout << green("../utils::string_formatting::string_coloring.h::magenta passed.") << std::endl;
5453

5554
test_strings("Dark Blue", dark_blue("test"), "\033[94;94mtest\033[0m");
56-
std::cout << dark_blue("Dark Blue passed.") << std::endl;
55+
std::cout << green("../utils::string_formatting::string_coloring.h::dark_blue passed.") << std::endl;
5756

5857
test_strings("Light Blue", light_blue("test"), "\033[34;34mtest\033[0m");
59-
std::cout << light_blue("Light Blue passed.") << std::endl;
58+
std::cout << green("../utils::string_formatting::string_coloring.h::light_blue passed.") << std::endl;
6059

6160
test_strings("Grey", grey("test"), "\033[90;90mtest\033[0m");
62-
std::cout << grey("Grey passed.") << std::endl;
61+
std::cout << green("../utils::string_formatting::string_coloring.h::grey passed.") << std::endl;
6362

6463
test_strings("Green", green("test"), "\033[32;32mtest\033[0m");
65-
std::cout << green("Green passed.") << std::endl;
64+
std::cout << green("../utils::string_formatting::string_coloring.h::green passed.") << std::endl;
6665

67-
std::cout << "All text color tests passed." << std::endl;
66+
std::cout << green("All text color tests passed.") << std::endl;
6867
}
6968

70-
7169
int main() {
7270
test_string_effects();
7371
test_string_colors();

‎utils/types.cpp

Copy file name to clipboardExpand all lines: utils/types.cpp
+66-12
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,26 @@
33
//
44

55
#include "types.h"
6+
#include "fs/handle_files.h"
67
#include <string>
8+
#include <utility>
79
#include <vector>
8-
9-
Directory :: Directory(const std::vector<File>& paths) {
10-
this->paths = paths;
11-
}
10+
#include <fstream>
11+
#include <filesystem>
12+
#include <sys/stat.h>
13+
#include <grp.h>
14+
#include <pwd.h>
15+
namespace fs = std::filesystem;
1216

1317
File :: File (
14-
const fs::directory_entry& path,
15-
const std::vector<FileMetaType>& meta_types,
16-
const std::string& group,
17-
const std::string& user,
18-
const std::string& modified,
19-
const std::string& created,
20-
const std::string& size,
21-
const std::string& perms
18+
const fs::directory_entry & path,
19+
const std::vector<FileMetaType> & meta_types,
20+
const std::string & group,
21+
const std::string & user,
22+
const std::string & modified,
23+
const std::string & created,
24+
const std::string & size,
25+
const std::string & perms
2226
) {
2327
this->path = path;
2428
this->meta_types = meta_types;
@@ -33,3 +37,53 @@ File :: File (
3337
std::string File :: doit() {
3438
return this->user;
3539
}
40+
41+
Directory :: Directory (const std::string& path) {
42+
std::vector<File> hold = {};
43+
std::vector<fs::directory_entry> phold = get_files(path);
44+
45+
for (const fs::directory_entry& i : phold) {
46+
std::vector<FileMetaType> meta_types = {};
47+
if (i.is_directory())
48+
meta_types.push_back(FileMetaType::Dir);
49+
50+
if (i.is_symlink())
51+
meta_types.push_back(FileMetaType::Symlink);
52+
53+
if (i.is_fifo())
54+
meta_types.push_back(FileMetaType::Pipe);
55+
56+
if (i.is_character_file())
57+
meta_types.push_back(FileMetaType::CharD);
58+
59+
if (i.is_block_file())
60+
meta_types.push_back(FileMetaType::BlockD);
61+
62+
if (i.is_socket())
63+
meta_types.push_back(FileMetaType::Socket);
64+
65+
if (meta_types.empty())
66+
meta_types.push_back(FileMetaType::Path);
67+
68+
struct stat info{};
69+
stat(i.path().c_str(), &info);
70+
struct group *gr = getgrgid(info.st_gid);
71+
struct tm *timeinfo = localtime((const time_t *) info.st_ctime);
72+
struct passwd *pws = getpwuid(info.st_uid);
73+
74+
75+
hold.push_back(
76+
new File (
77+
i,
78+
meta_types,
79+
gr->gr_name,
80+
pws->pw_name,
81+
i.last_write_time(),
82+
timeinfo->tm_year,
83+
i.file_size(),
84+
fs::status(i.path()).permissions()
85+
)
86+
);
87+
}
88+
this->paths = paths;
89+
}

‎utils/types.h

Copy file name to clipboardExpand all lines: utils/types.h
+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace fs = std::filesystem;
1313

1414
enum FileMetaType {
15-
Directory,
15+
Dir,
1616
Symlink,
1717
Path,
1818
Pipe,
@@ -28,7 +28,7 @@ enum SortBy {
2828
None,
2929
};
3030

31-
struct File {
31+
class File {
3232
private:
3333
fs::directory_entry path;
3434
std::vector<FileMetaType> meta_types;
@@ -49,18 +49,18 @@ struct File {
4949
const std::string&,
5050
const std::string&,
5151
const std::string&
52-
);
52+
);
5353

5454
~File() = default;
5555

5656
std::string doit();
5757
};
5858

59-
struct Directory {
59+
class Directory {
6060
private:
6161
std::vector<File> paths;
6262
public:
63-
explicit Directory(const std::vector<File>&);
63+
explicit Directory(const std::string&);
6464
~Directory() = default;
6565
};
6666
#endif //NATCPP_TYPES_H

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.