Skip to content

Commit

Permalink
show entry size in history improve script
Browse files Browse the repository at this point in the history
  • Loading branch information
Slackadays committed Oct 17, 2024
1 parent 916616f commit 113f998
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 66 deletions.
1 change: 0 additions & 1 deletion src/cb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ add_executable(cb
src/locales/de_de.cpp
src/locales/fr_fr.cpp
src/utils/editors.cpp
src/utils/runners.cpp
src/utils/utils.cpp
src/utils/formatting.cpp
src/utils/files.cpp
Expand Down
3 changes: 0 additions & 3 deletions src/cb/src/actions/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ void config() {
// Clipbord editor
fprintf(stderr, formatColors("[info]%sโ”ƒ Content editor: [help]%s[blank]\n").data(), generatedEndbar().data(), findUsableEditor() ? findUsableEditor().value().data() : "None");

// Clipboard script runner
fprintf(stderr, formatColors("[info]%sโ”ƒ Script runner: [help]%s[blank]\n").data(), generatedEndbar().data(), getenv("CLIPBOARD_SCRIPT_RUNNER") ? getenv("CLIPBOARD_SCRIPT_RUNNER") : "default");

// Max history size
fprintf(stderr, formatColors("[info]%sโ”ƒ Max history size: [help]%s[blank]\n").data(), generatedEndbar().data(), !maximumHistorySize.empty() ? maximumHistorySize.data() : "unlimited");

Expand Down
25 changes: 19 additions & 6 deletions src/cb/src/actions/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ void history() {
return;
}
std::vector<std::string> dates(path.entryIndex.size());
std::vector<std::string> sizes(path.entryIndex.size());

std::atomic<size_t> atomicLongestDateLength = 0;
std::atomic<size_t> atomicLongestSizeLength = 0;

auto now = std::chrono::system_clock::now();

Expand All @@ -82,14 +84,15 @@ void history() {

std::vector<std::thread> threads(totalThreads);

auto dateWorker = [&](const unsigned long& start, const unsigned long& end) {
auto dataWorker = [&](const unsigned long& start, const unsigned long& end) {
struct stat dateInfo;
std::string agoMessage;
agoMessage.reserve(16);

for (auto entry = start; entry < end; entry++) {
auto thisEntrysPath = path.entryPathFor(entry);
#if defined(UNIX_OR_UNIX_LIKE)
stat(path.entryPathFor(entry).string().data(), &dateInfo);
stat(thisEntrysPath.string().data(), &dateInfo);
auto timeSince = now - std::chrono::system_clock::from_time_t(dateInfo.st_mtime);
// format time like 1y 2d 3h 4m 5s
auto years = std::chrono::duration_cast<std::chrono::years>(timeSince);
Expand All @@ -110,14 +113,22 @@ void history() {
dates[entry] = "n/a";
atomicLongestDateLength.store(3, std::memory_order_relaxed);
#endif

size_t size = 0;
if (auto temp(fileContents(thisEntrysPath / constants.data_file_name)); temp.has_value())
size = temp.value().length();
else
size = totalDirectorySize(thisEntrysPath);
sizes[entry] = formatBytes(size);
if (sizes[entry].length() > atomicLongestSizeLength.load(std::memory_order_relaxed)) atomicLongestSizeLength.store(sizes[entry].length(), std::memory_order_relaxed);
}
};

for (unsigned long thread = 0; thread < totalThreads; thread++) {
auto start = thread * entriesPerThread;
auto end = start + entriesPerThread;
if (thread == totalThreads - 1) end = path.entryIndex.size();
threads[thread] = std::thread(dateWorker, start, end);
threads[thread] = std::thread(dataWorker, start, end);
}

// for (auto& thread : threads)
Expand Down Expand Up @@ -169,6 +180,7 @@ void history() {
thread.join();

size_t longestDateLength = atomicLongestDateLength.load(std::memory_order_relaxed);
size_t longestSizeLength = atomicLongestSizeLength.load(std::memory_order_relaxed);

for (long entry = path.entryIndex.size() - 1; entry >= 0; entry--) {
path.setEntry(entry);
Expand Down Expand Up @@ -197,16 +209,17 @@ void history() {
#endif
}

int widthRemaining = available.columns - (numberLength(entry) + longestEntryLength + longestDateLength + 7);
int widthRemaining = available.columns - (numberLength(entry) + longestEntryLength + longestDateLength + longestSizeLength + 7);

batchedMessage += preformattedMessageParts[0] + std::string(longestEntryLength - numberLength(entry), ' ') + std::to_string(entry) + preformattedMessageParts[1]
+ std::string(longestDateLength - dates.at(entry).length(), ' ') + dates.at(entry) + preformattedMessageParts[2];
+ std::string(longestDateLength - dates.at(entry).length(), ' ') + dates.at(entry) + preformattedMessageParts[1]
+ std::string(longestSizeLength - sizes.at(entry).length(), ' ') + sizes.at(entry) + preformattedMessageParts[2];

if (auto temp(fileContents(path.data.raw)); temp.has_value()) {
auto content = std::move(temp.value());
if (content.empty()) continue; // don't use holdsRawDataInCurrentEntry because we are reading anyway, so we can save on a syscall
if (auto MIMEtype = inferMIMEType(content); MIMEtype.has_value())
content = "\033[7m\033[1m " + std::string(MIMEtype.value()) + ", " + formatBytes(content.length()) + " \033[22m\033[27m";
content = "\033[7m\033[1m " + std::string(MIMEtype.value()) + " \033[22m\033[27m";
else
content = removeExcessWhitespace(content, available.columns * 2);
content = makeControlCharactersVisible(content, available.columns);
Expand Down
18 changes: 14 additions & 4 deletions src/cb/src/actions/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ void script() {
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, 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());
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;
}
Expand Down Expand Up @@ -69,14 +76,17 @@ void runClipboardScript() {
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");
if (res != 0) fprintf(stderr, "%s", formatColors("[error][inverse] โœ˜ [noinverse] Failed to set the CLIPBOARD_ACTION environment variable[blank]\n").data());

// 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");
if (res != 0) fprintf(stderr, "%s", formatColors("[error][inverse] โœ˜ [noinverse] Failed to set the CLIPBOARD_SCRIPT_TIMING environment variable[blank]\n").data());

res = system(path.metadata.script.string().c_str());
if (res != 0) throw std::runtime_error("Failed to run the clipboard script");
if (res != 0) {
res = WEXITSTATUS(res);
fprintf(stderr, formatColors("[error][inverse] โœ˜ [noinverse] Failed to run the clipboard script (returned exit code [bold]%d[nobold])[blank]\n").data(), res);
}
};

if (!secondRun)
Expand Down
3 changes: 1 addition & 2 deletions src/cb/src/clipboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ class Message {
};

std::string formatNumbers(const auto& num) {
static std::stringstream ss;
thread_local static std::stringstream ss;
ss.str(std::string());
ss << std::fixed << std::setprecision(2) << num;
return ss.str();
Expand Down Expand Up @@ -467,7 +467,6 @@ extern void writeToGUIClipboard(const ClipboardContent& clipboard);
extern const bool GUIClipboardSupportsCut;
extern bool playAsyncSoundEffect(const std::valarray<short>& samples);
extern std::optional<std::string> findUsableEditor();
extern std::optional<std::string> findUsableScriptRunner();

namespace PerformAction {
void copyItem(const fs::path& f, const bool use_regular_copy = copying.use_safe_copy);
Expand Down
2 changes: 1 addition & 1 deletion src/cb/src/utils/directorysize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ size_t directoryOverhead(const fs::path& directory) {
#endif
}

size_t size = 0;
thread_local size_t size = 0; // thread_local because multiple threads could call ftwHandler

#if defined(UNIX_OR_UNIX_LIKE)
int ftwHandler(const char* fpath, const struct stat* sb, int typeflag) {
Expand Down
49 changes: 0 additions & 49 deletions src/cb/src/utils/runners.cpp

This file was deleted.

0 comments on commit 113f998

Please sign in to comment.