Slackadays / Clipboard

😎🏖️🐬 Your new, 𝙧𝙞𝙙𝙤𝙣𝙠𝙪𝙡𝙞𝙘𝙞𝙤𝙪𝙨𝙡𝙮 smart clipboard manager
https://getclipboard.app/
GNU General Public License v3.0
4.81k stars 144 forks source link

CLI output on Mac in zsh seems broken #147

Closed pmalek closed 1 year ago

pmalek commented 1 year ago

Describe the bug

CLI output on Mac in zsh seems broken

To Reproduce

Steps to reproduce the behavior:

  1. Install via brew install clipboard
  2. Run cb
  3. Observe broken CLI output

Expected behavior

CLI prints clearly and can be understood without any problems.

Screenshots

cb
[info]┏━━[inverse] [info][bold]All of your clipboards with content[nobold] [noinverse]━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓[blank]
┃ [bold]0[nobold]│ [blank][help]qweerty[blank]                                                                                                                                                                                                                                             ┃
[info]┗━━▌[help]Text, Files, Directories,  Data [info]▐━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛[blank]
cb --help
[info]┃ This is the Clipboard Project 0.8.1 (commit ), the cut, copy, and paste system for the command line.[blank]
[info][bold]┃ Examples[blank]
[progress]┃ cb ct Nuclear_Launch_Codes.txt contactsfolder[blank] [help](This cuts the following items into the default clipboard, 0.)[blank]
[progress]┃ clipboard cp1 dogfood.conf[blank] [help](This copies the following items into clipboard 1.)[blank]
[progress]┃ cb p1[blank] [help](This pastes clipboard 1.)[blank]
[progress]┃ cb sh4[blank] [help](This shows the contents of clipboard 4.)[blank]
[progress]┃ cb clr[blank] [help](This clears the contents of the default clipboard.)[blank]
[info]┃ You can also choose which clipboard you want to use by adding a number to the end, or [bold]_[nobold] to use a persistent clipboard.[blank]
[info][bold]┃ More Info[blank]
[info]┃ See the complete online documentation for CB at https://github.com/Slackadays/Clipboard.[blank]
[info]┃ Show this help screen anytime with [bold]cb -h[nobold], [bold]cb --help[nobold], or[bold] cb help[nobold].
[info]┃ You can also get more help in our Discord server at [bold]https://discord.gg/J6asnc3pEG[blank]
[info][bold]┃ All Actions Available[blank]
[progress]┃ cut,     ct │ [help]Cut items into a clipboard.[blank]
[progress]┃ copy,    cp │ [help]Copy items into a clipboard.[blank]
[progress]┃ paste,   p  │ [help]Paste items from a clipboard.[blank]
[progress]┃ clear,   clr│ [help]Clear a clipboard.[blank]
[progress]┃ show,    sh │ [help]Show the contents of a clipboard.[blank]
[progress]┃ edit,    ed │ [help]Edit the contents of a clipboard.[blank]
[progress]┃ add,     ad │ [help]Add items to a clipboard.[blank]
[progress]┃ remove,  rm │ [help]Remove items from a clipboard.[blank]
[progress]┃ note,    nt │ [help]Add a note to a clipboard.[blank]
[progress]┃ swap,    sw │ [help]Swap the contents of multiple clipboards.[blank]
[progress]┃ status,  st │ [help]Check the status of a clipboard.[blank]
[progress]┃ info,    in │ [help]Show info about a clipboard.[blank]
[progress]┃ load,    ld │ [help]Load a clipboard into another clipboard.[blank]
[progress]┃ import,  imp│ [help]Import a clipboard from a file.[blank]
[progress]┃ export,  ex │ [help]Export a clipboard to a file.[blank]
[progress]┃ history, hs │ [help]Show the history of a clipboard.[blank]
[progress]┃ ignore,  ig │ [help]Ignore types of content in a clipboard.[blank]
[progress]┃ search,  sr │ [help]Search for items in a clipboard.[blank]
[info]┃ Copyright (C) 2023 Jackson Huff. Licensed under the GPLv3.[blank]
[info]┃ This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions.[blank]

Version

cb --help
[info]┃ This is the Clipboard Project 0.8.1 (commit ), the cut, copy, and paste system for the command line.[blank]
mccartykim commented 1 year ago

I see similar issues install clipboard via brew in Fish in iTerm 2:

image

But if I download the prebuilt binary from releases, it looks like I think it should: image

Maybe it's an issue with how the brew package is configured? I hope it's fixed because if I manually install this I won't remember to update it :(

pmalek commented 1 year ago

Same here

Slackadays commented 1 year ago

looks like an issue with the brew package, as I just tested the compiled version on my Mac Mini at MacStadium and it looks just fine, but I don't believe this issue could have been caused in the first place?

Slackadays commented 1 year ago

here is the code that deals with the colors:

std::string formatColors(const std::string_view& str, bool colorful) {
    std::string temp(str);
    for (size_t i = 0; (i = temp.find('[', i)) != std::string::npos; i++) {
        auto j = temp.find(']', i);
        if (j == std::string::npos) break;
        const std::string_view result = temp.substr(i, j - i + 1);
        for (const auto& key : colors) {
            if (key.first == result) {
                temp.replace(i, result.length(), colorful ? key.second : "");
                if (!colorful) i--; // because i may be at the start of the next color
                break;
            }
        }
    }
    return temp;
}

judging by how this only happens on Homebrew, this looks like it's a compiler bug (which would be sad).

@pmalek @mccartykim are you able to see which compiler compiled the Homebrew binary with objdump -s $(which cb)?

You may also want to open an issue here: https://github.com/Homebrew/homebrew-core/issues

Check this out: https://github.com/Homebrew/homebrew-core/issues/136551

carlocab commented 1 year ago

We applied a workaround at Homebrew which should fix this issue. You can do brew update && brew upgrade clipboard to pick up the fix.

However, it looks like this was caused by a bug in either of Clipboard or in Clang, so we should try to fix it if it's the former or report it upstream if it's the latter.

pmalek commented 1 year ago

Looks good now! Thanks!

image
Slackadays commented 1 year ago

I just went through some of my compile logs, and it turns out that there was a warning from Clang that said something about a deleted memory location once some string construction is done.

What this meant was, the reason this issue happened in the first place is because there is a Clang optimization that only happens with Os on ARM, where the memory of the found replacement result became nothing, so no colors ever got replaced. The reason I couldn't catch this is because CB switched to CMake's Release mode (O3) for all official builds, so it never happened in testing on my Mac Mini.

Last night, I made an optimization to this function that just so happened to fix the underlying issue once and for all by properly utilizing memory. And today, I made the whole thing 2x faster by making a manual comparison function.

This means that the specific O2 workaround can be removed without breaking anything. Unfortunately, in my testing, it turns out that with this new manual comparison function, Os (the default) is SLOWER than even O1 because of tons of function calls that aren't inlined. A similar thing is also present, at a much larger scale, elsewhere in the project. Therefore, the new O2 option is needed to keep performance high.