First working interactive version.

This commit is contained in:
Your Name 2022-01-22 23:24:59 +01:00
parent 9e6579c57b
commit fe8afe3f87
5 changed files with 88 additions and 31 deletions

View file

@ -32,6 +32,16 @@ class EventManager
void CallbackHome(); void CallbackHome();
void CallbackHelp(); void CallbackHelp();
void CallbackSort(); void CallbackSort();
void CallbackUnits();
/// Computes the visibility of the selected line and scrolls the element list if needed.
void ScrollList();
/// Sorts the elements of the current node according to the sorting method currently selected.
void SortNodeElements();
/// Updates the screen based on the current state.
void UpdateScreen();
void DebugPrintState(); void DebugPrintState();
@ -43,6 +53,7 @@ class EventManager
int currentLine; //<! Currently selected line int currentLine; //<! Currently selected line
TreeNodeDiskUsage *currentNode; //<! Pointer to the currently displayed node. TreeNodeDiskUsage *currentNode; //<! Pointer to the currently displayed node.
int sortType; //<! Entry sort type : 0 -> size descending, 1 -> name ascending int sortType; //<! Entry sort type : 0 -> size descending, 1 -> name ascending
bool SI_units; //<! Indicates whether SI units should be used.
}; };
#endif #endif

View file

