Skip to content

Commit

Permalink
Add ranges to clear action
Browse files Browse the repository at this point in the history
  • Loading branch information
Slackadays committed Oct 23, 2024
1 parent 4b0b2e8 commit 6cbf9e0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
56 changes: 48 additions & 8 deletions src/cb/src/actions/clear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,66 @@ void clear() {
std::string decision;
std::getline(std::cin, decision);
fprintf(stderr, "%s", formatColors("[blank]").data());
int clipboards_cleared = 0;
unsigned long clipboards_cleared = 0;
if (decision.substr(0, 1) != "y" && decision.substr(0, 1) != "Y") return;
startIndicator();
for (const auto& entry : fs::directory_iterator(global_path.temporary)) {
bool predicate = Clipboard(entry.path().filename().string()).holdsDataInCurrentEntry();
fs::remove_all(entry);
if (predicate) clipboards_cleared++;
clipboards_cleared++;
}
for (const auto& entry : fs::directory_iterator(global_path.persistent)) {
bool predicate = Clipboard(entry.path().filename().string()).holdsDataInCurrentEntry();
fs::remove_all(entry);
if (predicate) clipboards_cleared++;
clipboards_cleared++;
}
stopIndicator();
fprintf(stderr, formatColors("[success][inverse] โœ” [noinverse] Cleared %d clipboard%s[blank]\n").data(), clipboards_cleared, clipboards_cleared == 1 ? "" : "s");
}
} else {
fs::remove(path.metadata.originals);
fs::remove(path.metadata.notes);
fs::remove(path.metadata.ignore);
if (copying.items.size() >= 1) {
std::vector<unsigned long> entries_to_clear = {};
for (const auto& item : copying.items) {
// First, check if it fits the format abc-xyz
// If it does, treat that as a range
if (item.string().find('-') != std::string::npos) {
std::vector<std::string> range = regexSplit(item.string(), std::regex("-"));
if (range.size() == 2) {
try {
unsigned long start = std::stoul(range.at(0));
unsigned long end = std::stoul(range.at(1));
if (start > end) std::swap(start, end);
for (unsigned long i = start; i <= end; i++)
entries_to_clear.push_back(i);
} catch (const std::invalid_argument& e) {}
}
continue;
}

try {
unsigned long num = std::stoul(item.string());
entries_to_clear.push_back(num);
} catch (const std::invalid_argument& e) {}
}

if (entries_to_clear.empty()) {
stopIndicator();
fprintf(stderr,
"%s",
formatColors("[error][inverse] โœ˜ [noinverse] CB couldn't find any valid entries to clear. [help]โฌค Make sure you enter only numbers, or a range like 5-9 or 0-30.[blank]\n")
.data());
return;
}

// Now clear these entries
for (const auto& entry : entries_to_clear) {
for (const auto& item : fs::directory_iterator(path.entryPathFor(entry)))
fs::remove_all(item);
}

} else {
fs::remove(path.metadata.originals);
fs::remove(path.metadata.notes);
fs::remove(path.metadata.ignore);
}
stopIndicator();
if (!output_silent && !confirmation_silent) fprintf(stderr, "%s", formatColors("[success][inverse] โœ” [noinverse] Cleared clipboard[blank]\n").data());
}
Expand Down
4 changes: 2 additions & 2 deletions src/cb/src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ bool isAClearingAction() {

bool needsANewEntry() {
using enum Action;
return (action == Copy || action == Cut || (action == Clear && !all_option)) && clipboard_entry == constants.default_clipboard_entry;
return (action == Copy || action == Cut || (action == Clear && !all_option && copying.items.size() == 0)) && clipboard_entry == constants.default_clipboard_entry;
}

[[nodiscard]] CopyPolicy userDecision(const std::string& item) {
Expand Down Expand Up @@ -573,7 +573,7 @@ void checkForNoItems() {
if (action_is_one_of(Cut, Copy, Add, Remove) && io_type != IOType::Pipe && copying.items.size() < 1) {
error_exit(choose_action_items_message(), actions[action], actions[action], clipboard_invocation, actions[action]);
}
if (((action_is_one_of(Paste, Show) || (action == Clear && !all_option))) && (!fs::exists(path.data) || fs::is_empty(path.data))) {
if (((action_is_one_of(Paste, Show) || (action == Clear && !all_option && copying.items.size() == 0))) && (!fs::exists(path.data) || fs::is_empty(path.data))) {
PerformAction::status();
exit(EXIT_SUCCESS);
}
Expand Down

0 comments on commit 6cbf9e0

Please sign in to comment.