mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 19:22:45 +00:00 
			
		
		
		
	 ed4c2f2f8e
			
		
	
	
		ed4c2f2f8e
		
	
	
	
	
		
			
			This generally seems like a better name, especially if we somehow also need a better name for "read the entire buffer, but not the entire file" somewhere down the line.
		
			
				
	
	
		
			73 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			73 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2019-2021, Sergey Bugaev <bugaevc@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include <AK/Assertions.h>
 | |
| #include <AK/ByteBuffer.h>
 | |
| #include <AK/DeprecatedString.h>
 | |
| #include <AK/StringBuilder.h>
 | |
| #include <LibCore/ArgsParser.h>
 | |
| #include <LibCore/Stream.h>
 | |
| #include <LibGUI/Application.h>
 | |
| #include <LibGUI/Clipboard.h>
 | |
| #include <LibMain/Main.h>
 | |
| #include <unistd.h>
 | |
| 
 | |
| struct Options {
 | |
|     DeprecatedString data;
 | |
|     StringView type;
 | |
|     bool clear;
 | |
| };
 | |
| 
 | |
| static ErrorOr<Options> parse_options(Main::Arguments arguments)
 | |
| {
 | |
|     auto type = "text/plain"sv;
 | |
|     Vector<DeprecatedString> text;
 | |
|     bool clear = false;
 | |
| 
 | |
|     Core::ArgsParser args_parser;
 | |
|     args_parser.set_general_help("Copy text from stdin or the command-line to the clipboard.");
 | |
|     args_parser.add_option(type, "Pick a type", "type", 't', "type");
 | |
|     args_parser.add_option(clear, "Instead of copying, clear the clipboard", "clear", 'c');
 | |
|     args_parser.add_positional_argument(text, "Text to copy", "text", Core::ArgsParser::Required::No);
 | |
|     args_parser.parse(arguments);
 | |
| 
 | |
|     Options options;
 | |
|     options.type = type;
 | |
|     options.clear = clear;
 | |
| 
 | |
|     if (clear) {
 | |
|         // We're not copying anything.
 | |
|     } else if (text.is_empty()) {
 | |
|         // Copy our stdin.
 | |
|         auto c_stdin = TRY(Core::Stream::File::standard_input());
 | |
|         auto buffer = TRY(c_stdin->read_until_eof());
 | |
|         dbgln("Read size {}", buffer.size());
 | |
|         dbgln("Read data: `{}`", StringView(buffer.bytes()));
 | |
|         options.data = buffer.bytes();
 | |
|     } else {
 | |
|         // Copy the rest of our command-line args.
 | |
|         StringBuilder builder;
 | |
|         builder.join(' ', text);
 | |
|         options.data = builder.to_deprecated_string();
 | |
|     }
 | |
| 
 | |
|     return options;
 | |
| }
 | |
| 
 | |
| ErrorOr<int> serenity_main(Main::Arguments arguments)
 | |
| {
 | |
|     auto app = TRY(GUI::Application::try_create(arguments));
 | |
| 
 | |
|     Options options = TRY(parse_options(arguments));
 | |
| 
 | |
|     auto& clipboard = GUI::Clipboard::the();
 | |
|     if (options.clear)
 | |
|         clipboard.clear();
 | |
|     else
 | |
|         clipboard.set_data(options.data.bytes(), options.type);
 | |
| 
 | |
|     return 0;
 | |
| }
 |