mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-05-28 15:34:33 +00:00
Refactor ls command
This commit is contained in:
parent
6169edf1e8
commit
a47a97da87
@ -23,6 +23,49 @@
|
|||||||
|
|
||||||
using namespace vfs;
|
using namespace vfs;
|
||||||
|
|
||||||
|
const char *ColorNodeType(Node *node)
|
||||||
|
{
|
||||||
|
switch (node->Type)
|
||||||
|
{
|
||||||
|
case NodeType::DIRECTORY:
|
||||||
|
return "\e44FF22";
|
||||||
|
case NodeType::BLOCKDEVICE:
|
||||||
|
case NodeType::CHARDEVICE:
|
||||||
|
return "\eFF22AA";
|
||||||
|
case NodeType::PIPE:
|
||||||
|
return "\eFFAA22";
|
||||||
|
case NodeType::SYMLINK:
|
||||||
|
return "\eFF22FF";
|
||||||
|
case NodeType::FILE:
|
||||||
|
default:
|
||||||
|
return "\eCCCCCC";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t MaxNameLength(Node *nodes)
|
||||||
|
{
|
||||||
|
size_t maxLength = 0;
|
||||||
|
foreach (auto &node in nodes->Children)
|
||||||
|
maxLength = std::max(maxLength, strlen(node->Name));
|
||||||
|
return maxLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintLS(Node *node)
|
||||||
|
{
|
||||||
|
size_t maxNameLength = MaxNameLength(node);
|
||||||
|
int count = 0;
|
||||||
|
bool first = true;
|
||||||
|
foreach (auto &var in node->Children)
|
||||||
|
{
|
||||||
|
if (count % 5 == 0 && !first)
|
||||||
|
printf("\n");
|
||||||
|
printf(" %s%-*s ", ColorNodeType(var), (int)maxNameLength, var->Name);
|
||||||
|
count++;
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
printf("\eCCCCCC\n");
|
||||||
|
}
|
||||||
|
|
||||||
void cmd_ls(const char *args)
|
void cmd_ls(const char *args)
|
||||||
{
|
{
|
||||||
if (args[0] == '\0')
|
if (args[0] == '\0')
|
||||||
@ -32,8 +75,7 @@ void cmd_ls(const char *args)
|
|||||||
if (rootNode == nullptr)
|
if (rootNode == nullptr)
|
||||||
rootNode = fs->GetRootNode()->Children[0];
|
rootNode = fs->GetRootNode()->Children[0];
|
||||||
|
|
||||||
foreach (auto var in rootNode->Children)
|
PrintLS(rootNode);
|
||||||
printf("%s\n", var->Name);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -47,11 +89,10 @@ void cmd_ls(const char *args)
|
|||||||
|
|
||||||
if (thisNode->Type != NodeType::DIRECTORY)
|
if (thisNode->Type != NodeType::DIRECTORY)
|
||||||
{
|
{
|
||||||
printf("%s\n", thisNode->Name);
|
printf("%s%s\n", ColorNodeType(thisNode), thisNode->Name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (auto var in thisNode->Children)
|
PrintLS(thisNode);
|
||||||
printf("%s\n", var->Name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user