Skip to content

Commit 4941cae

Browse files
committed
Rename locate_dir to find_upwards and improve error handling in this function
1 parent 71c786e commit 4941cae

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

src/utils.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,17 @@ std::string style(std::string text, Color fore, Color back = Color::BLACK)
4343
return ansi_text.append(kt::format_str("{}\033[0m", text));
4444
}
4545

46-
std::filesystem::path locate_dir(std::filesystem::path& path, int max_depth = 10)
46+
std::filesystem::path find_upwards(std::string dir_name, int max_depth = 10)
4747
{
48-
if (max_depth > 0)
49-
{
50-
while (!std::filesystem::exists(path))
51-
{
52-
max_depth -= 1;
53-
path = path.parent_path().parent_path() / path.stem();
54-
}
48+
auto path = std::filesystem::current_path() / std::filesystem::path(dir_name);
5549

56-
return path;
57-
}
58-
else
50+
while (!std::filesystem::exists(path) && max_depth > 0)
5951
{
60-
std::cerr << "Directory not found (max_depth exceeded?): " << path.string() << '\n';
61-
}
52+
max_depth -= 1;
53+
path = path.parent_path().parent_path() / path.stem();
54+
}
55+
56+
return (max_depth > 0) ? path : std::filesystem::path();
6257
}
6358

6459
bool validate_asset_dir(const std::filesystem::path& asset_dir)

src/utils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ enum class Color
6666

6767
std::string style(std::string text, Color fore, Color back = Color::BLACK);
6868

69-
std::filesystem::path locate_dir(std::filesystem::path& path, int max_depth = 10);
69+
std::filesystem::path find_upwards(std::string dir_name, int max_depth = 10);
7070

7171
bool validate_asset_dir(const std::filesystem::path& asset_dir);
7272

0 commit comments

Comments
 (0)