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 CallbackHelp();
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();
@ -43,6 +53,7 @@ class EventManager
int currentLine; //<! Currently selected line
TreeNodeDiskUsage *currentNode; //<! Pointer to the currently displayed node.
int sortType; //<! Entry sort type : 0 -> size descending, 1 -> name ascending
bool SI_units; //<! Indicates whether SI units should be used.
};
#endif

View file

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

View file

@ -15,8 +15,12 @@ EventManager::EventManager(Display & display_, TreeNodeDiskUsage & rootNode_)
topLine(0),
currentLine(0),
currentNode(&rootNode),
sortType(0)
{}
sortType(0),
SI_units(true)
{
SortNodeElements();
UpdateScreen();
}
EventManager::~EventManager() {}
@ -70,6 +74,8 @@ void EventManager::MainEventLoop()
CallbackHelp();
else if(c == 's' || c == 'S')
CallbackSort();
else if(c == 'u' || c == 'U')
CallbackUnits();
}
else
{
@ -103,53 +109,50 @@ void EventManager::MainEventLoop()
void EventManager::CallbackArrowUp()
{
std::cout << "CallbackArrowUp\n";// DEBUG
if(currentNode == NULL) return;
if(currentNode->GetChildrenCount())
currentLine = (currentLine - 1) % currentNode->GetChildrenCount();
DebugPrintState();
currentLine = ((currentLine - 1) < 0) ? (currentNode->GetChildrenCount() - 1) : ((currentLine - 1) % currentNode->GetChildrenCount());
ScrollList();
UpdateScreen();
}
void EventManager::CallbackArrowDown()
{
std::cout << "CallbackArrowDown\n";// DEBUG
if(currentNode == NULL) return;
if(currentNode->GetChildrenCount())
currentLine = (currentLine + 1) % currentNode->GetChildrenCount();
DebugPrintState();
ScrollList();
UpdateScreen();
}
void EventManager::CallbackArrowLeft()
{
std::cout << "CallbackArrowLeft\n";// DEBUG
if(currentNode == NULL) return;
CallbackBackspace();
}
void EventManager::CallbackArrowRight()
{
std::cout << "CallbackArrowRight\n";// DEBUG
if(currentNode == NULL) return;
CallbackEnter();
}
void EventManager::CallbackEnter()
{
std::cout << "CallbackEnter\n";// DEBUG
if(currentNode == NULL) return;
if((size_t)currentLine < currentNode->GetChildrenCount())
if(currentNode->GetChild(currentLine).IsFolder())
{
currentNode = &currentNode->GetChild(currentLine);
SortNodeElements();
topLine = 0;
currentLine = 0;
}
DebugPrintState();
UpdateScreen();
}
void EventManager::CallbackBackspace()
{
std::cout << "CallbackBackspace\n";// DEBUG
if(currentNode == NULL) return;
// Go up one level
if(currentNode->GetParent() != NULL)
@ -158,38 +161,70 @@ void EventManager::CallbackBackspace()
topLine = 0;
currentLine = 0;
}
DebugPrintState();
UpdateScreen();
}
void EventManager::CallbackHome()
{
std::cout << "CallbackHome\n";// DEBUG
if(currentNode == NULL) return;
// Set the current node to be the root node (go back "home").
currentNode = &rootNode;
topLine = 0;
currentLine = 0;
DebugPrintState();
UpdateScreen();
}
void EventManager::CallbackHelp()
{
std::cout << "CallbackHelp\n";// DEBUG
if(currentNode == NULL) return;
DebugPrintState();
UpdateScreen();
}
void EventManager::CallbackSort()
{
std::cout << "CallbackSort\n";// DEBUG
if(currentNode == NULL) return;
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)
currentNode->SortBySizeDesc();
else if(sortType == 1)
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()

View file

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

View file

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