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>
|
2023-03-10 15:43:25 +00:00
|
|
|
#include <AK/String.h>
|
2019-09-17 21:45:38 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-09 03:02:46 +01:00
|
|
|
#include <LibCore/File.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 {
|
2023-03-10 15:43:25 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2022-09-13 22:02:20 +01:00
|
|
|
static ErrorOr<Options> parse_options(Main::Arguments arguments)
|
2019-09-17 21:45:38 +03:00
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
auto type = "text/plain"sv;
|
2023-03-10 15:43:25 +00:00
|
|
|
Vector<StringView> 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.
|
2023-02-09 03:02:46 +01:00
|
|
|
auto c_stdin = TRY(Core::File::standard_input());
|
2022-12-11 17:49:00 +01:00
|
|
|
auto buffer = TRY(c_stdin->read_until_eof());
|
2021-01-09 15:09:40 +01:00
|
|
|
dbgln("Read size {}", buffer.size());
|
2022-09-13 22:02:20 +01:00
|
|
|
dbgln("Read data: `{}`", StringView(buffer.bytes()));
|
2023-03-10 15:43:25 +00:00
|
|
|
options.data = TRY(String::from_utf8(StringView(buffer.bytes())));
|
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);
|
2023-03-10 15:43:25 +00:00
|
|
|
options.data = TRY(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
|
|
|
|
2022-09-13 22:02:20 +01:00
|
|
|
Options options = TRY(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
|
|
|
}
|