Working event system on tee structure, with debug prints.
This commit is contained in:
parent
63a7ca456e
commit
9e6579c57b
6 changed files with 138 additions and 39 deletions
|
|
@ -33,14 +33,16 @@ class EventManager
|
|||
void CallbackHelp();
|
||||
void CallbackSort();
|
||||
|
||||
void DebugPrintState();
|
||||
|
||||
protected:
|
||||
Display & display;
|
||||
TreeNodeDiskUsage & rootNode;
|
||||
|
||||
int topLine; //<! Index of top displayed line (for scrolling)
|
||||
int currentLine; //<! Currently selected line
|
||||
TreeNodeDiskUsage *currentNode;
|
||||
TreeNodeDiskUsage *parentNode;
|
||||
TreeNodeDiskUsage *currentNode; //<! Pointer to the currently displayed node.
|
||||
int sortType; //<! Entry sort type : 0 -> size descending, 1 -> name ascending
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -22,8 +22,6 @@ struct PathFilters
|
|||
/// \class TreeNodeDiskUsage Implements a node of a tree of the files and folders on a system.
|
||||
class TreeNodeDiskUsage
|
||||
{
|
||||
friend class Display;
|
||||
|
||||
public:
|
||||
TreeNodeDiskUsage(std::string const& path_, PathFilters const* pathFilters_ = NULL);
|
||||
~TreeNodeDiskUsage();
|
||||
|
|
@ -46,12 +44,33 @@ class TreeNodeDiskUsage
|
|||
/// Returns the total size of the node on disk, including all its children
|
||||
size_t GetTotalSizeOnDisk() const;
|
||||
|
||||
/// Returns the total number of elements contained within the node, including all its children
|
||||
size_t GetTotalElements() const;
|
||||
|
||||
/// Returns the name of the node : /path/to/node.bla -> node.bla
|
||||
std::string GetNodeName() const;
|
||||
|
||||
/// Returns the path of the node : /path/to/node.bla
|
||||
std::string GetNodePath() const;
|
||||
|
||||
/// Returns a pointer to the node's parent. Warning : this pointer can be NULL !
|
||||
TreeNodeDiskUsage * GetParent() const;
|
||||
|
||||
/// Returns true if the node corresponds to a folder.
|
||||
bool IsFolder() const;
|
||||
|
||||
/// Returns a constant reference to the list of children of the node.
|
||||
std::vector<TreeNodeDiskUsage> const& GetChildren() const;
|
||||
|
||||
/// Returns a constant reference to the the child i.
|
||||
TreeNodeDiskUsage const& GetChild(unsigned int i) const;
|
||||
|
||||
/// Returns a reference to the the child i.
|
||||
TreeNodeDiskUsage & GetChild(unsigned int i);
|
||||
|
||||
/// Builds the parent links. Must be called after the tree has been built.
|
||||
void BuildParentLinks();
|
||||
|
||||
/// Sorts the children of the node by size in descending order.
|
||||
void SortBySizeDesc(bool recursive = false);
|
||||
|
||||
|
|
@ -67,9 +86,6 @@ class TreeNodeDiskUsage
|
|||
/// Cleans the path to remove double slashes, trailing slashes and leading and trailing whitespaces.
|
||||
void SanitizePath();
|
||||
|
||||
/// Explores the whole tree to compute every node's size, including all their children.
|
||||
void ComputeTotalSize();
|
||||
|
||||
/// Removes the leading character 'c'.
|
||||
static std::string RemoveLeadingCharacter(std::string const& str, char c);
|
||||
|
||||
|
|
@ -87,6 +103,7 @@ class TreeNodeDiskUsage
|
|||
|
||||
std::string path; //<! Path of the node
|
||||
std::vector<TreeNodeDiskUsage> children; //<! Stores all the children contained within the folder.
|
||||
TreeNodeDiskUsage * parentNode; //<! Pointer to the parent of the node.
|
||||
bool isFolder; //<! Indicates whether the node is a folder or a file.
|
||||
size_t totalSize; //<! Total size of the node, taking all children into account.
|
||||
size_t totalSizeOnDisk; //<! Total size of the node on the disk, taking all children into account.
|
||||
|
|
|
|||
|
|
@ -77,8 +77,8 @@ void Display::DisplayTreeNode(TreeNodeDiskUsage const& treeNode, size_t topLine,
|
|||
|
||||
// Write currently displayed directory using its absolute path. If the path resolution does not succeed, the relative path is displayed
|
||||
char absoluteNodePathCstr[4096+1];
|
||||
char *ptr = realpath(treeNode.path.c_str(), absoluteNodePathCstr);
|
||||
std::string absoluteNodePath = (ptr != NULL) ? absoluteNodePathCstr : treeNode.path;
|
||||
char *ptr = realpath(treeNode.GetNodePath().c_str(), absoluteNodePathCstr);
|
||||
std::string absoluteNodePath = (ptr != NULL) ? absoluteNodePathCstr : treeNode.GetNodePath();
|
||||
lines[1] = "--- " + LeftJustify(absoluteNodePath + " ", screenCols-5, '-');
|
||||
|
||||
size_t availableLines = screenRows - 3;// Number of available lines for the listing
|
||||
|
|
@ -92,18 +92,18 @@ void Display::DisplayTreeNode(TreeNodeDiskUsage const& treeNode, size_t topLine,
|
|||
|
||||
for(size_t i = 0 ; i < MIN(availableLines, treeNode.GetChildrenCount()-topLine) ; i++)
|
||||
{
|
||||
TreeNodeDiskUsage const& node = treeNode.children[i+topLine];
|
||||
TreeNodeDiskUsage const& node = treeNode.GetChildren()[i+topLine];
|
||||
std::stringstream lineStream;
|
||||
lineStream << RightJustify(Bytes2HumanReadable(node.totalSize, SI_units), fieldWidth1);
|
||||
lineStream << GenerateProgressBar(fieldWidth2, node.totalSize, treeNode.totalSize, true, "#");
|
||||
lineStream << " " << LeftJustify(node.GetNodeName() + ((node.isFolder) ? "/" : " "), fieldWidth3-1);
|
||||
lineStream << RightJustify(Bytes2HumanReadable(node.GetTotalSize(), SI_units), fieldWidth1);
|
||||
lineStream << GenerateProgressBar(fieldWidth2, node.GetTotalSize(), treeNode.GetTotalSize(), true, "#");
|
||||
lineStream << " " << LeftJustify(node.GetNodeName() + ((node.IsFolder()) ? "/" : " "), fieldWidth3-1);
|
||||
lines[i+2] = LeftJustify(lineStream.str(), screenCols);
|
||||
}
|
||||
|
||||
// Write footer
|
||||
std::stringstream footerSstream; footerSstream << "Total disk usage: " << Bytes2HumanReadable(treeNode.totalSizeOnDisk, SI_units)
|
||||
<< " Apparent size: " << Bytes2HumanReadable(treeNode.totalSize, SI_units)
|
||||
<< " Items: " << treeNode.totalElements;
|
||||
std::stringstream footerSstream; footerSstream << "Total disk usage: " << Bytes2HumanReadable(treeNode.GetTotalSizeOnDisk(), SI_units)
|
||||
<< " Apparent size: " << Bytes2HumanReadable(treeNode.GetTotalSize(), SI_units)
|
||||
<< " Items: " << treeNode.GetTotalElements();
|
||||
lines[screenRows-1] = std::string(C_INVERT_FG_BG) + LeftJustify(footerSstream.str(), screenCols) + C_RESET;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,10 +15,8 @@ EventManager::EventManager(Display & display_, TreeNodeDiskUsage & rootNode_)
|
|||
topLine(0),
|
||||
currentLine(0),
|
||||
currentNode(&rootNode),
|
||||
parentNode(&rootNode)
|
||||
{
|
||||
|
||||
}
|
||||
sortType(0)
|
||||
{}
|
||||
|
||||
EventManager::~EventManager() {}
|
||||
|
||||
|
|
@ -105,45 +103,108 @@ void EventManager::MainEventLoop()
|
|||
|
||||
void EventManager::CallbackArrowUp()
|
||||
{
|
||||
std::cout << "CallbackArrowUp\n";
|
||||
std::cout << "CallbackArrowUp\n";// DEBUG
|
||||
if(currentNode == NULL) return;
|
||||
if(currentNode->GetChildrenCount())
|
||||
currentLine = (currentLine - 1) % currentNode->GetChildrenCount();
|
||||
DebugPrintState();
|
||||
}
|
||||
|
||||
void EventManager::CallbackArrowDown()
|
||||
{
|
||||
std::cout << "CallbackArrowDown\n";
|
||||
std::cout << "CallbackArrowDown\n";// DEBUG
|
||||
if(currentNode == NULL) return;
|
||||
if(currentNode->GetChildrenCount())
|
||||
currentLine = (currentLine + 1) % currentNode->GetChildrenCount();
|
||||
DebugPrintState();
|
||||
}
|
||||
|
||||
void EventManager::CallbackArrowLeft()
|
||||
{
|
||||
std::cout << "CallbackArrowLeft\n";
|
||||
std::cout << "CallbackArrowLeft\n";// DEBUG
|
||||
if(currentNode == NULL) return;
|
||||
CallbackBackspace();
|
||||
}
|
||||
|
||||
void EventManager::CallbackArrowRight()
|
||||
{
|
||||
std::cout << "CallbackArrowRight\n";
|
||||
std::cout << "CallbackArrowRight\n";// DEBUG
|
||||
if(currentNode == NULL) return;
|
||||
CallbackEnter();
|
||||
}
|
||||
|
||||
void EventManager::CallbackEnter()
|
||||
{
|
||||
std::cout << "CallbackEnter\n";
|
||||
std::cout << "CallbackEnter\n";// DEBUG
|
||||
if(currentNode == NULL) return;
|
||||
if((size_t)currentLine < currentNode->GetChildrenCount())
|
||||
if(currentNode->GetChild(currentLine).IsFolder())
|
||||
{
|
||||
currentNode = ¤tNode->GetChild(currentLine);
|
||||
topLine = 0;
|
||||
currentLine = 0;
|
||||
}
|
||||
DebugPrintState();
|
||||
}
|
||||
|
||||
void EventManager::CallbackBackspace()
|
||||
{
|
||||
std::cout << "CallbackBackspace\n";
|
||||
std::cout << "CallbackBackspace\n";// DEBUG
|
||||
if(currentNode == NULL) return;
|
||||
// Go up one level
|
||||
if(currentNode->GetParent() != NULL)
|
||||
{
|
||||
currentNode = currentNode->GetParent();
|
||||
topLine = 0;
|
||||
currentLine = 0;
|
||||
}
|
||||
DebugPrintState();
|
||||
}
|
||||
|
||||
void EventManager::CallbackHome()
|
||||
{
|
||||
std::cout << "CallbackHome\n";
|
||||
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();
|
||||
}
|
||||
|
||||
void EventManager::CallbackHelp()
|
||||
{
|
||||
std::cout << "CallbackHelp\n";
|
||||
std::cout << "CallbackHelp\n";// DEBUG
|
||||
if(currentNode == NULL) return;
|
||||
|
||||
DebugPrintState();
|
||||
}
|
||||
|
||||
void EventManager::CallbackSort()
|
||||
{
|
||||
std::cout << "CallbackSort\n";
|
||||
std::cout << "CallbackSort\n";// DEBUG
|
||||
if(currentNode == NULL) return;
|
||||
sortType = (sortType + 1) % 2;// Cycle through all the sort types.
|
||||
if(sortType == 0)
|
||||
currentNode->SortBySizeDesc();
|
||||
else if(sortType == 1)
|
||||
currentNode->SortByNameAsc();
|
||||
DebugPrintState();
|
||||
}
|
||||
|
||||
void EventManager::DebugPrintState()
|
||||
{
|
||||
PRINT_VAR(&rootNode);
|
||||
PRINT_VAR(topLine);
|
||||
PRINT_VAR(currentLine);
|
||||
PRINT_VAR(currentNode);
|
||||
PRINT_VAR(currentNode->GetParent());
|
||||
PRINT_VAR(sortType);
|
||||
|
||||
if((size_t)currentLine < currentNode->GetChildrenCount())
|
||||
{
|
||||
PRINT_VAR(currentNode->GetChild(currentLine).IsFolder());
|
||||
PRINT_VAR(currentNode->GetChild(currentLine).GetNodePath());
|
||||
}
|
||||
std::cout << "-----------------------------------------------\n";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
TreeNodeDiskUsage::TreeNodeDiskUsage(std::string const& path_, PathFilters const* pathFilters_)
|
||||
: pathFilters(pathFilters_),
|
||||
path(path_),
|
||||
parentNode(NULL),
|
||||
isFolder(false),
|
||||
totalSize(0),
|
||||
totalSizeOnDisk(0),
|
||||
|
|
@ -143,6 +144,7 @@ void TreeNodeDiskUsage::PrintTree(unsigned int maxDepth, bool humanReadableSizes
|
|||
size_t TreeNodeDiskUsage::GetChildrenCount() const { return children.size(); }
|
||||
size_t TreeNodeDiskUsage::GetTotalSize() const { return totalSize; }
|
||||
size_t TreeNodeDiskUsage::GetTotalSizeOnDisk() const { return totalSizeOnDisk; }
|
||||
size_t TreeNodeDiskUsage::GetTotalElements() const { return totalElements; }
|
||||
std::string TreeNodeDiskUsage::GetNodePath() const{ return path; }
|
||||
|
||||
std::string TreeNodeDiskUsage::GetNodeName() const
|
||||
|
|
@ -165,6 +167,18 @@ std::string TreeNodeDiskUsage::GetNodeName() const
|
|||
return path.substr(lastSepPos+1, path.size()-lastSepPos-1);
|
||||
}
|
||||
|
||||
TreeNodeDiskUsage * TreeNodeDiskUsage::GetParent() const { return parentNode; }
|
||||
|
||||
bool TreeNodeDiskUsage::IsFolder() const { return isFolder; }
|
||||
|
||||
std::vector<TreeNodeDiskUsage> const& TreeNodeDiskUsage::GetChildren() const { return children; }
|
||||
|
||||
/// Returns a constant reference to the the child i.
|
||||
TreeNodeDiskUsage const& TreeNodeDiskUsage::GetChild(unsigned int i) const { return children[i]; }
|
||||
|
||||
/// Returns a reference to the the child i.
|
||||
TreeNodeDiskUsage & TreeNodeDiskUsage::GetChild(unsigned int i) { return children[i]; }
|
||||
|
||||
void TreeNodeDiskUsage::SortBySizeDesc(bool recursive)
|
||||
{
|
||||
std::sort(children.begin(), children.end(), TreeNodeDiskUsage::SortOperatorSizeDesc);
|
||||
|
|
@ -173,6 +187,16 @@ void TreeNodeDiskUsage::SortBySizeDesc(bool recursive)
|
|||
children[i].SortBySizeDesc(recursive);
|
||||
}
|
||||
|
||||
void TreeNodeDiskUsage::BuildParentLinks()
|
||||
{
|
||||
for(unsigned int i = 0 ; i < children.size() ; i++)
|
||||
{
|
||||
children[i].parentNode = this;
|
||||
if(children[i].isFolder)
|
||||
children[i].BuildParentLinks();
|
||||
}
|
||||
}
|
||||
|
||||
void TreeNodeDiskUsage::SortByNameAsc(bool recursive)
|
||||
{
|
||||
std::sort(children.begin(), children.end(), TreeNodeDiskUsage::SortOperatorNameAsc);
|
||||
|
|
@ -256,9 +280,3 @@ std::string TreeNodeDiskUsage::ReplaceCharacterInStr(std::string str, char c1, c
|
|||
str[i] = c2;
|
||||
return str;
|
||||
}
|
||||
|
||||
void TreeNodeDiskUsage::ComputeTotalSize()
|
||||
{
|
||||
// Recursively add all sizes to the current node's size
|
||||
//if()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -189,15 +189,16 @@ void runTests(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
}
|
||||
if(0)
|
||||
//if(0)
|
||||
{// test EventManager
|
||||
Display display;
|
||||
TreeNodeDiskUsage tree(rootpath);
|
||||
tree.BuildTree(false);
|
||||
tree.BuildParentLinks();
|
||||
EventManager eventManager(display, tree);
|
||||
eventManager.MainEventLoop();
|
||||
}
|
||||
//if(0)
|
||||
if(0)
|
||||
{// test interactive display of tree node (without interactivity)
|
||||
Display display;
|
||||
TreeNodeDiskUsage tree(rootpath);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue