Skip to content

Commit

Permalink
Script is almost finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Slackadays committed Oct 16, 2024
1 parent 0c1dbb2 commit 916616f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
49 changes: 44 additions & 5 deletions src/cb/src/actions/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,66 @@ void script() {
if (copying.items.empty()) {
stopIndicator();
if (fs::is_regular_file(path.metadata.script)) {
fprintf(stderr, formatColors("[info]โ”ƒ The current script is: [help]%s[blank]\n").data(), fileContents(path.metadata.script).value().data());
fprintf(stderr, formatColors("[info]โ”ƒ Here is this clipboard's current script: [help]%s[blank]\n").data(), fileContents(path.metadata.script).value().data());
} else {

fprintf(stderr, "%s", formatColors("[error][inverse] โœ˜ [noinverse] No script is currently set. [help]โฌค Try providing a script file to set one.[blank]\n").data());
fprintf(stderr, formatColors("[error][inverse] โœ˜ [noinverse] There is currently no script set for this clipboard. [help]โฌค To set a script, add it to the end, like [bold]%s %s myscript.sh[nobold], or specify it as an argument, like [bold]%s %s \"echo Hello World!\".[blank]\n").data(), clipboard_invocation.data(), actions[action].data(), clipboard_invocation.data(), actions[action].data());
}
return;
}
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());
} else if (io_type == IOType::Text) {
if (copying.items.at(0).string() == "") {
fs::remove(path.metadata.script);
if (output_silent || confirmation_silent) return;
stopIndicator();
fprintf(stderr, "%s", formatColors("[success][inverse] โœ” [noinverse] Removed script[blank]\n").data());
} else {
fs::remove(path.metadata.script);
fs::copy(copying.items.at(0), path.metadata.script);
writeToFile(path.metadata.script, copying.items.at(0).string());
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());
}
} else if (io_type == IOType::Text) {
}
}

} // namespace PerformAction
} // namespace PerformAction

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

static bool secondRun = false;

fs::path currentPath = fs::current_path();

fs::current_path(path.data);

auto execute = [&](const std::string_view& timing) {
// Set the CLIPBOARD_ACTION environment variable to the action that was performed
int res = setenv("CLIPBOARD_ACTION", actions[action].data(), 1);
if (res != 0) throw std::runtime_error("Failed to set the CLIPBOARD_ACTION environment variable");

// Set the CLIPBOARD_SCRIPT_TIMING environment variable to "before" or "after" depending on the timing
res = setenv("CLIPBOARD_SCRIPT_TIMING", timing.data(), 1);
if (res != 0) throw std::runtime_error("Failed to set the CLIPBOARD_SCRIPT_TIMING environment variable");

res = system(path.metadata.script.string().c_str());
if (res != 0) throw std::runtime_error("Failed to run the clipboard script");
};

if (!secondRun)
execute("before");
else
execute("after");

fs::current_path(currentPath);

secondRun = true;
}
1 change: 1 addition & 0 deletions src/cb/src/clipboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ void showSuccesses();
void setTheme(const std::string_view& theme);
size_t totalDirectorySize(const fs::path& directory);
size_t directoryOverhead(const fs::path& directory);
void runClipboardScript();

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

checkItemSize(totalItemSize());

runClipboardScript();

performAction();

runClipboardScript();

if (isAWriteAction()) path.applyIgnoreRules();

copying.mime = getMIMEType();
Expand Down
2 changes: 1 addition & 1 deletion src/cb/src/utils/formatting.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* The Clipboard Project - Cut, copy, and paste anything, anytime, anywhere, all from the terminal.
Copyright (C) 2023 Jackson Huff and other contributors on GitHub.com
Copyright (C) 2024 Jackson Huff and other contributors on GitHub.com
SPDX-License-Identifier: GPL-3.0-or-later
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down

0 comments on commit 916616f

Please sign in to comment.