Skip to content

Commit

Permalink
Finish Script action, add justfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Slackadays committed Nov 10, 2024
1 parent e4cf83c commit a037cc4
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 15 deletions.
14 changes: 14 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
default:
@just --list

build:
if [ ! -d "build" ]; then mkdir build; fi

cd build; cmake .. -DCMAKE_BUILD_TYPE=Release

cd build; cmake --build . -j 12

cd build; sudo cmake --install .

clean:
if [ -d "build" ]; then rm -rf build; fi
25 changes: 23 additions & 2 deletions src/cb/src/actions/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,30 @@ void info() {
if (fs::exists(path.metadata.script)) {
auto script = fileContents(path.metadata.script).value();
if (script.size() > 50) {
fprintf(stderr, formatColors("[info]%sโ”ƒ Script: [help]%s...[blank]\n").data(), endbar, makeControlCharactersVisible(removeExcessWhitespace(script.substr(0, 50))).data());
fprintf(stderr, formatColors("[info]%sโ”ƒ Script preview: [help]%s...[blank]\n").data(), endbar, makeControlCharactersVisible(removeExcessWhitespace(script.substr(0, 50))).data());
} else {
fprintf(stderr, formatColors("[info]%sโ”ƒ Script: [help]%s[blank]\n").data(), endbar, makeControlCharactersVisible(removeExcessWhitespace(script)).data());
fprintf(stderr, formatColors("[info]%sโ”ƒ Script preview: [help]%s[blank]\n").data(), endbar, makeControlCharactersVisible(removeExcessWhitespace(script)).data());
}
auto lines = fileLines(path.metadata.script_config, true);
if (lines[0] != "") {
fprintf(stderr, formatColors("[info]%sโ”ƒ Script actions: [help]").data(), endbar);
for (const auto& action : regexSplit(lines[0], std::regex(" "))) {
fprintf(stderr, "%s", action.data());
if (action != regexSplit(lines[0], std::regex(" ")).back()) fprintf(stderr, ", ");
}
fprintf(stderr, "%s", formatColors("[blank]\n").data());
} else {
fprintf(stderr, formatColors("[info]%sโ”ƒ This script is set to run for all actions.[blank]\n").data(), endbar);
}
if (lines.size() > 1 && lines[1] != "") {
fprintf(stderr, formatColors("[info]%sโ”ƒ Script timings: [help]").data(), endbar);
for (const auto& timing : regexSplit(lines[1], std::regex(" "))) {
fprintf(stderr, "%s", timing.data());
if (timing != regexSplit(lines[1], std::regex(" ")).back()) fprintf(stderr, ", ");
}
fprintf(stderr, "%s", formatColors("[blank]\n").data());
} else {
fprintf(stderr, formatColors("[info]%sโ”ƒ This script is set to run before and after all actions.[blank]\n").data(), endbar);
}
} else {
fprintf(stderr, formatColors("[info]%sโ”ƒ There is no script for this clipboard.[blank]\n").data(), endbar);
Expand Down
56 changes: 51 additions & 5 deletions src/cb/src/actions/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,23 @@
#include <sys/wait.h>
#endif

bool runForThisAction = true;
bool runBefore = true;
bool runAfter = true;

namespace PerformAction {
void script() {
std::string dataToWrite;
for (const auto& action : script_actions) {
dataToWrite += action + " ";
}
if (dataToWrite.back() == ' ') dataToWrite.back() = '\n'; else dataToWrite += '\n';
for (const auto& timing : script_timings) {
dataToWrite += timing + " ";
}
if (dataToWrite.back() == ' ') dataToWrite.back() = '\n'; else dataToWrite += '\n';
writeToFile(path.metadata.script_config, dataToWrite);
checkClipboardScriptEligibility();
if (io_type == IOType::File) {
if (copying.items.size() > 1) {
error_exit("%s", formatColors("[error][inverse] โœ˜ [noinverse] You can only set one script file to run. [help]โฌค Try providing a single script file instead.[blank]\n"));
Expand All @@ -45,6 +60,7 @@ void script() {
fs::remove(path.metadata.script);
fs::copy(copying.items.at(0), path.metadata.script);
fs::permissions(path.metadata.script, fs::perms::owner_exec, fs::perm_options::add);

if (output_silent || confirmation_silent) return;
stopIndicator();
fprintf(stderr, formatColors("[success][inverse] โœ” [noinverse] Saved script \"%s\"[blank]\n").data(), fileContents(path.metadata.script).value().data());
Expand Down Expand Up @@ -72,6 +88,10 @@ void script() {
} // namespace PerformAction

void runClipboardScript() {
//std::cout << "Run for this action: " << runForThisAction << std::endl;
//std::cout << "Run before: " << runBefore << std::endl;
//std::cout << "Run after: " << runAfter << std::endl;

if (!fs::is_regular_file(path.metadata.script)) return;

#if defined(_WIN32) || defined(_WIN64)
Expand Down Expand Up @@ -101,13 +121,39 @@ void runClipboardScript() {
}
};

if (!secondRun)
execute("before");
else
execute("after");
if (runForThisAction) {
if (!secondRun && runBefore) {
execute("before");
} else if (secondRun && runAfter) {
execute("after");
}
}

fs::current_path(currentPath);

secondRun = true;
#endif
}
}

void checkClipboardScriptEligibility() {
if (!fs::is_regular_file(path.metadata.script_config)) return;

auto lines = fileLines(path.metadata.script_config, true);

if (lines.empty()) return;

auto scriptActions = regexSplit(lines[0], std::regex(" "));

if (!scriptActions.empty() && scriptActions.back() != "") {
runForThisAction = (std::find(scriptActions.begin(), scriptActions.end(), actions[action]) != scriptActions.end()) || (std::find(scriptActions.begin(), scriptActions.end(), action_shortcuts[action]) != scriptActions.end());
}

if (lines.size() < 2) return;

auto scriptTimings = regexSplit(lines[1], std::regex(" "));

if (!scriptTimings.empty() && scriptTimings.back() != "") {
runBefore = std::find(scriptTimings.begin(), scriptTimings.end(), "before") != scriptTimings.end();
runAfter = std::find(scriptTimings.begin(), scriptTimings.end(), "after") != scriptTimings.end();
}
}
1 change: 1 addition & 0 deletions src/cb/src/clipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Clipboard::Clipboard(const std::string& clipboard_name, const unsigned long& cli
metadata.notes = metadata / constants.notes_name;
metadata.originals = metadata / constants.original_files_name;
metadata.script = metadata / constants.script_name;
metadata.script_config = metadata / constants.script_config_name;
metadata.version = metadata / constants.storage_protocol_version_name;

fs::create_directories(data);
Expand Down
7 changes: 6 additions & 1 deletion src/cb/src/clipboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ struct Constants {
std::string_view ignore_secret_name = "ignore.secret";
std::string_view lock_name = "lock";
std::string_view script_name = "script";
std::string_view script_config_name = "script.config";
std::string_view data_directory = "data";
std::string_view metadata_directory = "metadata";
std::string_view import_export_directory = "Exported_Clipboards";
Expand Down Expand Up @@ -144,7 +145,7 @@ static auto thisPID() {
}

std::optional<std::string> fileContents(const fs::path& path);
std::vector<std::string> fileLines(const fs::path& path);
std::vector<std::string> fileLines(const fs::path& path, bool includeEmptyLines = false);

bool stopIndicator(bool change_condition_variable = true);

Expand Down Expand Up @@ -176,6 +177,8 @@ extern bool secret_selection;

extern std::string preferred_mime;
extern std::vector<std::string> available_mimes;
extern std::vector<std::string> script_actions;
extern std::vector<std::string> script_timings;

enum class ClipboardState : int {
Setup,
Expand Down Expand Up @@ -318,6 +321,7 @@ class Clipboard {
fs::path notes;
fs::path originals;
fs::path script;
fs::path script_config;
fs::path version;
operator fs::path() { return root; }
operator fs::path() const { return root; }
Expand Down Expand Up @@ -463,6 +467,7 @@ void setTheme(const std::string_view& theme);
size_t totalDirectorySize(const fs::path& directory);
size_t directoryOverhead(const fs::path& directory);
void runClipboardScript();
void checkClipboardScriptEligibility();

extern Message help_message;
extern Message check_clipboard_status_message;
Expand Down
2 changes: 2 additions & 0 deletions src/cb/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ int main(int argc, char* argv[]) {

checkItemSize(totalItemSize());

checkClipboardScriptEligibility();

runClipboardScript();

performAction();
Expand Down
11 changes: 6 additions & 5 deletions src/cb/src/utils/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ std::optional<std::string> fileContents(const fs::path& path) {
#endif
}

std::vector<std::string> fileLines(const fs::path& path) {
std::vector<std::string> fileLines(const fs::path& path, bool includeEmptyLines) {
std::vector<std::string> lines;
std::ifstream input_file(path, std::ios::binary);
for (std::string line; !input_file.eof();) {
std::getline(input_file, line);
if (!line.empty()) lines.emplace_back(line);
auto content = fileContents(path);
if (!content) return lines;
std::istringstream stream(*content);
for (std::string line; std::getline(stream, line);) {
if (!line.empty() || includeEmptyLines) lines.emplace_back(line);
}
return lines;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cb/src/utils/formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ std::string makeControlCharactersVisible(const std::string_view& oldStr, size_t
const std::array<std::pair<char, std::string_view>, 8> replacementCharacters {
{{'\n', "\\n"}, {'\r', "\\r"}, {'\a', "\\a"}, {'\b', "\\b"}, {'\f', "\\f"}, {'\t', "\\t"}, {'\v', "\\v"}, {'\0', "\\0"}}};

for (size_t i = 0; i < len - 1 && i < oldStr.size(); i++) {
for (size_t i = 0; i < len && i < oldStr.size(); i++) {
bool matched = false;
for (const auto& [character, replacement] : replacementCharacters) {
if (oldStr[i] == character) {
Expand Down
6 changes: 5 additions & 1 deletion src/cb/src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ std::string maximumHistorySize;

std::string preferred_mime;
std::vector<std::string> available_mimes;
std::vector<std::string> script_actions;
std::vector<std::string> script_timings;

std::vector<std::string> arguments;

Expand Down Expand Up @@ -468,6 +470,8 @@ void setFlags() {
if (flagIsPresent<bool>("--fast-copy") || flagIsPresent<bool>("-fc")) copying.use_safe_copy = false;
if (auto flag = flagIsPresent<std::string>("--mime"); flag != "") preferred_mime = flag;
if (auto flag = flagIsPresent<std::string>("-m"); flag != "") preferred_mime = flag;
if (auto flag = flagIsPresent<std::string>("--actions"); flag != "") script_actions = regexSplit(flag, std::regex(","));
if (auto flag = flagIsPresent<std::string>("--timings"); flag != "") script_timings = regexSplit(flag, std::regex(","));
if (flagIsPresent<bool>("--no-progress") || flagIsPresent<bool>("-np")) progress_silent = true;
if (flagIsPresent<bool>("--no-confirmation") || flagIsPresent<bool>("-nc")) confirmation_silent = true;
if (flagIsPresent<bool>("--secret") || flagIsPresent<bool>("-s")) secret_selection = true;
Expand Down Expand Up @@ -769,4 +773,4 @@ void showSuccesses() {
else if ((successes.files > 1) && (successes.directories > 1))
fprintf(stderr, many_files_many_directories_success_message().data(), did_action[action].data(), successes.files.load(), successes.directories.load());
}
}
}

0 comments on commit a037cc4

Please sign in to comment.