Skip to content

Commit

Permalink
Fix Win, add config docs, add man, add dedup
Browse files Browse the repository at this point in the history
  • Loading branch information
Slackadays committed Dec 27, 2022
1 parent 6fd7e6b commit ad26acb
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if(TEST)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
endif()
else()
if(NOT CMAKE_BUILD_TYPE)
if(NOT CMAKE_BUILD_TYPE AND NOT WIN32)
set(CMAKE_BUILD_TYPE MinSizeRel)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -s") # strip the binary for reduced size
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,32 @@ Windows:

This is currently WIP

## Actions Install

You can also install Clipboard by downloading the latest build from GitHub Actions.

## AUR Install

Arch-Linux users can install the [clipboard](https://aur.archlinux.org/packages/clipboard), [clipboard-bin](https://aur.archlinux.org/packages/clipboard-bin), or [clipboard-git](https://aur.archlinux.org/packages/clipboard-git) AUR package.

# (Really) Simple Configuration

## `CI`

Set this environment variable to make Clipboard overwrite existing items without a user prompt when pasting. This variable is intended for Continuous Integration scripts where a live human is not present to make decisions.

## `FORCE_COLOR`

Set this environment variable to make Clipboard always show color regardless of what you set `NO_COLOR` to.

## `TMPDIR`

Set this environment variable to the directory that Clipboard will use to hold the items you cut or copy.

## `NO_COLOR`

Set this environment variable to make Clipboard not show any colors.

# Painless Documentation

[Click here](https://github.com/Slackadays/Clipboard/wiki) to go the Clipboard Wiki.
Expand Down
77 changes: 77 additions & 0 deletions man/man.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
.\" Automatically generated by Pandoc 2.9.2.1
.\"
.TH "" "" "" "" ""
.hy
.SH clipboard(1) \[en] cut, copy, and paste in the terminal
.SS SYNOPSIS
.PP
\f[B]clipboard\f[R] [copy|cp][(id)] [cut|ct][id] [paste|p][(id)] (files)
.PP
\f[B]clipboard\f[R] [show|sh][(id)] [clear|clr][(id)]
.PP
(stdout/stderr) | \f[B]clipboard\f[R] [copy|cp][(id)]
.PP
\f[B]clipboard\f[R] [paste|p][(id)] | (stdin)
.PP
\f[B]clipboard\f[R] [paste|p][(id)] > (file)
.PP
\f[B]clipboard\f[R]
.SS DESCRIPTION
.PP
\f[B]clipboard\f[R] lets you cut, copy, and paste files and piped data
in the terminal.
It lacks dependencies on any GUI clipboard system, although they can be
enabled if you want.
If available, you can substitute \f[B]cb\f[R] for \f[B]clipboard\f[R] as
a shortcut.
.PP
Input a number into \f[B](id)\f[R] to select which clipboard you want to
use.
.SS FILES
.PP
\f[B]clipboard\f[R] stores its temporary data in the \f[B]Clipboard\f[R]
subdirectory in a system-provided temporary folder or in the
\f[B].clipboard\f[R] subdirectory in the user\[cq]s home folder.
There may be a symlink \f[B]cb\f[R] in the same directory where you
installed \f[B]clipboard\f[R].
.SS ENVIRONMENT VARIABLES
.SS \f[B]CI\f[R]
.PP
Set this environment variable to make Clipboard overwrite existing items
without a user prompt when pasting.
This variable is intended for Continuous Integration scripts where a
live human is not present to make decisions.
.SS \f[B]FORCE_COLOR\f[R]
.PP
Set this environment variable to make Clipboard always show color
regardless of what you set \f[B]NO_COLOR\f[R] to.
.SS \f[B]TMPDIR\f[R]
.PP
Set this environment variable to the directory that Clipboard will use
to hold the items you cut or copy.
.SS \f[B]NO_COLOR\f[R]
.PP
Set this environment variable to make Clipboard not show any colors.
.SS EXAMPLES
.IP
.nf
\f[C]
clipboard copy SomeFile.iso foobarDirectory
clipboard cut5 MyDirectory
cb cut69 bar.conf AnotherDirectory baz.txt
\f[R]
.fi
.SS FULL DOCUMENTATION
.PP
Full documentation is available at
\f[B]https://github.com/Slackadays/Clipboard\f[R].
.SS SUPPORT
.PP
Our Discord group is at \f[B]https://discord.gg/J6asnc3pEG\f[R].
.SS BUGS
.PP
Report all bugs to \f[B]https://github.com/Slackadays/Clipboard\f[R] or
\f[B]https://discord.gg/J6asnc3pEG\f[R].
.SS COPYRIGHT
.PP
Copyright (c) 2022 Jackson Huff.
68 changes: 68 additions & 0 deletions man/man.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
clipboard(1) -- cut, copy, and paste in the terminal
=====

## SYNOPSIS

**clipboard** [copy|cp][(id)] [cut|ct][id] [paste|p][(id)] (files)

**clipboard** [show|sh][(id)] [clear|clr][(id)]

(stdout/stderr) | **clipboard** [copy|cp][(id)]

**clipboard** [paste|p][(id)] | (stdin)

**clipboard** [paste|p][(id)] > (file)

**clipboard**

## DESCRIPTION

**clipboard** lets you cut, copy, and paste files and piped data in the terminal. It lacks dependencies on any GUI clipboard system, although they can be enabled if you want. If available, you can substitute **cb** for **clipboard** as a shortcut.

Input a number into **(id)** to select which clipboard you want to use.

## FILES

**clipboard** stores its temporary data in the **Clipboard** subdirectory in a system-provided temporary folder or in the **.clipboard** subdirectory in the user's home folder. There may be a symlink **cb** in the same directory where you installed **clipboard**.

## ENVIRONMENT VARIABLES

### **CI**

Set this environment variable to make Clipboard overwrite existing items without a user prompt when pasting. This variable is intended for Continuous Integration scripts where a live human is not present to make decisions.

### **FORCE_COLOR**

Set this environment variable to make Clipboard always show color regardless of what you set **NO_COLOR** to.

### **TMPDIR**

Set this environment variable to the directory that Clipboard will use to hold the items you cut or copy.

### **NO_COLOR**

Set this environment variable to make Clipboard not show any colors.

## EXAMPLES

```
clipboard copy SomeFile.iso foobarDirectory
clipboard cut5 MyDirectory
cb cut69 bar.conf AnotherDirectory baz.txt
```

## FULL DOCUMENTATION

Full documentation is available at __https://github.com/Slackadays/Clipboard__.

## SUPPORT

Our Discord group is at __https://discord.gg/J6asnc3pEG__.

## BUGS

Report all bugs to __https://github.com/Slackadays/Clipboard__ or __https://discord.gg/J6asnc3pEG__.

## COPYRIGHT

Copyright (c) 2022 Jackson Huff.
78 changes: 49 additions & 29 deletions src/clipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <utility>
#include <string_view>
#include <locale>
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <array>
Expand Down Expand Up @@ -59,7 +58,7 @@ bool stdin_is_tty = true;
bool stdout_is_tty = true;
bool stderr_is_tty = true;

constexpr std::string_view clipboard_version = "0.1.3";
constexpr std::string_view clipboard_version = "0.1.4";

std::array<std::pair<std::string_view, std::string_view>, 8> colors = {{
{"{red}", "\033[38;5;196m"},
Expand Down Expand Up @@ -129,13 +128,18 @@ std::string_view help_message = "{blue}▏This is Clipboard %s, the cut, copy, a
"{orange}▏clipboard show{blank} {pink}(This shows what's in a clipboard.){blank}\n"
"{orange}▏clipboard clear{blank} {pink}(This clears a clipboard's contents.){blank}\n"
"{blue}▏You can substitute \"cb\" for \"clipboard\" and use various shorthands for the actions to save time.{blank}\n"
"{blue}▏You can also choose which of the 10 clipboards that you have available you want to use by adding a number to the end.{blank}\n"
"{blue}▏You can also choose which clipboard you want to use by adding a number to the end, or {bold}-{blank}{blue} to use a permanent clipboard.{blank}\n"
"{blue}{bold}▏Examples{blank}\n"
"{orange}▏cb ct Nuclear_Launch_Codes.txt contactsfolder{blank} {pink}(This cuts the following items into the default clipboard, 0.){blank}\n"
"{orange}▏clipboard cp1 dogfood.conf{blank} {pink}(This copies the following items into clipboard 1.){blank}\n"
"{orange}▏cb p1{blank} {pink}(This pastes clipboard 1.){blank}\n"
"{orange}▏cb sh4{blank} {pink}(This shows the contents of clipboard 4.){blank}\n"
"{orange}▏cb clr{blank} {pink}(This clears the contents of the default clipboard.){blank}\n"
"{blue}{bold}▏Configuration{blank}\n"
"{orange}▏CI: {pink}Set to make Clipboard overwrite existing items without a user prompt when pasting.{blank}\n"
"{orange}▏FORCE_COLOR: {pink}Set to make Clipboard always show color regardless of what you set NO_COLOR to.{blank}\n"
"{orange}▏TMPDIR: {pink}Set to the directory that Clipboard will use to hold the items you cut or copy.{blank}\n"
"{orange}▏NO_COLOR: {pink}Set to make Clipboard not show color.{blank}\n"
"{blue}▏You can show this help screen anytime with {bold}clipboard -h{blank}{blue}, {bold}clipboard --help{blank}{blue}, or{bold} clipboard help{blank}{blue}.\n"
"{blue}▏You can also get more help in our Discord server at {bold}https://discord.gg/J6asnc3pEG{blank}\n"
"{blue}▏Copyright (C) 2022 Jackson Huff. Licensed under the GPLv3.{blank}\n"
Expand All @@ -157,7 +161,10 @@ std::string_view and_more_items_message = "{blue}▏ ...and {bold}%i{blank}{blue
std::string_view fix_problem_message = "{pink}▏ See if you have the needed permissions, or\n"
"▏ try double-checking the spelling of the files or what directory you're in.{blank}\n";
std::string_view not_enough_storage_message = "{red}╳ There won't be enough storage available to paste all your items (%gkB to paste, %gkB available).{blank}{pink} Try double-checking what items you've selected or delete some files to free up space.{blank}\n";
std::string_view item_already_exists_message = "{yellow}• The item {bold}%s{blank}{yellow} already exists here. Would you like to replace it? {pink}Add {bold}all {blank}{pink}or {bold}a{blank}{pink} to use this decision for all items. {bold}[(y)es/(n)o)] ";
std::string_view bad_response_message = "{red}╳ Sorry, that wasn't a valid choice. Try again: {blank}{pink}{bold}[(y)es/(n)o)] ";
std::string_view working_message = "{yellow}• %s... %i%s %s{blank}\r";
std::string_view cancelled_message = "{green}✓ Cancelled %s{blank}\n";
std::string_view pipe_success_message = "{green}✓ %s %i bytes{blank}\n";
std::string_view one_item_success_message = "{green}✓ %s %s{blank}\n";
std::string_view multiple_files_success_message = "{green}✓ %s %i files{blank}\n";
Expand All @@ -181,12 +188,21 @@ void setupSignals() {
signal(SIGINT, [](int dummy) {
indicator.request_stop();
fprintf(stderr, "\r%*s\r", output_length, "");
fprintf(stderr, replaceColors("{green}✓ Cancelled %s{blank}\n").data(), actions[action].data());
fprintf(stderr, replaceColors(cancelled_message).data(), actions[action].data());
fflush(stderr);
exit(1);
});
}

void checkFlags(const int argc, char *argv[]) {
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help") || (argc >= 2 && !strcmp(argv[1], "help"))) {
printf(replaceColors(help_message).data(), clipboard_version.data());
exit(0);
}
}
}

void setClipboardName(const int argc, char *argv[]) {
if (argc >= 2) {
clipboard_name = argv[1];
Expand All @@ -202,6 +218,18 @@ void setClipboardName(const int argc, char *argv[]) {
argv[1][strlen(argv[1]) - (clipboard_name.length() + use_perma_clip)] = '\0';
}
}

if (use_perma_clip) {
filepath = home_directory / ".clipboard" / clipboard_name;
} else {
if (getenv("TMPDIR") != nullptr) {
filepath = fs::path(getenv("TMPDIR")) / "Clipboard" / clipboard_name;
} else {
filepath = fs::temp_directory_path() / "Clipboard" / clipboard_name;
}
}

original_files_path = filepath.parent_path() / (clipboard_name + ".files");
}

void setupVariables(const int argc, char *argv[]) {
Expand All @@ -227,20 +255,6 @@ void setupVariables(const int argc, char *argv[]) {
home_directory = getenv("HOME");
#endif

setClipboardName(argc, argv);

if (use_perma_clip) {
filepath = home_directory / ".clipboard" / clipboard_name;
} else {
if (getenv("TMPDIR") != nullptr) {
filepath = fs::path(getenv("TMPDIR")) / "Clipboard" / clipboard_name;
} else {
filepath = fs::temp_directory_path() / "Clipboard" / clipboard_name;
}
}

original_files_path = filepath.parent_path() / (clipboard_name + ".files");

for (int i = 2; i < argc; i++) {
items.push_back(argv[i]);
}
Expand All @@ -262,15 +276,6 @@ void setupVariables(const int argc, char *argv[]) {
} catch (...) {}
}

void checkFlags(const int argc, char *argv[]) {
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help") || (argc >= 2 && !strcmp(argv[1], "help"))) {
printf(replaceColors(help_message).data(), clipboard_version.data());
exit(0);
}
}
}

void createTempDirectory() {
if (!fs::is_directory(filepath)) {
fs::create_directories(filepath);
Expand Down Expand Up @@ -469,6 +474,11 @@ void setupIndicator(std::stop_token st) {
}
}

void deduplicateItems() {
std::sort(items.begin(), items.end());
items.erase(std::unique(items.begin(), items.end()), items.end());
}

unsigned long long calculateTotalItemSize() {
unsigned long long total_item_size = 0;
for (const auto& i : items) {
Expand Down Expand Up @@ -587,7 +597,7 @@ int getUserDecision(const std::string& item) {
if (userIsARobot()) {
return 2;
}
fprintf(stderr, replaceColors("{yellow}• The item {bold}%s{blank}{yellow} already exists here. Would you like to replace it? {pink}Add {bold}all {blank}{pink}or {bold}a{blank}{pink} to use this decision for all items. {bold}[(y)es/(n)o)] ").data(), item.data());
fprintf(stderr, replaceColors(item_already_exists_message).data(), item.data());
std::string decision;
while (true) {
std::getline(std::cin, decision);
Expand All @@ -601,7 +611,7 @@ int getUserDecision(const std::string& item) {
} else if (decision == "na" || decision == "noall") {
return -2;
} else {
fprintf(stderr, "%s", replaceColors("{red}╳ Sorry, that wasn't a valid choice. Try again: {blank}{pink}{bold}[(y)es/(n)o)] ").data());
fprintf(stderr, "%s", replaceColors(bad_response_message).data());
}
}
}
Expand Down Expand Up @@ -724,6 +734,10 @@ void performAction() {
}
}

void updateGUIClipboard() {

}

void showFailures() {
if (failedItems.size() > 0) {
printf(replaceColors(clipboard_failed_message).data(), actions[action].data());
Expand Down Expand Up @@ -767,6 +781,8 @@ int main(int argc, char *argv[]) {

checkFlags(argc, argv);

setClipboardName(argc, argv);

createTempDirectory();

syncWithGUIClipboard();
Expand All @@ -777,12 +793,16 @@ int main(int argc, char *argv[]) {

indicator = std::jthread(setupIndicator);

deduplicateItems();

checkItemSize();

clearTempDirectory();

performAction();

updateGUIClipboard();

indicator.request_stop();
cv.notify_one();

Expand Down
2 changes: 0 additions & 2 deletions src/langs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ void setLanguageES() {
"▏ vuelve a revisar el deletro de los archivos o la carpeta en que estás.{blank}\n";
not_enough_storage_message = "{red}╳ No habrá espacio suficiente para pegar todas tus cosas (%gkB a pegar, %gkB disponible).{blank}{pink} Vuelve a revisar las cosas que especificaste o saca algunas cosas para hacer más espacio.{blank}\n";
pipe_success_message = "{green}✓ %s %i bytes{blank}\n";
one_item_success_message = "{green}✓ %s %s{blank}\n";
multiple_files_success_message = "{green}✓ %s %i archivos{blank}\n";
multiple_directories_success_message = "{green}✓ %s %i carpetas{blank}\n";
multiple_files_directories_success_message = "{green}✓ %s %i archivos y %i carpetas{blank}\n";
Expand Down Expand Up @@ -183,7 +182,6 @@ void setLanguageTR() {
"▏ bulunduğunuz dizini veya girdiğiniz dosya isimlerini ikinci kez kontrol edin.{blank}\n";
not_enough_storage_message = "{red}╳ Bütün öğelerinizi yapıştırabileceğin kadar yeterli bir alanınız yok (%gkB yapıştırılacak, %gkB boş).{blank}{pink} Hangi öğeleri seçtiğinizi ikinci kez kontrol etmeyi deneyin veya yer açmak için bazı dosyaları silin.{blank}\n";
pipe_success_message = "{green}✓ %s %i bayt{blank}\n";
one_item_success_message = "{green}✓ %s %s{blank}\n";
multiple_files_success_message = "{green}✓ %s %i dosya{blank}\n";
multiple_directories_success_message = "{green}✓ %s %i dizin{blank}\n";
multiple_files_directories_success_message = "{green}✓ %s %i dosya ve %i dizin{blank}\n";
Expand Down

0 comments on commit ad26acb

Please sign in to comment.