| 24 | |
| 25 | namespace PerformAction { |
| 26 | void script() { |
| 27 | std::string dataToWrite; |
| 28 | for (const auto& action : script_actions) { |
| 29 | dataToWrite += action + " "; |
| 30 | } |
| 31 | if (dataToWrite.back() == ' ') dataToWrite.back() = '\n'; else dataToWrite += '\n'; |
| 32 | for (const auto& timing : script_timings) { |
| 33 | dataToWrite += timing + " "; |
| 34 | } |
| 35 | if (dataToWrite.back() == ' ') dataToWrite.back() = '\n'; else dataToWrite += '\n'; |
| 36 | writeToFile(path.metadata.script_config, dataToWrite); |
| 37 | checkClipboardScriptEligibility(); |
| 38 | if (io_type == IOType::File) { |
| 39 | if (copying.items.size() > 1) { |
| 40 | 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")); |
| 41 | return; |
| 42 | } |
| 43 | if (copying.items.empty()) { |
| 44 | stopIndicator(); |
| 45 | if (fs::is_regular_file(path.metadata.script)) { |
| 46 | fprintf(stderr, formatColors("[info]┃ Here is this clipboard's current script: [help]%s[blank]\n").data(), fileContents(path.metadata.script).value().data()); |
| 47 | } else { |
| 48 | |
| 49 | fprintf(stderr, |
| 50 | 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 " |
| 51 | "myscript.sh[nobold], or specify it as an argument, like [bold]%s %s \"echo Hello World!\".[blank]\n") |
| 52 | .data(), |
| 53 | clipboard_invocation.data(), |
| 54 | actions[action].data(), |
| 55 | clipboard_invocation.data(), |
| 56 | actions[action].data()); |
| 57 | } |
| 58 | return; |
| 59 | } |
| 60 | fs::remove(path.metadata.script); |
| 61 | fs::copy(copying.items.at(0), path.metadata.script); |
| 62 | fs::permissions(path.metadata.script, fs::perms::owner_exec, fs::perm_options::add); |
| 63 | |
| 64 | if (output_silent || confirmation_silent) return; |
| 65 | stopIndicator(); |
| 66 | fprintf(stderr, formatColors("[success][inverse] ✔ [noinverse] Saved script \"%s\"[blank]\n").data(), fileContents(path.metadata.script).value().data()); |
| 67 | } else if (io_type == IOType::Text) { |
| 68 | if (copying.items.at(0).string() == "") { |
| 69 | fs::remove(path.metadata.script); |
| 70 | if (output_silent || confirmation_silent) return; |
| 71 | stopIndicator(); |
| 72 | fprintf(stderr, "%s", formatColors("[success][inverse] ✔ [noinverse] Removed script[blank]\n").data()); |
| 73 | } else { |
| 74 | for (size_t i = 0; i < copying.items.size(); i++) { |
| 75 | copying.buffer += copying.items.at(i).string(); |
| 76 | if (i != copying.items.size() - 1) copying.buffer += " "; |
| 77 | } |
| 78 | fs::remove(path.metadata.script); |
| 79 | writeToFile(path.metadata.script, copying.buffer); |
| 80 | fs::permissions(path.metadata.script, fs::perms::owner_exec, fs::perm_options::add); |
| 81 | if (output_silent || confirmation_silent) return; |
| 82 | stopIndicator(); |
| 83 | fprintf(stderr, formatColors("[success][inverse] ✔ [noinverse] Saved script \"%s\"[blank]\n").data(), fileContents(path.metadata.script).value().data()); |
no test coverage detected