First usable version in static mode !
This commit is contained in:
parent
28751f9369
commit
41746288fc
7 changed files with 338 additions and 220 deletions
|
|
@ -60,7 +60,7 @@ class Display
|
|||
};
|
||||
|
||||
/// Returns a left-justified string containing str, with fillChar used to fill the blank space : "bla", 7 -> "bla ". If the string is longer than width, it is truncated.
|
||||
std::string LeftJustify(std::string const& str, unsigned int width, char fillChar = ' ');
|
||||
std::string LeftJustify(std::string str, unsigned int width, char fillChar = ' ');
|
||||
|
||||
/// Returns a centered string containing str, with fillChar used to fill the blank space : "bla", 7 -> " bla ". If the string is longer than width, it is truncated.
|
||||
std::string CenterString(std::string const& str, unsigned int width, char fillChar = ' ');
|
||||
|
|
@ -69,7 +69,7 @@ std::string CenterString(std::string const& str, unsigned int width, char fillCh
|
|||
std::string RightJustify(std::string const& str, unsigned int width, char fillChar = ' ');
|
||||
|
||||
/// Creates a progress bar
|
||||
std::string GenerateProgressBar(unsigned int width, unsigned int current, unsigned int total, bool showPercentage = true, std::string const& fillChar = "\u25A0");
|
||||
std::string GenerateProgressBar(size_t width, size_t current, size_t total, bool showPercentage = true, std::string const& fillChar = "\u25A0");
|
||||
|
||||
/// Converts a size in bytes to a human readable size (To, Go, Mo, ko, o).
|
||||
/// If SI_units is false, the size will be displayed using powers of 1024 instead of 1000, producing Mio, kio etc.
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ class TreeNodeDiskUsage
|
|||
void BuildTree(bool verbose = false, unsigned int screenWidth = 80);
|
||||
|
||||
/// Prints the tree to the standard output only including the names of the nodes
|
||||
void PrintTree(unsigned int maxDepth = 0xFFFFFFFF, unsigned int depth = 0) const;
|
||||
void PrintTree(unsigned int maxDepth = 0xFFFFFFFF, bool humanReadableSizes = true, size_t parentSize = 0, unsigned int depth = 0) const;
|
||||
|
||||
/// Returns the number of children of the node.
|
||||
size_t GetChildrenCount() const;
|
||||
|
|
@ -50,11 +50,11 @@ class TreeNodeDiskUsage
|
|||
/// Returns the path of the node : /path/to/node.bla
|
||||
std::string GetNodePath() const;
|
||||
|
||||
/// Sorts the children of the node by size in descending order. Non-recursive.
|
||||
void SortBySizeDesc();
|
||||
/// Sorts the children of the node by size in descending order.
|
||||
void SortBySizeDesc(bool recursive = false);
|
||||
|
||||
/// Sorts the children of the node by name in ascending order. Non-recursive.
|
||||
void SortByNameAsc();
|
||||
/// Sorts the children of the node by name in ascending order.
|
||||
void SortByNameAsc(bool recursive = false);
|
||||
|
||||
/// Operator used to sort the nodes by size in descending order. Returns true when a.totalSize > b.totalSize.
|
||||
static bool SortOperatorSizeDesc(TreeNodeDiskUsage const& a, TreeNodeDiskUsage const& b);
|
||||
|
|
|
|||
17
include/test.hpp
Normal file
17
include/test.hpp
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef H_Test
|
||||
#define H_Test
|
||||
|
||||
#include <iostream>
|
||||
#include <TreeNodeDiskUsage.hpp>
|
||||
#include <Display.hpp>
|
||||
#include <AnsiTerminal.hpp>
|
||||
|
||||
using std::cout;
|
||||
using std::cerr;
|
||||
using std::endl;
|
||||
|
||||
#ifdef D_UNIT_TESTS
|
||||
void runTests(int argc, char *argv[]);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
@ -70,12 +70,10 @@ winsize Display::GetTerminalSize()
|
|||
|
||||
void Display::ClearScreen() { cout << "\033[2J\033[1;1H"; }
|
||||
|
||||
std::string LeftJustify(std::string const& str, unsigned int width, char fillChar)
|
||||
std::string LeftJustify(std::string str, unsigned int width, char fillChar)
|
||||
{
|
||||
if(str.size() <= width)
|
||||
return str + std::string(width - str.size(), fillChar);
|
||||
else
|
||||
return str.substr(0, width);
|
||||
str.resize(width, fillChar);
|
||||
return str;
|
||||
}
|
||||
|
||||
std::string CenterString(std::string const& str, unsigned int width, char fillChar)
|
||||
|
|
@ -97,20 +95,18 @@ std::string RightJustify(std::string const& str, unsigned int width, char fillCh
|
|||
return str.substr(0, width);
|
||||
}
|
||||
|
||||
std::string GenerateProgressBar(unsigned int width, unsigned int current, unsigned int total, bool showPercentage, std::string const& fillChar)
|
||||
std::string GenerateProgressBar(size_t width, size_t current, size_t total, bool showPercentage, std::string const& fillChar)
|
||||
{
|
||||
std::string progressBar;
|
||||
unsigned int freeWidth = width-2;
|
||||
size_t freeWidth = width-2;
|
||||
char buf[8];
|
||||
|
||||
if(total == 0) // Protect against divisions by 0
|
||||
total = 1;
|
||||
|
||||
if(current > total) // Protect against > 100 %
|
||||
current = total;
|
||||
|
||||
if(total == 0) // Protect against divisions by 0
|
||||
total = 1;
|
||||
|
||||
progressBar = '[';
|
||||
|
||||
if(showPercentage)
|
||||
{
|
||||
double percentCurrent = 100.*((double)current/(double)total);
|
||||
|
|
@ -118,13 +114,14 @@ std::string GenerateProgressBar(unsigned int width, unsigned int current, unsign
|
|||
sprintf(buf, "%5.1f%% ", percentCurrent);
|
||||
progressBar += buf;
|
||||
}
|
||||
|
||||
unsigned int scaledCurrent = (current*freeWidth)/total;
|
||||
|
||||
for(unsigned int i = 0 ; i < scaledCurrent ; i++)
|
||||
progressBar += '[';
|
||||
|
||||
size_t scaledCurrent = (current*freeWidth)/total;
|
||||
|
||||
for(size_t i = 0 ; i < scaledCurrent ; i++)
|
||||
progressBar += fillChar;
|
||||
for(unsigned int i = scaledCurrent ; i < freeWidth ; i++)
|
||||
progressBar += ' ';
|
||||
progressBar += std::string(freeWidth - scaledCurrent, ' ');
|
||||
|
||||
progressBar += ']';
|
||||
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ void TreeNodeDiskUsage::BuildTree(bool verbose, unsigned int screenWidth)
|
|||
}
|
||||
}
|
||||
|
||||
void TreeNodeDiskUsage::PrintTree(unsigned int maxDepth, unsigned int depth) const
|
||||
void TreeNodeDiskUsage::PrintTree(unsigned int maxDepth, bool humanReadableSizes, size_t parentSize, unsigned int depth) const
|
||||
{
|
||||
if(depth > maxDepth)
|
||||
return;
|
||||
|
|
@ -98,11 +98,46 @@ void TreeNodeDiskUsage::PrintTree(unsigned int maxDepth, unsigned int depth) con
|
|||
paddedPath = " " + paddedPath;
|
||||
|
||||
unsigned int screenWidth = Display::GetTerminalSize().ws_col;
|
||||
std::cout << std::setw(2*screenWidth/5) << std::left << paddedPath
|
||||
<< std::setw(screenWidth/6) << std::right << totalSize << std::setw(screenWidth/6) << totalSizeOnDisk << std::setw(screenWidth/6) << totalElements << "\n";
|
||||
unsigned int pathWidth = screenWidth/2;
|
||||
unsigned int fieldWidth1 = (screenWidth - pathWidth)/4;
|
||||
unsigned int fieldWidth2 = fieldWidth1;
|
||||
|
||||
if(humanReadableSizes)
|
||||
{
|
||||
fieldWidth1 = 8;
|
||||
fieldWidth2 = (screenWidth - pathWidth - 2*fieldWidth1 - 3)/2;
|
||||
}
|
||||
|
||||
if(depth == 0)
|
||||
{
|
||||
std::cout << std::setw(fieldWidth1) << std::right << "Size"
|
||||
<< CenterString("Percentage", fieldWidth2)
|
||||
<< std::setw(pathWidth) << std::left << "Path"
|
||||
<< std::setw(fieldWidth1) << "Size on disk"
|
||||
<< std::setw(fieldWidth2) << std::right << "Elements contained" << "\n"
|
||||
<< std::string(screenWidth, '-') << "\n";
|
||||
parentSize = totalSize;
|
||||
}
|
||||
|
||||
std::string progressBar = GenerateProgressBar(fieldWidth2, totalSize, parentSize, true, "#");
|
||||
|
||||
if(humanReadableSizes)
|
||||
std::cout << std::setw(fieldWidth1) << std::right << Bytes2HumanReadable(totalSize) << ' '
|
||||
<< std::setw(fieldWidth2) << progressBar << ' '
|
||||
<< std::setw(pathWidth) << std::left << paddedPath
|
||||
<< std::setw(fieldWidth1) << Bytes2HumanReadable(totalSizeOnDisk)
|
||||
<< std::setw(fieldWidth2) << std::right << totalElements
|
||||
<< "\n";
|
||||
else
|
||||
std::cout << std::setw(fieldWidth1) << std::right << (totalSize) << ' '
|
||||
<< std::setw(fieldWidth2) << progressBar << ' '
|
||||
<< std::setw(pathWidth) << std::left << paddedPath
|
||||
<< std::setw(fieldWidth1) << (totalSizeOnDisk)
|
||||
<< std::setw(fieldWidth2) << std::right << totalElements
|
||||
<< "\n";
|
||||
if(isFolder)
|
||||
for(unsigned int i = 0 ; i < children.size() ; i++)
|
||||
children[i].PrintTree(maxDepth, depth+1);
|
||||
children[i].PrintTree(maxDepth, humanReadableSizes, totalSize, depth+1);
|
||||
}
|
||||
|
||||
size_t TreeNodeDiskUsage::GetChildrenCount() const { return children.size(); }
|
||||
|
|
@ -130,8 +165,21 @@ std::string TreeNodeDiskUsage::GetNodeName() const
|
|||
return path.substr(lastSepPos+1, path.size()-lastSepPos-1);
|
||||
}
|
||||
|
||||
void TreeNodeDiskUsage::SortBySizeDesc() { std::sort(children.begin(), children.end(), TreeNodeDiskUsage::SortOperatorSizeDesc); }
|
||||
void TreeNodeDiskUsage::SortByNameAsc() { std::sort(children.begin(), children.end(), TreeNodeDiskUsage::SortOperatorNameAsc); }
|
||||
void TreeNodeDiskUsage::SortBySizeDesc(bool recursive)
|
||||
{
|
||||
std::sort(children.begin(), children.end(), TreeNodeDiskUsage::SortOperatorSizeDesc);
|
||||
if(recursive)
|
||||
for(unsigned int i = 0 ; i < children.size() ; i++)
|
||||
children[i].SortBySizeDesc(recursive);
|
||||
}
|
||||
|
||||
void TreeNodeDiskUsage::SortByNameAsc(bool recursive)
|
||||
{
|
||||
std::sort(children.begin(), children.end(), TreeNodeDiskUsage::SortOperatorNameAsc);
|
||||
if(recursive)
|
||||
for(unsigned int i = 0 ; i < children.size() ; i++)
|
||||
children[i].SortByNameAsc(recursive);
|
||||
}
|
||||
|
||||
bool TreeNodeDiskUsage::SortOperatorSizeDesc(TreeNodeDiskUsage const& a, TreeNodeDiskUsage const& b) { return a.totalSize > b.totalSize; }
|
||||
bool TreeNodeDiskUsage::SortOperatorNameAsc(TreeNodeDiskUsage const& a, TreeNodeDiskUsage const& b) { return a.GetNodeName() < b.GetNodeName(); }
|
||||
|
|
|
|||
246
src/main.cpp
246
src/main.cpp
|
|
@ -9,213 +9,79 @@ using std::endl;
|
|||
|
||||
//#define D_UNIT_TESTS
|
||||
|
||||
#ifdef D_UNIT_TESTS
|
||||
void runTests(int argc, char *argv[]);
|
||||
#endif
|
||||
#include <test.hpp>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
#ifdef D_UNIT_TESTS
|
||||
runTests(argc, argv);
|
||||
#else
|
||||
runTests(argc, argv);
|
||||
return 0;
|
||||
#endif
|
||||
|
||||
// Main program variables and parameters
|
||||
std::string rootpath = ".";
|
||||
bool staticMode = false;
|
||||
unsigned int maxDepth = 0xFFFFFFFF;// used for static mode
|
||||
|
||||
// Parse arguments
|
||||
if(argc > 1)
|
||||
{
|
||||
rootpath = argv[1];
|
||||
bool pathFound = false;
|
||||
for(int i = 1 ; i < argc ; i++)
|
||||
{
|
||||
std::string arg = argv[i];
|
||||
if(arg == "-r" || arg == "--recursion-limit")
|
||||
{
|
||||
if(i+1 < argc)
|
||||
{
|
||||
maxDepth = atoi(argv[++i]);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
cerr << "Error : option \"-r\" must be followed by the recursion limit !\n";
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else if(arg == "-s" || arg == "--static")
|
||||
staticMode = true;
|
||||
else if(arg == "-h" || arg == "--help")
|
||||
{
|
||||
cout << "usage : DiskUsageInteractive [-s] [-r recursion_limit] path\n"
|
||||
" -s --static Prints the file tree to the standard output.\n"
|
||||
" -r recursion_limit --recursion-limit recursion_limit Sets the recursion limit for the printing. Level 0 is the root node.\n"
|
||||
" -h --help Prints this help message.\n";
|
||||
exit(0);
|
||||
}
|
||||
else if(!pathFound) // lonely parameter, it must be the root path, right ?
|
||||
{
|
||||
rootpath = arg;
|
||||
pathFound = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Display display;
|
||||
|
||||
// Scan root path
|
||||
TreeNodeDiskUsage tree(rootpath);
|
||||
cout << "Scanning folder \"" << rootpath << "\" ...\n";
|
||||
tree.BuildTree(true, display.Cols());
|
||||
if(staticMode)
|
||||
tree.BuildTree(false, display.Cols());
|
||||
else
|
||||
{
|
||||
cout << "Scanning folder \"" << rootpath << "\" ...\n";
|
||||
tree.BuildTree(true, display.Cols());
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
// DEBUG
|
||||
tree.PrintTree(1);
|
||||
tree.SortBySizeDesc(); tree.PrintTree(1);
|
||||
tree.SortByNameAsc(); tree.PrintTree(1);
|
||||
tree.SortBySizeDesc(true);
|
||||
|
||||
// Static mode : the tree is displayed in the standard output.
|
||||
if(staticMode)
|
||||
tree.PrintTree(maxDepth, true);
|
||||
|
||||
#endif // D_UNIT_TESTS
|
||||
// Interactive mode : the user can navigate through the tree structure.
|
||||
// else
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef D_UNIT_TESTS
|
||||
void runTests(int /* argc */, char *argv[])
|
||||
{
|
||||
if(0)
|
||||
{// stat test
|
||||
cout << "Reading '" << argv[1] << "'\n";
|
||||
struct stat s;
|
||||
PRINT_VAR(stat(argv[1], &s));
|
||||
PRINT_VAR(s.st_mode);
|
||||
PRINT_VAR(s.st_mode & S_IFMT);
|
||||
PRINT_VAR((s.st_mode & S_IFMT) == S_IFLNK);// symbolic link
|
||||
PRINT_VAR((s.st_mode & S_IFMT) == S_IFREG);// regular file
|
||||
PRINT_VAR((s.st_mode & S_IFMT) == S_IFDIR);// folder
|
||||
PRINT_VAR(s.st_size);
|
||||
PRINT_VAR(s.st_blksize);
|
||||
PRINT_VAR(s.st_blocks);
|
||||
PRINT_VAR(s.st_blocks*512);// Size on disk, in bytes
|
||||
}
|
||||
if(0)
|
||||
{// test GetNodeName()
|
||||
PRINT_VAR(TreeNodeDiskUsage("/some/path/to/victory.tar.gz").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/some/path/to/a/folder").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/some/path/to/a/folder/").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage(".").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("..").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/a").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/a/b/c//////////").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/a///b//c/").GetNodeName());
|
||||
}
|
||||
if(0)
|
||||
{// test GetNodePath()
|
||||
PRINT_VAR(TreeNodeDiskUsage("/a/b/c//////////").GetNodePath());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/a///b//c/").GetNodePath());
|
||||
PRINT_VAR(TreeNodeDiskUsage("////a///b//c/").GetNodePath());
|
||||
PRINT_VAR(TreeNodeDiskUsage(" /a/b/c ").GetNodePath());
|
||||
PRINT_VAR(TreeNodeDiskUsage(" /a/b/c/// ").GetNodePath());
|
||||
PRINT_VAR(TreeNodeDiskUsage(" \\this\\\\is/sparta/// ").GetNodePath());
|
||||
PRINT_VAR(TreeNodeDiskUsage(" \\this\\\\is/sparta/with spaces// ").GetNodePath());
|
||||
}
|
||||
if(0)
|
||||
{// basic tree building and printing test
|
||||
TreeNodeDiskUsage tree(argv[1]);
|
||||
tree.BuildTree();
|
||||
tree.PrintTree();
|
||||
cout << "\n\n";
|
||||
PRINT_VAR(tree.GetTotalSize());
|
||||
PRINT_VAR(tree.GetTotalSizeOnDisk());
|
||||
cout << "\n\n";
|
||||
tree.PrintTree(0);
|
||||
tree.PrintTree(1);
|
||||
tree.PrintTree(2);
|
||||
}
|
||||
if(0)
|
||||
{// terminal display functions test
|
||||
Display d;
|
||||
PRINT_VAR(d.Rows());
|
||||
PRINT_VAR(d.Cols());
|
||||
|
||||
d.ClearScreen();
|
||||
cout << "coucou !\n";
|
||||
}
|
||||
if(0)
|
||||
{// progress bar test
|
||||
// unsigned int width, unsigned int current, unsigned int total, char fillChar = 219, bool showPercentage = true
|
||||
PRINT_VAR(GenerateProgressBar(20, 0, 100, false));
|
||||
PRINT_VAR(GenerateProgressBar(20, 25, 100,false));
|
||||
PRINT_VAR(GenerateProgressBar(20, 50, 100, false));
|
||||
PRINT_VAR(GenerateProgressBar(20, 75, 100, false));
|
||||
PRINT_VAR(GenerateProgressBar(20, 100, 100, false));
|
||||
PRINT_VAR(GenerateProgressBar(50, 666, 1000, false));
|
||||
|
||||
PRINT_VAR(GenerateProgressBar(20, 0, 100, true));
|
||||
PRINT_VAR(GenerateProgressBar(20, 25, 100, true));
|
||||
PRINT_VAR(GenerateProgressBar(20, 50, 100, true));
|
||||
PRINT_VAR(GenerateProgressBar(20, 75, 100, true));
|
||||
PRINT_VAR(GenerateProgressBar(20, 100, 100, true));
|
||||
PRINT_VAR(GenerateProgressBar(50, 666, 1000, true));
|
||||
PRINT_VAR(GenerateProgressBar(50, 1666, 1000, true));
|
||||
PRINT_VAR(GenerateProgressBar(50, 42, 0, true));
|
||||
PRINT_VAR(GenerateProgressBar(50, 0, 0, true));
|
||||
|
||||
PRINT_VAR(GenerateProgressBar(50, 333, 1000, true, "#"));
|
||||
}
|
||||
if(0)
|
||||
{// test output
|
||||
PRINT_VAR(Bytes2HumanReadable(123ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890123ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890123456ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890123456789ULL, true));
|
||||
cout << "\n";
|
||||
PRINT_VAR(Bytes2HumanReadable(123ULL, false));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234ULL, false));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567ULL, false));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890ULL, false));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890123ULL, false));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890123456ULL, false));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890123456789ULL, false));
|
||||
cout << "\n";
|
||||
PRINT_VAR(Bytes2HumanReadable(1073741824ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1073741824ULL, false));
|
||||
PRINT_VAR(Bytes2HumanReadable(1000000000ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1000000000ULL, false));
|
||||
|
||||
}
|
||||
//if(0)
|
||||
{// test screen functions
|
||||
Display d;
|
||||
PRINT_VAR(d.Rows());
|
||||
PRINT_VAR(d.Cols());
|
||||
PRINT_VAR(d.GetLine(0));
|
||||
PRINT_VAR(d.GetLine(0).size());
|
||||
|
||||
d.GetPixel(5,5) = '#';
|
||||
d.GetPixel(6,6) = '#';
|
||||
d.GetPixel(7,7) = '#';
|
||||
d.GetPixel(20,50) = '#';
|
||||
|
||||
d.SetLine(4, "Coucou !");
|
||||
d.SetLine(10, "------------------------- Coucou ! ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
|
||||
d.SetLine(11, "------------------------- Inverted -------------------------");
|
||||
d.HighlightLine(11, true);
|
||||
d.SetLine(12, " That's normal, bro. Really !");
|
||||
d.SetLineCentered(13, "Bonjour tout le monde.");
|
||||
d.SetLineCentered(14, "Tralala c'est moi la pute.", '-');
|
||||
d.SetLineRjustified(15, "Tralala c'est moi la pute, mais a droite de l'ecran.", '-');
|
||||
|
||||
Display::ClearScreen();
|
||||
d.DrawScreenLines();
|
||||
|
||||
PRINT_VAR(d.GetLine(12).size());
|
||||
d.HighlightLine(12, true);
|
||||
PRINT_VAR(d.GetLine(12).size());
|
||||
d.HighlightLine(12, true);
|
||||
PRINT_VAR(d.GetLine(12).size());
|
||||
d.HighlightLine(12, false);
|
||||
PRINT_VAR(d.GetLine(12).size());
|
||||
PRINT_VAR(d.GetLine(12));
|
||||
PRINT_VAR(std::string(C_INVERT_FG_BG).size());
|
||||
}
|
||||
if(0)
|
||||
{// test terminal colors
|
||||
cout << C_BLUE_F << "blue" << "\n";
|
||||
cout << C_CYAN_F << "cyan" << "\n";
|
||||
cout << C_YELLOW_F << "yellow" << "\n";
|
||||
cout << C_WHITE_F << "white" << "\n";
|
||||
cout << C_RED_F << "red" << "\n";
|
||||
cout << C_GREEN_F << "green" << "\n";
|
||||
cout << C_BLACK_F << "black" << "\n";
|
||||
cout << C_RESET << "Normal" << "\n";
|
||||
cout << C_BOLD << "Bold text." << C_RESET << " Normal text." << "\n";
|
||||
|
||||
cout << C_BLUE_B << C_YELLOW_F << "Hard on the eyes..." << C_RESET << "\n";
|
||||
cout << C_UNDERLINE << "underlined" << C_RESET << "\n";
|
||||
cout << C_INVERT_FG_BG << "inverted" << C_RESET << "\n";
|
||||
|
||||
cout << "\n\n";
|
||||
|
||||
{// Nice color table
|
||||
int i, j, n;
|
||||
for (i = 0; i < 11; i++) {
|
||||
for (j = 0; j < 10; j++) {
|
||||
n = 10 * i + j;
|
||||
if (n > 107) break;
|
||||
printf("\033[%dm %3d\033[m", n, n);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // D_UNIT_TESTS
|
||||
190
src/test.cpp
Normal file
190
src/test.cpp
Normal file
|
|
@ -0,0 +1,190 @@
|
|||
|
||||
#ifdef D_UNIT_TESTS
|
||||
void runTests(int /* argc */, char *argv[])
|
||||
{
|
||||
if(0)
|
||||
{// stat test
|
||||
cout << "Reading '" << argv[1] << "'\n";
|
||||
struct stat s;
|
||||
PRINT_VAR(stat(argv[1], &s));
|
||||
PRINT_VAR(s.st_mode);
|
||||
PRINT_VAR(s.st_mode & S_IFMT);
|
||||
PRINT_VAR((s.st_mode & S_IFMT) == S_IFLNK);// symbolic link
|
||||
PRINT_VAR((s.st_mode & S_IFMT) == S_IFREG);// regular file
|
||||
PRINT_VAR((s.st_mode & S_IFMT) == S_IFDIR);// folder
|
||||
PRINT_VAR(s.st_size);
|
||||
PRINT_VAR(s.st_blksize);
|
||||
PRINT_VAR(s.st_blocks);
|
||||
PRINT_VAR(s.st_blocks*512);// Size on disk, in bytes
|
||||
}
|
||||
if(0)
|
||||
{// test GetNodeName()
|
||||
PRINT_VAR(TreeNodeDiskUsage("/some/path/to/victory.tar.gz").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/some/path/to/a/folder").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/some/path/to/a/folder/").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage(".").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("..").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/a").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/a/b/c//////////").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/a///b//c/").GetNodeName());
|
||||
}
|
||||
if(0)
|
||||
{// test GetNodePath()
|
||||
PRINT_VAR(TreeNodeDiskUsage("/a/b/c//////////").GetNodePath());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/a///b//c/").GetNodePath());
|
||||
PRINT_VAR(TreeNodeDiskUsage("////a///b//c/").GetNodePath());
|
||||
PRINT_VAR(TreeNodeDiskUsage(" /a/b/c ").GetNodePath());
|
||||
PRINT_VAR(TreeNodeDiskUsage(" /a/b/c/// ").GetNodePath());
|
||||
PRINT_VAR(TreeNodeDiskUsage(" \\this\\\\is/sparta/// ").GetNodePath());
|
||||
PRINT_VAR(TreeNodeDiskUsage(" \\this\\\\is/sparta/with spaces// ").GetNodePath());
|
||||
}
|
||||
if(0)
|
||||
{// basic tree building and printing test
|
||||
TreeNodeDiskUsage tree(argv[1]);
|
||||
cout << "Scanning folder \"" << rootpath << "\" ...\n";
|
||||
tree.BuildTree(true, display.Cols());
|
||||
cout << endl;
|
||||
|
||||
tree.PrintTree();
|
||||
cout << "\n\n";
|
||||
PRINT_VAR(tree.GetTotalSize());
|
||||
PRINT_VAR(tree.GetTotalSizeOnDisk());
|
||||
cout << "\n\n";
|
||||
tree.PrintTree(0);
|
||||
tree.PrintTree(1);
|
||||
tree.PrintTree(2);
|
||||
|
||||
cout << "Effects of sorting --------------------\n";
|
||||
tree.SortBySizeDesc(); tree.PrintTree(1);
|
||||
tree.SortByNameAsc(); tree.PrintTree(1);
|
||||
tree.PrintTree(1, false);
|
||||
|
||||
cout << "Recursive sorting --------------------\n";
|
||||
tree.SortBySizeDesc();
|
||||
tree.PrintTree(2);
|
||||
tree.SortBySizeDesc(true);
|
||||
tree.PrintTree(2);
|
||||
}
|
||||
if(0)
|
||||
{// terminal display functions test
|
||||
Display d;
|
||||
PRINT_VAR(d.Rows());
|
||||
PRINT_VAR(d.Cols());
|
||||
|
||||
d.ClearScreen();
|
||||
cout << "coucou !\n";
|
||||
}
|
||||
if(0)
|
||||
{// progress bar test
|
||||
// unsigned int width, unsigned int current, unsigned int total, char fillChar = 219, bool showPercentage = true
|
||||
PRINT_VAR(GenerateProgressBar(20, 0, 100, false));
|
||||
PRINT_VAR(GenerateProgressBar(20, 25, 100,false));
|
||||
PRINT_VAR(GenerateProgressBar(20, 50, 100, false));
|
||||
PRINT_VAR(GenerateProgressBar(20, 75, 100, false));
|
||||
PRINT_VAR(GenerateProgressBar(20, 100, 100, false));
|
||||
PRINT_VAR(GenerateProgressBar(50, 666, 1000, false));
|
||||
|
||||
PRINT_VAR(GenerateProgressBar(20, 0, 100, true));
|
||||
PRINT_VAR(GenerateProgressBar(20, 25, 100, true));
|
||||
PRINT_VAR(GenerateProgressBar(20, 50, 100, true));
|
||||
PRINT_VAR(GenerateProgressBar(20, 75, 100, true));
|
||||
PRINT_VAR(GenerateProgressBar(20, 100, 100, true));
|
||||
PRINT_VAR(GenerateProgressBar(50, 666, 1000, true));
|
||||
PRINT_VAR(GenerateProgressBar(50, 1666, 1000, true));
|
||||
PRINT_VAR(GenerateProgressBar(50, 42, 0, true));
|
||||
PRINT_VAR(GenerateProgressBar(50, 0, 0, true));
|
||||
|
||||
PRINT_VAR(GenerateProgressBar(50, 333, 1000, true, "#"));
|
||||
}
|
||||
if(0)
|
||||
{// test output
|
||||
PRINT_VAR(Bytes2HumanReadable(123ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890123ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890123456ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890123456789ULL, true));
|
||||
cout << "\n";
|
||||
PRINT_VAR(Bytes2HumanReadable(123ULL, false));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234ULL, false));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567ULL, false));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890ULL, false));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890123ULL, false));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890123456ULL, false));
|
||||
PRINT_VAR(Bytes2HumanReadable(1234567890123456789ULL, false));
|
||||
cout << "\n";
|
||||
PRINT_VAR(Bytes2HumanReadable(1073741824ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1073741824ULL, false));
|
||||
PRINT_VAR(Bytes2HumanReadable(1000000000ULL, true));
|
||||
PRINT_VAR(Bytes2HumanReadable(1000000000ULL, false));
|
||||
|
||||
}
|
||||
//if(0)
|
||||
{// test screen functions
|
||||
Display d;
|
||||
PRINT_VAR(d.Rows());
|
||||
PRINT_VAR(d.Cols());
|
||||
PRINT_VAR(d.GetLine(0));
|
||||
PRINT_VAR(d.GetLine(0).size());
|
||||
|
||||
d.GetPixel(5,5) = '#';
|
||||
d.GetPixel(6,6) = '#';
|
||||
d.GetPixel(7,7) = '#';
|
||||
d.GetPixel(20,50) = '#';
|
||||
|
||||
d.SetLine(4, "Coucou !");
|
||||
d.SetLine(10, "------------------------- Coucou ! ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
|
||||
d.SetLine(11, "------------------------- Inverted -------------------------");
|
||||
d.HighlightLine(11, true);
|
||||
d.SetLine(12, " That's normal, bro. Really !");
|
||||
d.SetLineCentered(13, "Bonjour tout le monde.");
|
||||
d.SetLineCentered(14, "Tralala c'est moi la pute.", '-');
|
||||
d.SetLineRjustified(15, "Tralala c'est moi la pute, mais a droite de l'ecran.", '-');
|
||||
|
||||
Display::ClearScreen();
|
||||
d.DrawScreenLines();
|
||||
|
||||
PRINT_VAR(d.GetLine(12).size());
|
||||
d.HighlightLine(12, true);
|
||||
PRINT_VAR(d.GetLine(12).size());
|
||||
d.HighlightLine(12, true);
|
||||
PRINT_VAR(d.GetLine(12).size());
|
||||
d.HighlightLine(12, false);
|
||||
PRINT_VAR(d.GetLine(12).size());
|
||||
PRINT_VAR(d.GetLine(12));
|
||||
PRINT_VAR(std::string(C_INVERT_FG_BG).size());
|
||||
}
|
||||
if(0)
|
||||
{// test terminal colors
|
||||
cout << C_BLUE_F << "blue" << "\n";
|
||||
cout << C_CYAN_F << "cyan" << "\n";
|
||||
cout << C_YELLOW_F << "yellow" << "\n";
|
||||
cout << C_WHITE_F << "white" << "\n";
|
||||
cout << C_RED_F << "red" << "\n";
|
||||
cout << C_GREEN_F << "green" << "\n";
|
||||
cout << C_BLACK_F << "black" << "\n";
|
||||
cout << C_RESET << "Normal" << "\n";
|
||||
cout << C_BOLD << "Bold text." << C_RESET << " Normal text." << "\n";
|
||||
|
||||
cout << C_BLUE_B << C_YELLOW_F << "Hard on the eyes..." << C_RESET << "\n";
|
||||
cout << C_UNDERLINE << "underlined" << C_RESET << "\n";
|
||||
cout << C_INVERT_FG_BG << "inverted" << C_RESET << "\n";
|
||||
|
||||
cout << "\n\n";
|
||||
|
||||
{// Nice color table
|
||||
int i, j, n;
|
||||
for (i = 0; i < 11; i++) {
|
||||
for (j = 0; j < 10; j++) {
|
||||
n = 10 * i + j;
|
||||
if (n > 107) break;
|
||||
printf("\033[%dm %3d\033[m", n, n);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // D_UNIT_TESTS
|
||||
Loading…
Add table
Reference in a new issue