500e82f812
Just use 4 characters, it's fine. :-)
123 lines
3.7 KiB
C++
123 lines
3.7 KiB
C++
#include <iostream>
|
|
#include <iomanip>
|
|
#include <vector>
|
|
#include <chrono>
|
|
#include <algorithm>
|
|
#include <filesystem>
|
|
|
|
#include "Entry.h"
|
|
|
|
auto
|
|
getEntries( const std::filesystem::path where )
|
|
{
|
|
std::vector< Entry > entries;
|
|
|
|
for( const auto &entry: std::filesystem::directory_iterator{ where } )
|
|
{
|
|
const auto time= std::filesystem::file_time_type::clock::to_sys( last_write_time( entry.path() ) );
|
|
const auto day= std::chrono::floor< std::chrono::days >( time );
|
|
const std::chrono::year_month_day ymd{ day };
|
|
const auto time_since_midnight= day - time;
|
|
const std::chrono::hh_mm_ss time_of_day{ std::chrono::floor< std::chrono::minutes >( time_since_midnight ) };
|
|
|
|
entries.push_back( Entry{ entry.path().filename(), drwx( entry ), entry.is_directory() ? 0 : file_size( entry.path() ),
|
|
static_cast< int >( ymd.year() ), static_cast< unsigned >( ymd.month() ), static_cast< unsigned >( ymd.day() ),
|
|
time_of_day.hours().count(), time_of_day.minutes().count(),
|
|
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;
|
|
}
|
|
|
|
using namespace std::literals::string_literals;
|
|
|
|
namespace C
|
|
{
|
|
constexpr int sizeWidth= 14;
|
|
constexpr int dateWidth= "1982-12-21"s.size();
|
|
constexpr int timeWidth= "12:34"s.size();
|
|
}
|
|
|
|
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 + C::filePadding + C::sizeWidth + 2 + 10 + 2 + C::dateWidth + 1 + C::timeWidth;
|
|
|
|
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;
|
|
|
|
std::size_t fileCount= 0;
|
|
std::size_t totalSize= 0;
|
|
std::size_t totalStorage= 0;
|
|
bool side= false;
|
|
for( const auto &entry: entries )
|
|
{
|
|
++fileCount;
|
|
totalSize+= entry.size;
|
|
std::cout << format_entry( entry, widest ) << "[m ";
|
|
if( entry.is_dir )
|
|
{
|
|
std::cout << "[1;35;95m";
|
|
std::cout << std::setw( C::sizeWidth - 1 ) << std::setfill( ' ' ) << std::right << "<DIR>" << ' ';
|
|
}
|
|
else
|
|
{
|
|
//std::cout << " ";
|
|
std::cout << "\e[1;33;93m" << std::setw( C::sizeWidth ) << std::setfill( ' ' ) << entry.size;
|
|
}
|
|
std::cout << "[m ";
|
|
std::cout << "[1;34;94m" << entry.perms << "[m";
|
|
std::cout << " ";
|
|
|
|
// Date
|
|
std::cout << "\e[1;31;91m";
|
|
std::cout << std::setw( 2 ) << std::setfill( '0' ) << entry.year % 10000 << '-';
|
|
std::cout << std::setw( 2 ) << std::setfill( '0' ) << entry.month << '-';
|
|
std::cout << std::setw( 2 ) << std::setfill( '0' ) << entry.day;
|
|
std::cout << "\e[m";
|
|
|
|
std::cout << ' ';
|
|
|
|
// Time
|
|
std::cout << "\e[1;32;92m";
|
|
std::cout << std::setw( 2 ) << std::setfill( '0' ) << entry.hour << ':';
|
|
std::cout << std::setw( 2 ) << std::setfill( '0' ) << entry.minute;
|
|
std::cout << "\e[m";
|
|
|
|
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;
|
|
|
|
std::cout << std::setw( C::sizeWidth ) << std::setfill( ' ' ) << std::right << "\e[;1;36;96m" << fileCount;
|
|
std::cout << "\e[;1;32;92m files totaling \e[;1;36;96m" << totalSize << "\e[;1;32;92m bytes.\e[m" << std::endl;
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|