Skip to content

Commit 883a63a

Browse files
committed
Fix building on macOS Sonoma, make control chars visible
1 parent 3b76ba2 commit 883a63a

File tree

6 files changed

+33
-5
lines changed

6 files changed

+33
-5
lines changed

โ€Žsrc/cb/src/actions/history.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ void history() {
207207
if (auto MIMEtype = inferMIMEType(content); MIMEtype.has_value())
208208
content = "\033[7m\033[1m " + std::string(MIMEtype.value()) + ", " + formatBytes(content.length()) + " \033[22m\033[27m";
209209
else
210-
std::erase(content, '\n');
210+
content = makeControlCharactersVisible(content, available.columns);
211211
batchedMessage += content.substr(0, widthRemaining);
212212
continue;
213213
}

โ€Žsrc/cb/src/actions/search.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void displaySearchResults(const std::vector<Result>& results) {
5353
"",
5454
result.entry);
5555
std::string preview = result.preview;
56-
std::erase(preview, '\n');
56+
preview = makeControlCharactersVisible(preview, available.columns);
5757
auto widthRemaining = available.columns - (longestClipboardLength + longestEntryLength + 7);
5858
if (preview.length() > widthRemaining) {
5959
preview = preview.substr(0, widthRemaining - (preview.length() - columnLength(preview)));

โ€Žsrc/cb/src/actions/show.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ void show() {
2424

2525
stopIndicator();
2626

27+
auto available = thisTerminalSize();
28+
2729
if (path.holdsRawDataInCurrentEntry()) {
2830
std::string content(fileContents(path.data.raw).value());
29-
std::erase(content, '\n');
31+
content = makeControlCharactersVisible(content, available.columns);
3032
fprintf(stderr, clipboard_text_contents_message().data(), std::min(static_cast<size_t>(250), content.size()), clipboard_name.data());
3133
fprintf(stderr, formatColors("[bold][info]%s\n[blank]").data(), content.substr(0, 250).data());
3234
if (content.size() > 250) {
@@ -35,7 +37,6 @@ void show() {
3537
return;
3638
}
3739

38-
auto available = thisTerminalSize();
3940
fprintf(stderr, "%s", formatColors("[info]โ”โ”โ”").data());
4041
fprintf(stderr, clipboard_item_many_contents_message().data(), clipboard_name.data());
4142
fprintf(stderr, "%s", formatColors("[info]โ”").data());

โ€Žsrc/cb/src/actions/status.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void status() {
5858
if (auto type = inferMIMEType(content); type.has_value())
5959
content = "\033[7m\033[1m " + std::string(type.value()) + ", " + formatBytes(content.length()) + " \033[22m\033[27m";
6060
else
61-
std::erase(content, '\n');
61+
content = makeControlCharactersVisible(content, available.columns);
6262
fprintf(stderr, formatColors("[help]%s[blank]\n").data(), content.substr(0, widthRemaining).data());
6363
clipboard.releaseLock();
6464
continue;

โ€Žsrc/cb/src/clipboard.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
You should have received a copy of the GNU General Public License
1414
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
1515
#pragma once
16+
#include <algorithm>
1617
#include <array>
1718
#include <atomic>
1819
#include <chrono>
@@ -348,6 +349,7 @@ bool envVarIsTrue(const std::string_view& name);
348349
size_t columnLength(const std::string_view& message);
349350
std::string generatedEndbar();
350351
std::string repeatString(const std::string_view& character, const size_t& length);
352+
std::string makeControlCharactersVisible(const std::string_view& oldStr, size_t len = 0);
351353
unsigned long levenshteinDistance(const std::string_view& one, const std::string_view& two);
352354
void setLanguagePT();
353355
void setLanguageTR();

โ€Žsrc/cb/src/utils.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,31 @@ std::string formatColors(const std::string_view& oldStr, bool colorful) {
200200
return newStr;
201201
}
202202

203+
std::string makeControlCharactersVisible(const std::string_view& oldStr, size_t len) {
204+
std::string newStr;
205+
newStr.reserve(oldStr.size());
206+
207+
if (len == 0) len = oldStr.size();
208+
209+
// format characters such as \n and \r such that they appear as \n and \r surrounded by lightening terminal colors
210+
const std::array<std::pair<char, std::string_view>, 8> replacementCharacters {
211+
{{'\n', "\\n"}, {'\r', "\\r"}, {'\a', "\\a"}, {'\b', "\\b"}, {'\f', "\\f"}, {'\t', "\\t"}, {'\v', "\\v"}, {'\0', "\\0"}}};
212+
213+
for (size_t i = 0; i < len - 1 && i < oldStr.size(); i++) {
214+
bool matched = false;
215+
for (const auto& [character, replacement] : replacementCharacters) {
216+
if (oldStr[i] == character) {
217+
newStr += "\033[2m" + std::string(replacement) + "\033[22m";
218+
matched = true;
219+
break;
220+
}
221+
}
222+
if (!matched) newStr += oldStr[i];
223+
}
224+
225+
return newStr;
226+
}
227+
203228
std::string JSONescape(const std::string_view& input) {
204229
std::string temp(input);
205230

0 commit comments

Comments
ย (0)