Time & date support.
This commit is contained in:
@@ -13,6 +13,11 @@ struct Entry
|
|||||||
std::string name;
|
std::string name;
|
||||||
std::string perms;
|
std::string perms;
|
||||||
std::size_t size;
|
std::size_t size;
|
||||||
|
int year;
|
||||||
|
unsigned month;
|
||||||
|
unsigned day;
|
||||||
|
long int hour;
|
||||||
|
long int minute;
|
||||||
bool is_dir;
|
bool is_dir;
|
||||||
bool is_exe;
|
bool is_exe;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <chrono>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
@@ -12,7 +14,16 @@ getEntries( const std::filesystem::path where )
|
|||||||
|
|
||||||
for( const auto &entry: std::filesystem::directory_iterator{ where } )
|
for( const auto &entry: std::filesystem::directory_iterator{ where } )
|
||||||
{
|
{
|
||||||
entries.emplace_back( entry.path().filename(), drwx( entry ), entry.is_directory() ? 0 : file_size( entry.path() ), entry.is_directory(), is_exe( entry ) );
|
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; } );
|
std::sort( begin( entries ), end( entries ), []( const auto &lhs, const auto &rhs ) { return lhs.name < rhs.name; } );
|
||||||
@@ -20,9 +31,13 @@ getEntries( const std::filesystem::path where )
|
|||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
using namespace std::literals::string_literals;
|
||||||
|
|
||||||
namespace C
|
namespace C
|
||||||
{
|
{
|
||||||
constexpr int dirwidth= 9;
|
constexpr int dirwidth= 9;
|
||||||
|
constexpr int dateWidth= "82-12-21"s.size();
|
||||||
|
constexpr int timeWidth= "12:34"s.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@@ -35,7 +50,7 @@ main()
|
|||||||
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 + C::filePadding + C::dirwidth + 3 + 10 + 2;
|
const int rowWidth= widest + C::filePadding + C::dirwidth + 3 + 10 + 2 + C::dateWidth + 1 + C::timeWidth;
|
||||||
|
|
||||||
std::cout << "[1;36;96m";
|
std::cout << "[1;36;96m";
|
||||||
for( int i= 0; i < rowWidth; ++i ) std::cout << "\u2500";
|
for( int i= 0; i < rowWidth; ++i ) std::cout << "\u2500";
|
||||||
@@ -50,16 +65,32 @@ main()
|
|||||||
if( entry.is_dir )
|
if( entry.is_dir )
|
||||||
{
|
{
|
||||||
std::cout << "[1;35;95m";
|
std::cout << "[1;35;95m";
|
||||||
std::cout << std::setw( C::dirwidth - 2 ) << std::right << "<DIR>" << ' ';
|
std::cout << std::setw( C::dirwidth - 2 ) << std::setfill( ' ' ) << std::right << "<DIR>" << ' ';
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//std::cout << " ";
|
//std::cout << " ";
|
||||||
std::cout << "\e[1;33;93m" << std::setw( C::dirwidth - 1 ) << entry.size;
|
std::cout << "\e[1;33;93m" << std::setw( C::dirwidth - 1 ) << std::setfill( ' ' ) << entry.size;
|
||||||
}
|
}
|
||||||
std::cout << "[m ";
|
std::cout << "[m ";
|
||||||
std::cout << "[1;34;94m" << entry.perms << "[m";
|
std::cout << "[1;34;94m" << entry.perms << "[m";
|
||||||
std::cout << " ";
|
std::cout << " ";
|
||||||
|
|
||||||
|
// Date
|
||||||
|
std::cout << "\e[1;31;91m";
|
||||||
|
std::cout << std::setw( 2 ) << std::setfill( '0' ) << entry.year % 100 << '-';
|
||||||
|
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 )
|
if( side == false )
|
||||||
{
|
{
|
||||||
std::cout << "[1;36;96m\u2502[m";
|
std::cout << "[1;36;96m\u2502[m";
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ format_entry( const Entry &entry, const std::size_t widest )
|
|||||||
{
|
{
|
||||||
oss << "1;36;96";
|
oss << "1;36;96";
|
||||||
}
|
}
|
||||||
oss << 'm' << std::setw( widest + C::filePadding ) << std::left << entry.name;
|
oss << 'm' << std::setw( widest + C::filePadding ) << std::setfill( ' ' ) << std::left << entry.name;
|
||||||
|
|
||||||
return std::move( oss ).str();
|
return std::move( oss ).str();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user