2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2020-01-24 13:45:29 +00:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
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>
|
2020-02-06 19:33:02 +00:00
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
#include <LibGUI/Clipboard.h>
|
2019-09-22 18:50:39 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2019-09-17 18:45:38 +00:00
|
|
|
|
|
|
|
struct Options {
|
|
|
|
String data;
|
2020-01-27 17:25:36 +00:00
|
|
|
StringView type { "text" };
|
2019-09-17 18:45:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Options parse_options(int argc, char* argv[])
|
|
|
|
{
|
2020-01-27 17:25:36 +00:00
|
|
|
const char* type = nullptr;
|
|
|
|
Vector<const char*> text;
|
2019-09-17 18:45:38 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2020-01-27 17:25:36 +00:00
|
|
|
args_parser.add_option(type, "Pick a type", "type", 't', "type");
|
2020-02-02 11:34:39 +00:00
|
|
|
args_parser.add_positional_argument(text, "Text to copy", "text", Core::ArgsParser::Required::No);
|
2020-01-27 17:25:36 +00:00
|
|
|
args_parser.parse(argc, argv);
|
2019-09-17 18:45:38 +00:00
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
Options options;
|
|
|
|
options.type = type;
|
2019-09-17 18:45:38 +00:00
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
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,
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::IODevice::OpenMode::ReadOnly,
|
|
|
|
Core::File::ShouldCloseFileDescription::No);
|
2019-09-17 18:45:38 +00:00
|
|
|
ASSERT(success);
|
2019-09-21 18:50:06 +00:00
|
|
|
auto buffer = c_stdin->read_all();
|
2019-09-17 18:45:38 +00:00
|
|
|
dbg() << "Read size " << buffer.size();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2020-02-02 14:07:41 +00:00
|
|
|
GUI::Application app(argc, argv);
|
2019-09-17 18:45:38 +00:00
|
|
|
|
2019-09-23 07:36:25 +00:00
|
|
|
Options options = parse_options(argc, argv);
|
2019-09-17 18:45:38 +00:00
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
auto& clipboard = GUI::Clipboard::the();
|
2019-09-17 18:45:38 +00:00
|
|
|
clipboard.set_data(options.data, options.type);
|
2019-09-23 07:36:25 +00:00
|
|
|
|
|
|
|
return 0;
|
2019-09-17 18:45:38 +00:00
|
|
|
}
|