From 2c39765a4d75af4d98503925293a628a97a881e5869b11a2cb0679bf248b608f Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Wed, 20 May 2026 22:32:42 -0400 Subject: [PATCH] Split things up a bit. Also start approximating the look and feel --- Entry.h | 18 +++++++++ Makefile | 7 +++- cudir.cc | 109 +++++++++++++++++++++-------------------------------- format.cc | 24 ++++++++++++ helpers.cc | 39 +++++++++++++++++++ 5 files changed, 130 insertions(+), 67 deletions(-) create mode 100644 Entry.h create mode 100644 format.cc create mode 100644 helpers.cc diff --git a/Entry.h b/Entry.h new file mode 100644 index 0000000..9ef68ba --- /dev/null +++ b/Entry.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +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 ); diff --git a/Makefile b/Makefile index eadc765..04acc88 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,12 @@ CPPFLAGS+= -I. LDFLAGS+= -Wl,-rpath,'$$ORIGIN/' +OBJECTS= helpers.o format.o cudir.o + all: cudir +cudir: $(OBJECTS) + $(CXX) -o $@ $(OBJECTS) + clean: - rm -f cudir + rm -f cudir *.o diff --git a/cudir.cc b/cudir.cc index 6639dbb..a65a3e6 100644 --- a/cudir.cc +++ b/cudir.cc @@ -3,72 +3,11 @@ #include #include -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; } diff --git a/format.cc b/format.cc new file mode 100644 index 0000000..1c17deb --- /dev/null +++ b/format.cc @@ -0,0 +1,24 @@ +#include "Entry.h" + +#include +#include + +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(); +} + diff --git a/helpers.cc b/helpers.cc new file mode 100644 index 0000000..f9979ee --- /dev/null +++ b/helpers.cc @@ -0,0 +1,39 @@ +#include "Entry.h" + +#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::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(); +}