Split things up a bit.

Also start approximating the look and feel
This commit is contained in:
2026-05-20 22:32:42 -04:00
parent 82ca0de82d
commit 2c39765a4d
5 changed files with 130 additions and 67 deletions
+43 -66
View File
@@ -3,72 +3,11 @@
#include <algorithm>
#include <filesystem>
bool
is_exe( const std::filesystem::directory_entry entry )
#include "Entry.h"
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;
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 ) );
}
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 &lhs, const auto &rhs ) { return lhs.name.size() < rhs.name.size(); } )->name.size();
const int rowWidth= widest + 8 + 5 + 3 + 10 + 2;
std::cout << "";
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 << "" << std::endl;
bool side= false;
for( const auto &entry: entries )
{
std::cout << format_entry( entry, widest ) << " ";
@@ -92,8 +52,25 @@ main()
}
std::cout << " ";
std::cout << "" << entry.perms << "";
std::cout << " |" << std::endl;
std::cout << " ";
if( side == false )
{
std::cout << "\u2502";
side= true;
}
else
{
side= false;
std::cout << std::endl;
}
}
if( side ) std::cout << std::endl;
std::cout << "";
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 << "" << std::endl;
return EXIT_SUCCESS;
}