@ -56,6 +56,8 @@ void Display::UpdateScreenSize()
} }
void Display::HighlightLine(unsigned int i, bool highlight) void Display::HighlightLine(unsigned int i, bool highlight)
{
if(i < lines.size())
{ {
if(highlight) if(highlight)
{ {
@ -68,6 +70,7 @@ void Display::HighlightLine(unsigned int i, bool highlight)
lines[i] = lines[i].substr(4, lines[i].size()-4-4); lines[i] = lines[i].substr(4, lines[i].size()-4-4);
} }
} }
}
void Display::DisplayTreeNode(TreeNodeDiskUsage const& treeNode, size_t topLine, bool SI_units) void Display::DisplayTreeNode(TreeNodeDiskUsage const& treeNode, size_t topLine, bool SI_units)
{ {
@ -100,6 +103,9 @@ void Display::DisplayTreeNode(TreeNodeDiskUsage const& treeNode, size_t topLine,
lines[i+2] = LeftJustify(lineStream.str(), screenCols); lines[i+2] = LeftJustify(lineStream.str(), screenCols);
} }
if(!treeNode.GetChildrenCount())
lines[2] = LeftJustify(" [Empty directory]", screenCols);
// Write footer // Write footer
std::stringstream footerSstream; footerSstream << "Total disk usage: " << Bytes2HumanReadable(treeNode.GetTotalSizeOnDisk(), SI_units) std::stringstream footerSstream; footerSstream << "Total disk usage: " << Bytes2HumanReadable(treeNode.GetTotalSizeOnDisk(), SI_units)
<< " Apparent size: " << Bytes2HumanReadable(treeNode.GetTotalSize(), SI_units) << " Apparent size: " << Bytes2HumanReadable(treeNode.GetTotalSize(), SI_units)

View file

@ -15,8 +15,12 @@ EventManager::EventManager(Display & display_, TreeNodeDiskUsage & rootNode_)
topLine(0), topLine(0),
currentLine(0), currentLine(0),
currentNode(&rootNode), currentNode(&rootNode),
sortType(0) sortType(0),
{} SI_units(true)
{
SortNodeElements();
UpdateScreen();
}
EventManager::~EventManager() {} EventManager::~EventManager() {}
@ -70,6 +74,8 @@ void EventManager::MainEventLoop()
CallbackHelp(); CallbackHelp();
else if(c == 's' || c == 'S') else if(c == 's' || c == 'S')
CallbackSort(); CallbackSort();
else if(c == 'u' || c == 'U')
CallbackUnits();
} }
else else
{ {
@ -103,53 +109,50 @@ void EventManager::MainEventLoop()
void EventManager::CallbackArrowUp() void EventManager::CallbackArrowUp()
{ {
std::cout << "CallbackArrowUp\n";// DEBUG
if(currentNode == NULL) return; if(currentNode == NULL) return;
if(currentNode->GetChildrenCount()) if(currentNode->GetChildrenCount())
currentLine = (currentLine - 1) % currentNode->GetChildrenCount(); currentLine = ((currentLine - 1) < 0) ? (currentNode->GetChildrenCount() - 1) : ((currentLine - 1) % currentNode->GetChildrenCount());
DebugPrintState(); ScrollList();
UpdateScreen();
} }
void EventManager::CallbackArrowDown() void EventManager::CallbackArrowDown()
{ {
std::cout << "CallbackArrowDown\n";// DEBUG
if(currentNode == NULL) return; if(currentNode == NULL) return;
if(currentNode->GetChildrenCount()) if(currentNode->GetChildrenCount())
currentLine = (currentLine + 1) % currentNode->GetChildrenCount(); currentLine = (currentLine + 1) % currentNode->GetChildrenCount();
DebugPrintState(); ScrollList();
UpdateScreen();
} }
void EventManager::CallbackArrowLeft() void EventManager::CallbackArrowLeft()
{ {
std::cout << "CallbackArrowLeft\n";// DEBUG
if(currentNode == NULL) return; if(currentNode == NULL) return;
CallbackBackspace(); CallbackBackspace();
} }
void EventManager::CallbackArrowRight() void EventManager::CallbackArrowRight()
{ {
std::cout << "CallbackArrowRight\n";// DEBUG
if(currentNode == NULL) return; if(currentNode == NULL) return;
CallbackEnter(); CallbackEnter();
} }
void EventManager::CallbackEnter() void EventManager::CallbackEnter()
{ {
std::cout << "CallbackEnter\n";// DEBUG
if(currentNode == NULL) return; if(currentNode == NULL) return;
if((size_t)currentLine < currentNode->GetChildrenCount()) if((size_t)currentLine < currentNode->GetChildrenCount())
if(currentNode->GetChild(currentLine).IsFolder()) if(currentNode->GetChild(currentLine).IsFolder())
{ {
currentNode = &currentNode->GetChild(currentLine); currentNode = &currentNode->GetChild(currentLine);
SortNodeElements();
topLine = 0; topLine = 0;
currentLine = 0; currentLine = 0;
} }
DebugPrintState(); UpdateScreen();
} }
void EventManager::CallbackBackspace() void EventManager::CallbackBackspace()
{ {
std::cout << "CallbackBackspace\n";// DEBUG
if(currentNode == NULL) return; if(currentNode == NULL) return;
// Go up one level // Go up one level
if(currentNode->GetParent() != NULL) if(currentNode->GetParent() != NULL)
@ -158,38 +161,70 @@ void EventManager::CallbackBackspace()
topLine = 0; topLine = 0;
currentLine = 0; currentLine = 0;
} }
DebugPrintState(); UpdateScreen();
} }
void EventManager::CallbackHome() void EventManager::CallbackHome()
{ {
std::cout << "CallbackHome\n";// DEBUG
if(currentNode == NULL) return; if(currentNode == NULL) return;
// Set the current node to be the root node (go back "home"). // Set the current node to be the root node (go back "home").
currentNode = &rootNode; currentNode = &rootNode;
topLine = 0; topLine = 0;
currentLine = 0; currentLine = 0;
DebugPrintState(); UpdateScreen();
} }
void EventManager::CallbackHelp() void EventManager::CallbackHelp()
{ {
std::cout << "CallbackHelp\n";// DEBUG
if(currentNode == NULL) return; if(currentNode == NULL) return;
DebugPrintState(); UpdateScreen();
} }
void EventManager::CallbackSort() void EventManager::CallbackSort()
{ {
std::cout << "CallbackSort\n";// DEBUG
if(currentNode == NULL) return; if(currentNode == NULL) return;
sortType = (sortType + 1) % 2;// Cycle through all the sort types. sortType = (sortType + 1) % 2;// Cycle through all the sort types.
SortNodeElements();
UpdateScreen();
}
void EventManager::CallbackUnits()
{
if(currentNode == NULL) return;
SI_units ^= true;// Invert the SI_units flag.
UpdateScreen();
}
void EventManager::ScrollList()
{
/*if(currentNode == NULL) return;
if(currentNode->GetChildrenCount())
{
// compute visibility of selected line and scroll if needed
PRINT_VAR(2 + currentLine - topLine);
PRINT_VAR(display.Rows()-1);
if((2 + currentLine - topLine) >= (display.Rows()-1))
topLine = currentLine - (display.Rows() - 2);// DOES NOT WORK. WORK IN PROGRESS.
}//*/
}
void EventManager::SortNodeElements()
{
if(currentNode == NULL) return;
if(sortType == 0) if(sortType == 0)
currentNode->SortBySizeDesc(); currentNode->SortBySizeDesc();
else if(sortType == 1) else if(sortType == 1)
currentNode->SortByNameAsc(); currentNode->SortByNameAsc();
DebugPrintState(); }
void EventManager::UpdateScreen()
{
//DebugPrintState(); return;// DEBUG
Display::ClearScreen();
display.DisplayTreeNode(*currentNode, topLine, SI_units);
display.HighlightLine(2 + topLine + currentLine, true);
display.DrawScreenLines();
} }
void EventManager::DebugPrintState() void EventManager::DebugPrintState()

View file

@ -8,7 +8,7 @@ using std::cout;
using std::cerr; using std::cerr;
using std::endl; using std::endl;
#define D_UNIT_TESTS //#define D_UNIT_TESTS
#include <test.hpp> #include <test.hpp>
@ -82,7 +82,12 @@ int main(int argc, char *argv[])
tree.PrintTree(maxDepth, true); tree.PrintTree(maxDepth, true);
// Interactive mode : the user can navigate through the tree structure. // Interactive mode : the user can navigate through the tree structure.
// else else
{
tree.BuildParentLinks();
EventManager eventManager(display, tree);
eventManager.MainEventLoop();
}
return 0; return 0;
} }

View file

@ -203,7 +203,7 @@ void runTests(int argc, char *argv[])
Display display; Display display;
TreeNodeDiskUsage tree(rootpath); TreeNodeDiskUsage tree(rootpath);
tree.BuildTree(false); tree.BuildTree(false);
tree.SortBySizeDesc(); tree.SortBySizeDesc(true);
display.DisplayTreeNode(tree, 0, true); display.DisplayTreeNode(tree, 0, true);
//Display::ClearScreen(); //Display::ClearScreen();
display.DrawScreenLines(); display.DrawScreenLines();