2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-05-10 16:05:03 +00:00
|
|
|
* Copyright (c) 2019-2021, Sergey Bugaev <bugaevc@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-15 10:34:40 +00:00
|
|
|
#include <AK/Assertions.h>
|
2019-09-17 18:45:38 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/StringBuilder.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/File.h>
|
2021-11-25 19:58:56 +00:00
|
|
|
#include <LibCore/System.h>
|
2020-02-06 19:33:02 +00:00
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
#include <LibGUI/Clipboard.h>
|
2021-11-25 19:58:56 +00:00
|
|
|
#include <LibMain/Main.h>
|
2021-03-12 16:29:37 +00:00
|
|
|
#include <unistd.h>
|
2019-09-17 18:45:38 +00:00
|
|
|
|
|
|
|
struct Options {
|
|
|
|
String data;
|
2020-05-15 19:35:03 +00:00
|
|
|
StringView type;
|
2021-05-10 16:05:03 +00:00
|
|
|
bool clear;
|
2019-09-17 18:45:38 +00:00
|
|
|
};
|
|
|
|
|
2021-11-25 19:58:56 +00:00
|
|
|
static Options parse_options(Main::Arguments arguments)
|
2019-09-17 18:45:38 +00:00
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
auto type = "text/plain"sv;
|
2022-04-02 15:48:05 +00:00
|
|
|
Vector<String> text;
|
2021-05-10 16:05:03 +00:00
|
|
|
bool clear = false;
|
2019-09-17 18:45:38 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 15:22:58 +00:00
|
|
|
args_parser.set_general_help("Copy text from stdin or the command-line to the clipboard.");
|
2020-01-27 17:25:36 +00:00
|
|
|
args_parser.add_option(type, "Pick a type", "type", 't', "type");
|
2021-05-10 16:05:03 +00:00
|
|
|
args_parser.add_option(clear, "Instead of copying, clear the clipboard", "clear", 'c');
|
2020-02-02 11:34:39 +00:00
|
|
|
args_parser.add_positional_argument(text, "Text to copy", "text", Core::ArgsParser::Required::No);
|
2021-11-25 19:58:56 +00:00
|
|
|
args_parser.parse(arguments);
|
2019-09-17 18:45:38 +00:00
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
Options options;
|
|
|
|
options.type = type;
|
2021-05-10 16:05:03 +00:00
|
|
|
options.clear = clear;
|
2019-09-17 18:45:38 +00:00
|
|
|
|
2021-05-10 16:05:03 +00:00
|
|
|
if (clear) {
|
|
|
|
// We're not copying anything.
|
|
|
|
} else if (text.is_empty()) {
|
2019-09-17 18:45:38 +00:00
|
|
|
// Copy our stdin.
|
2020-02-02 11:34:39 +00:00
|
|
|
auto c_stdin = Core::File::construct();
|
2019-09-21 18:50:06 +00:00
|
|
|
bool success = c_stdin->open(
|
2019-09-17 18:45:38 +00:00
|
|
|
STDIN_FILENO,
|
2021-05-12 09:26:43 +00:00
|
|
|
Core::OpenMode::ReadOnly,
|
2020-10-25 11:31:27 +00:00
|
|
|
Core::File::ShouldCloseFileDescriptor::No);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(success);
|
2019-09-21 18:50:06 +00:00
|
|
|
auto buffer = c_stdin->read_all();
|
2021-01-09 14:09:40 +00:00
|
|
|
dbgln("Read size {}", buffer.size());
|
2019-09-17 18:45:38 +00:00
|
|
|
options.data = String((char*)buffer.data(), buffer.size());
|
2020-01-27 17:25:36 +00:00
|
|
|
} else {
|
|
|
|
// Copy the rest of our command-line args.
|
|
|
|
StringBuilder builder;
|
2020-03-20 13:40:17 +00:00
|
|
|
builder.join(' ', text);
|
2020-01-27 17:25:36 +00:00
|
|
|
options.data = builder.to_string();
|
2019-09-17 18:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2021-11-25 19:58:56 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-09-17 18:45:38 +00:00
|
|
|
{
|
2022-04-16 16:17:39 +00:00
|
|
|
auto app = TRY(GUI::Application::try_create(arguments));
|
2019-09-17 18:45:38 +00:00
|
|
|
|
2021-11-25 19:58:56 +00:00
|
|
|
Options options = parse_options(arguments);
|
2019-09-17 18:45:38 +00:00
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
auto& clipboard = GUI::Clipboard::the();
|
2021-05-10 16:05:03 +00:00
|
|
|
if (options.clear)
|
|
|
|
clipboard.clear();
|
|
|
|
else
|
|
|
|
clipboard.set_data(options.data.bytes(), options.type);
|
2019-09-23 07:36:25 +00:00
|
|
|
|
|
|
|
return 0;
|
2019-09-17 18:45:38 +00:00
|
|
|
}
|