-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd15163
commit 7166161
Showing
8 changed files
with
258 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#pragma once | ||
|
||
#include <base.hpp> | ||
#include <converter.hpp> | ||
#include <error.hpp> | ||
#include <standard.hpp> | ||
|
||
class CdCommand : public BaseCommand | ||
{ | ||
public: | ||
using BaseCommand::run; | ||
|
||
CdCommand() : BaseCommand("cd") {} | ||
|
||
int run(const ParseResult &arguments) override | ||
{ | ||
if (arguments.positional_arguments.size() == 1) | ||
{ | ||
throw std::invalid_argument("No target directory specified"); | ||
} | ||
else if (arguments.positional_arguments.size() > 2) | ||
{ | ||
throw std::invalid_argument("Expected 1 argument only"); | ||
} | ||
else | ||
{ | ||
auto target = arguments.positional_arguments[1]; | ||
if (!SetCurrentDirectoryW(utf_convert(target).c_str())) | ||
{ | ||
throw_last_error("Error when changing directory"); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#pragma once | ||
|
||
#include <base.hpp> | ||
#include <converter.hpp> | ||
#include <error.hpp> | ||
#include <standard.hpp> | ||
#include <tables.hpp> | ||
#include <units.hpp> | ||
#include <utils.hpp> | ||
|
||
class LsCommand : public BaseCommand | ||
{ | ||
public: | ||
using BaseCommand::run; | ||
|
||
LsCommand() : BaseCommand("ls") {} | ||
|
||
int run(const ParseResult &arguments) override | ||
{ | ||
auto directory = get_working_directory(); | ||
if (arguments.positional_arguments.size() == 2) | ||
{ | ||
directory = arguments.positional_arguments[1]; | ||
} | ||
else if (arguments.positional_arguments.size() > 2) | ||
{ | ||
throw std::invalid_argument("Expected at most 1 argument only"); | ||
} | ||
|
||
Table displayer({"Name", "Type", "Size"}); | ||
|
||
directory += "\\*"; | ||
WIN32_FIND_DATAW data; | ||
HANDLE h_file = FindFirstFileW(utf_convert(directory).c_str(), &data); | ||
if (h_file == INVALID_HANDLE_VALUE) | ||
{ | ||
throw_last_error("Error when listing directory"); | ||
} | ||
|
||
do | ||
{ | ||
long double size = ((long double)data.nFileSizeHigh * ((long double)MAXDWORD + 1.0L)) + (long double)data.nFileSizeLow; | ||
bool is_directory = data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; | ||
displayer.add_row( | ||
{utf_convert(std::wstring(data.cFileName)), | ||
is_directory ? "DIR" : "FILE", | ||
is_directory ? "-" : memory_size(size)}); | ||
} while (FindNextFileW(h_file, &data)); | ||
|
||
std::cout << displayer.display() << std::endl; | ||
|
||
if (!FindClose(h_file)) | ||
{ | ||
throw_last_error("Error when closing file search handle"); | ||
} | ||
|
||
return 0; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
|
||
#include "format.hpp" | ||
#include "standard.hpp" | ||
|
||
void throw_last_error(const std::string &message) | ||
{ | ||
throw std::runtime_error(format("%s: %d", message.c_str(), GetLastError())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#pragma once | ||
|
||
#include "format.hpp" | ||
#include "standard.hpp" | ||
|
||
class Table | ||
{ | ||
private: | ||
std::vector<std::vector<std::string>> rows; | ||
|
||
public: | ||
const unsigned columns; | ||
bool align_left = true; | ||
|
||
Table(const std::initializer_list<std::string> &header) : columns(header.size()) | ||
{ | ||
rows.push_back(header); | ||
} | ||
|
||
void add_row(const std::initializer_list<std::string> &row) | ||
{ | ||
if (row.size() != columns) | ||
{ | ||
throw std::invalid_argument(format("Attempted to add a row of %d item(s) to a table with only %d column(s)", row.size(), columns)); | ||
} | ||
|
||
rows.push_back(row); | ||
} | ||
|
||
std::string display() | ||
{ | ||
std::vector<unsigned> column_widths(columns); | ||
for (auto &row : rows) | ||
{ | ||
for (unsigned column = 0; column < columns; column++) | ||
{ | ||
column_widths[column] = std::max(column_widths[column], 2u + (unsigned)row[column].size()); | ||
} | ||
} | ||
|
||
std::vector<std::string> lines; | ||
for (unsigned row = 0; row < rows.size(); row++) | ||
{ | ||
std::string line; | ||
for (unsigned column = 0; column < columns; column++) | ||
{ | ||
if (align_left) | ||
{ | ||
line += ' '; | ||
line += rows[row][column]; | ||
} | ||
|
||
for (unsigned i = 0; i < column_widths[column] - rows[row][column].size() - 1; i++) | ||
{ | ||
line += ' '; | ||
} | ||
|
||
if (!align_left) | ||
{ | ||
line += rows[row][column]; | ||
line += ' '; | ||
} | ||
|
||
line += '|'; | ||
} | ||
|
||
lines.push_back(line); | ||
|
||
if (row == 0) | ||
{ | ||
std::string line; | ||
for (unsigned column = 0; column < columns; column++) | ||
{ | ||
for (unsigned i = 0; i < column_widths[column]; i++) | ||
{ | ||
line += '-'; | ||
} | ||
line += '+'; | ||
} | ||
lines.push_back(line); | ||
} | ||
} | ||
|
||
std::string result; | ||
for (auto &line : lines) | ||
{ | ||
result += line; | ||
result += '\n'; | ||
} | ||
|
||
return result; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
#include "format.hpp" | ||
#include "standard.hpp" | ||
|
||
std::string memory_size(long double bytes) | ||
{ | ||
std::string result = format("%.2LfB", bytes); | ||
if (bytes > 1024) | ||
{ | ||
bytes /= 1024; | ||
result = format("%.2LfKB", bytes); | ||
|
||
if (bytes > 1024) | ||
{ | ||
bytes /= 1024; | ||
result = format("%.2LfMB", bytes); | ||
|
||
if (bytes > 1024) | ||
{ | ||
bytes /= 1024; | ||
result = format("%.2LfGB", bytes); | ||
|
||
if (bytes > 1024) | ||
{ | ||
bytes /= 1024; | ||
result = format("%.2LfTB", bytes); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include "converter.hpp" | ||
#include "standard.hpp" | ||
|
||
std::string get_working_directory() | ||
{ | ||
wchar_t buffer[MAX_PATH]; | ||
auto size = GetCurrentDirectoryW(MAX_PATH, buffer); | ||
|
||
if (size == 0) | ||
{ | ||
throw std::runtime_error("Error getting current directory."); | ||
} | ||
|
||
return utf_convert(std::wstring(buffer, buffer + size)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters