Split things up a bit.
Also start approximating the look and feel
This commit is contained in:
@@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
struct Entry
|
||||||
|
{
|
||||||
|
std::string name;
|
||||||
|
std::string perms;
|
||||||
|
bool is_dir;
|
||||||
|
bool is_exe;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool is_exe( std::filesystem::directory_entry entry );
|
||||||
|
|
||||||
|
std::string drwx( std::filesystem::directory_entry entry );
|
||||||
|
|
||||||
|
std::string format_entry( const Entry &entry, std::size_t widest );
|
||||||
@@ -5,7 +5,12 @@ CPPFLAGS+= -I.
|
|||||||
|
|
||||||
LDFLAGS+= -Wl,-rpath,'$$ORIGIN/'
|
LDFLAGS+= -Wl,-rpath,'$$ORIGIN/'
|
||||||
|
|
||||||
|
OBJECTS= helpers.o format.o cudir.o
|
||||||
|
|
||||||
all: cudir
|
all: cudir
|
||||||
|
|
||||||
|
cudir: $(OBJECTS)
|
||||||
|
$(CXX) -o $@ $(OBJECTS)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f cudir
|
rm -f cudir *.o
|
||||||
|
|||||||
@@ -3,72 +3,11 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
bool
|
#include "Entry.h"
|
||||||
is_exe( const std::filesystem::directory_entry entry )
|
|
||||||
|
auto
|
||||||
|
getEntries( const std::filesystem::path where )
|
||||||
{
|
{
|
||||||
using std::filesystem::perms;
|
|
||||||
auto p= status( entry.path() ).permissions();
|
|
||||||
|
|
||||||
return ( perms::none != ( p & ( perms::owner_exec | perms::group_exec | perms::others_exec ) ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string
|
|
||||||
drwx( const std::filesystem::directory_entry entry )
|
|
||||||
{
|
|
||||||
auto p= status( entry.path() ).permissions();
|
|
||||||
|
|
||||||
std::ostringstream oss;
|
|
||||||
using std::filesystem::perms;
|
|
||||||
auto show = [&](const char op, const perms perm)
|
|
||||||
{
|
|
||||||
oss << (perms::none == (perm bitand p) ? '-' : op);
|
|
||||||
};
|
|
||||||
oss << ( entry.is_directory() ? 'd' : '-' );
|
|
||||||
show('r', perms::owner_read);
|
|
||||||
show('w', perms::owner_write);
|
|
||||||
show('x', perms::owner_exec);
|
|
||||||
show('r', perms::group_read);
|
|
||||||
show('w', perms::group_write);
|
|
||||||
show('x', perms::group_exec);
|
|
||||||
show('r', perms::others_read);
|
|
||||||
show('w', perms::others_write);
|
|
||||||
show('x', perms::others_exec);
|
|
||||||
|
|
||||||
return std::move( oss ).str();
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Entry
|
|
||||||
{
|
|
||||||
std::string name;
|
|
||||||
std::string perms;
|
|
||||||
bool is_dir;
|
|
||||||
bool is_exe;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::string
|
|
||||||
format_entry( const Entry &entry, const std::size_t widest )
|
|
||||||
{
|
|
||||||
std::ostringstream oss;
|
|
||||||
oss << "[";
|
|
||||||
|
|
||||||
if( entry.is_dir )
|
|
||||||
{
|
|
||||||
oss << "1;35;95";
|
|
||||||
}
|
|
||||||
else if( entry.is_exe )
|
|
||||||
{
|
|
||||||
oss << "1;36;96";
|
|
||||||
}
|
|
||||||
oss << 'm' << std::setw( widest + 8 ) << std::left << entry.name;
|
|
||||||
|
|
||||||
return std::move( oss ).str();
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
main()
|
|
||||||
{
|
|
||||||
std::filesystem::path where="./";
|
|
||||||
|
|
||||||
std::vector< Entry > entries;
|
std::vector< Entry > entries;
|
||||||
|
|
||||||
for( const auto &entry: std::filesystem::directory_iterator{ where } )
|
for( const auto &entry: std::filesystem::directory_iterator{ where } )
|
||||||
@@ -76,9 +15,30 @@ main()
|
|||||||
entries.emplace_back( entry.path().filename(), drwx( entry ), entry.is_directory(), is_exe( entry ) );
|
entries.emplace_back( entry.path().filename(), drwx( entry ), entry.is_directory(), is_exe( entry ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::sort( begin( entries ), end( entries ), []( const auto &lhs, const auto &rhs ) { return lhs.name < rhs.name; } );
|
||||||
|
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
std::filesystem::path where="./";
|
||||||
|
|
||||||
|
const auto entries= getEntries( where );
|
||||||
|
|
||||||
const auto widest= std::max_element( begin( entries ), end( entries ),
|
const auto widest= std::max_element( begin( entries ), end( entries ),
|
||||||
[]( const auto &lhs, const auto &rhs ) { return lhs.name.size() < rhs.name.size(); } )->name.size();
|
[]( const auto &lhs, const auto &rhs ) { return lhs.name.size() < rhs.name.size(); } )->name.size();
|
||||||
|
|
||||||
|
const int rowWidth= widest + 8 + 5 + 3 + 10 + 2;
|
||||||
|
|
||||||
|
std::cout << "[1;36;96m";
|
||||||
|
for( int i= 0; i < rowWidth; ++i ) std::cout << "\u2500";
|
||||||
|
std::cout << "\u252c";
|
||||||
|
for( int i= 0; i < rowWidth; ++i ) std::cout << "\u2500";
|
||||||
|
std::cout << "[m" << std::endl;
|
||||||
|
|
||||||
|
bool side= false;
|
||||||
for( const auto &entry: entries )
|
for( const auto &entry: entries )
|
||||||
{
|
{
|
||||||
std::cout << format_entry( entry, widest ) << "[m ";
|
std::cout << format_entry( entry, widest ) << "[m ";
|
||||||
@@ -92,8 +52,25 @@ main()
|
|||||||
}
|
}
|
||||||
std::cout << "[m ";
|
std::cout << "[m ";
|
||||||
std::cout << "[1;34;94m" << entry.perms << "[m";
|
std::cout << "[1;34;94m" << entry.perms << "[m";
|
||||||
std::cout << " [1;36;96m|[m" << std::endl;
|
std::cout << " ";
|
||||||
|
if( side == false )
|
||||||
|
{
|
||||||
|
std::cout << "[1;36;96m\u2502[m";
|
||||||
|
side= true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
side= false;
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if( side ) std::cout << std::endl;
|
||||||
|
|
||||||
|
std::cout << "[1;36;96m";
|
||||||
|
for( int i= 0; i < rowWidth; ++i ) std::cout << "\u2500";
|
||||||
|
std::cout << "\u2534";
|
||||||
|
for( int i= 0; i < rowWidth; ++i ) std::cout << "\u2500";
|
||||||
|
std::cout << "[m" << std::endl;
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#include "Entry.h"
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
std::string
|
||||||
|
format_entry( const Entry &entry, const std::size_t widest )
|
||||||
|
{
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << "[";
|
||||||
|
|
||||||
|
if( entry.is_dir )
|
||||||
|
{
|
||||||
|
oss << "1;35;95";
|
||||||
|
}
|
||||||
|
else if( entry.is_exe )
|
||||||
|
{
|
||||||
|
oss << "1;36;96";
|
||||||
|
}
|
||||||
|
oss << 'm' << std::setw( widest + 8 ) << std::left << entry.name;
|
||||||
|
|
||||||
|
return std::move( oss ).str();
|
||||||
|
}
|
||||||
|
|
||||||
+39
@@ -0,0 +1,39 @@
|
|||||||
|
#include "Entry.h"
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
bool
|
||||||
|
is_exe( const std::filesystem::directory_entry entry )
|
||||||
|
{
|
||||||
|
using std::filesystem::perms;
|
||||||
|
auto p= status( entry.path() ).permissions();
|
||||||
|
|
||||||
|
return ( perms::none != ( p & ( perms::owner_exec | perms::group_exec | perms::others_exec ) ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string
|
||||||
|
drwx( const std::filesystem::directory_entry entry )
|
||||||
|
{
|
||||||
|
auto p= status( entry.path() ).permissions();
|
||||||
|
|
||||||
|
std::ostringstream oss;
|
||||||
|
using std::filesystem::perms;
|
||||||
|
auto show = [&](const char op, const perms perm)
|
||||||
|
{
|
||||||
|
oss << (perms::none == (perm bitand p) ? '-' : op);
|
||||||
|
};
|
||||||
|
oss << ( entry.is_directory() ? 'd' : '-' );
|
||||||
|
show('r', perms::owner_read);
|
||||||
|
show('w', perms::owner_write);
|
||||||
|
show('x', perms::owner_exec);
|
||||||
|
show('r', perms::group_read);
|
||||||
|
show('w', perms::group_write);
|
||||||
|
show('x', perms::group_exec);
|
||||||
|
show('r', perms::others_read);
|
||||||
|
show('w', perms::others_write);
|
||||||
|
show('x', perms::others_exec);
|
||||||
|
|
||||||
|
return std::move( oss ).str();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user