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