File tree 3 files changed +13
-3
lines changed
Filter options
3 files changed +13
-3
lines changed
Original file line number Diff line number Diff line change @@ -11,12 +11,14 @@ int main(int argc, char* argv[]) {
11
11
// CLI flag vars
12
12
std::string dir = " ." ;
13
13
bool gdf = false ;
14
+ bool sort = false ;
14
15
15
16
app.add_option (" Directory" , dir, " Input a file" );
16
17
app.add_flag (" -g,--group-directories-first" , gdf, " Do you want dirs first?" );
18
+ app.add_flag (" -s,--sort" , sort, " Sort by name" );
17
19
CLI11_PARSE (app, argc, argv);
18
20
19
- (new Directory (dir))->show_ls (gdf);
21
+ (new Directory (dir, sort ))->show_ls (gdf);
20
22
21
23
return 0 ;
22
24
}
Original file line number Diff line number Diff line change @@ -154,7 +154,11 @@ File get_file_info(const fs::directory_entry& file) {
154
154
);
155
155
}
156
156
157
- Directory :: Directory (const std::string& path) {
157
+ bool sort_compare_file (File f1, File f2) {
158
+ return f1.get_path ().path ().filename ().string () < f2.get_path ().path ().filename ().string ();
159
+ }
160
+
161
+ Directory :: Directory (const std::string& path, bool sort) {
158
162
std::vector<File> paths_h = {};
159
163
160
164
if (fs::directory_entry (path).exists () && !fs::directory_entry (path).is_directory ()) {
@@ -168,6 +172,9 @@ Directory :: Directory (const std::string& path) {
168
172
}
169
173
}
170
174
175
+ if (sort)
176
+ std::sort (paths_h.begin (), paths_h.end (), sort_compare_file);
177
+
171
178
this ->paths = paths_h;
172
179
}
173
180
@@ -231,6 +238,7 @@ void Directory :: show_ls(bool gdf) {
231
238
std::vector<File> dirs;
232
239
std::vector<File> others;
233
240
241
+
234
242
if (gdf) {
235
243
for (File file : paths_h) {
236
244
if (file.get_path ().is_directory ()) {
Original file line number Diff line number Diff line change @@ -76,7 +76,7 @@ class Directory {
76
76
private:
77
77
std::vector<File> paths;
78
78
public:
79
- explicit Directory (const std::string&);
79
+ explicit Directory (const std::string&, bool );
80
80
~Directory () = default ;
81
81
82
82
std::vector<File> get_paths ();
You can’t perform that action at this time.
0 commit comments