From 82ca0de82df785d86911d2fbb68aa96eb83b1d6cc0759d11403f9ae19bacff19 Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Wed, 20 May 2026 04:13:43 -0400 Subject: [PATCH] A bit of visual sketch for where I want to go. I'm looking to emulate a lot of the `HotDir 2.1` look that I used to use. --- Makefile | 3 +++ cudir.cc | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index a5a5025..eadc765 100644 --- a/Makefile +++ b/Makefile @@ -6,3 +6,6 @@ CPPFLAGS+= -I. LDFLAGS+= -Wl,-rpath,'$$ORIGIN/' all: cudir + +clean: + rm -f cudir diff --git a/cudir.cc b/cudir.cc index 09e2605..6639dbb 100644 --- a/cudir.cc +++ b/cudir.cc @@ -1,15 +1,29 @@ -#include #include +#include +#include +#include + +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::perms p ) +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); @@ -23,13 +37,63 @@ drwx( const std::filesystem::perms p ) 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 } ) { - std::cout << entry.path().string() << " " << drwx( status( entry.path() ).permissions() ) << std::endl; + entries.emplace_back( entry.path().filename(), drwx( entry ), entry.is_directory(), is_exe( entry ) ); } + + 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(); + + for( const auto &entry: entries ) + { + std::cout << format_entry( entry, widest ) << " "; + if( entry.is_dir ) + { + std::cout << ""; + } + else + { + std::cout << " "; + } + std::cout << " "; + std::cout << "" << entry.perms << ""; + std::cout << " |" << std::endl; + } + + return EXIT_SUCCESS; }