xtd 0.2.0
test_forms.cpp

tests forms control

Windows

macOS

Gnome

#include <xtd/diagnostics/process>
#include <xtd/forms/application>
#include <xtd/forms/form>
#include <xtd/forms/text_box>
#include <xtd/environment>
#include <xtd/startup>
class virtual_console : public xtd::forms::text_box {
public:
virtual_console() {
prompt = xtd::string::format("[{}] $ ", xtd::environment::current_directory());
multiline(true);
text(prompt);
select(text().size() - 1, 0);
}
protected:
xtd::drawing::size default_size() const noexcept override {return {300, 200};}
switch (e.key_code()) {
case xtd::forms::keys::down: key_down_pressed(e); break;
case xtd::forms::keys::up: key_up_pressed(e); break;
case xtd::forms::keys::enter: key_enter_pressed(e); break;
default: break;
}
}
private:
xtd::string get_command() const {
auto pos = text().last_index_of(prompt);
if (pos != xtd::string::npos) {
pos += prompt.size();
return text().substring(pos);
}
return "";
}
void key_down_pressed(xtd::forms::key_event_args& e) {
e.handled(true);
}
void key_up_pressed(xtd::forms::key_event_args& e) {
e.handled(true);
}
void key_enter_pressed(xtd::forms::key_event_args& e) {
auto command_line = get_command();
if (!command_line.empty()) {
try {
auto args = command_line.split(' ');
start_info.file_name(args[0]);
if (args.size() > 1)
start_info.arguments(xtd::string::join(" ", xtd::argument_collection(args.begin() + 1, args.end())));
start_info.redirect_standard_error(true);
start_info.redirect_standard_output(true);
if (start_info.file_name() == "cd" && !start_info.arguments().empty()) {
prompt = xtd::string::format("[{}] $ ", xtd::environment::current_directory());
} else {
append_text(xtd::string::format("cd: no such file or directory: {}", start_info.arguments()));
}
} else {
process.start_info(start_info);
process.start();
std::istream& standard_error = process.standard_error();
std::istream& standard_output = process.standard_output();
xtd::io::stream_reader error_reader(standard_error);
while (!error_reader.end_of_stream()) {
append_text(error_reader.read_line());
}
xtd::io::stream_reader output_reader(standard_output);
while (!output_reader.end_of_stream()) {
append_text(output_reader.read_line());
}
}
} catch (...) {
append_text(xtd::string::format("command not found: {}", start_info.file_name()));
}
}
append_text(prompt);
e.handled(true);
}
xtd::string prompt = xtd::string::format("[{}] $ ", xtd::environment::current_directory());
};
using namespace xtd;
using namespace xtd::windows::forms;
class form_main : public form {
public:
static auto main() {
application::run(form_main());
}
form_main() {
text("Virtual console");
client_size({600, 600});
virtual_console.dock(dock_style::fill);
virtual_console.parent(*this);
}
private:
class virtual_console virtual_console;
};
startup_(form_main::main);
xtd::size last_index_of(const basic_string &value) const noexcept
Reports the index of the last occurrence of the specified basic_string in this basic_string.
Definition basic_string.hpp:1476
Specifies a set of values that are used when you start a process.
Definition process_start_info.hpp:39
bool redirect_standard_output() const noexcept
Gets a value that indicates whether the textual output of an application is written to the xtd::diagn...
bool redirect_standard_error() const noexcept
Gets a value that indicates whether the error output of an application is written to the xtd::diagnos...
const xtd::string & arguments() const noexcept
Gets the set of command-line arguments to use when starting the application.
const xtd::string & file_name() const noexcept
Gets the application or document to start.
Provides access to local and remote processes and enables you to start and stop local system processe...
Definition process.hpp:50
Represents an ARGB (alpha, red, green, blue) color.
Definition color.hpp:46
static const xtd::drawing::color lime
Gets a system-defined color that has an ARGB value of 0xFF00FF00. This field is constant.
Definition color.hpp:287
static const xtd::drawing::color black
Gets a system-defined color that has an ARGB value of 0xFF000000. This field is constant.
Definition color.hpp:80
static font_family generic_monospace() noexcept
Gets a generic monospace font_family.
Defines a particular format for text, including font face, size, and style attributes....
Definition font.hpp:45
static xtd::string new_line() noexcept
Gets the newline string defined for this environment.
@ home
The file system directory that contains home folder.
Definition environment.hpp:243
static xtd::string get_folder_path(environment::special_folder folder)
Gets the path to the system special folder that is identified by the specified enumeration.
Definition environment.hpp:613
static xtd::string current_directory()
Gets the fully qualified path of the current working directory.
virtual drawing::color fore_color() const noexcept
Gets the foreground color of the control.
virtual void on_key_down(key_event_args &e)
Raises the xtd::forms::control::key_down event.
virtual drawing::font default_font() const noexcept
Gets the default font of the control.
virtual drawing::font font() const noexcept
Gets the font of the text displayed by the control.
virtual drawing::color back_color() const noexcept
Gets the background color for the control.
virtual drawing::size default_size() const noexcept
Gets the default size of the control.
virtual drawing::size size() const noexcept
Gets the height and width of the control.
Provides data for the xtd::forms::control::key_down or xtd::forms::control::key_up event.
Definition key_event_args.hpp:25
virtual bool multiline() const noexcept
Gets a value indicating whether this is a multiline text box control.
Represents a standard Windows text box.
Definition text_box.hpp:31
drawing::color default_back_color() const noexcept override
Gets the default background color of the control.
drawing::color default_fore_color() const noexcept override
Gets the default foreground color of the control.
void append_text(const xtd::string &value) override
Appends text to the current text of a text box.
const xtd::string & text() const noexcept override
Gets the text associated with this control.
static bool exists(const xtd::string &path)
Determines whether the given path refers to an existing directory on disk.
Implements a xtd::io::text_reader that reads characters from a byte stream.
Definition stream_reader.hpp:28
bool end_of_stream() const
Gets a value that indicates whether the current stream position is at the end of the stream.
virtual xtd::string read_line()
Reads a line of characters from the current stream and returns the data as a string.
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:168
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
xtd::array< xtd::string > argument_collection
Represents the collection of arguments passed to the main entry point method.
Definition argument_collection.hpp:19
@ up
The UP key.
Definition keys.hpp:157
@ down
The DOWN key.
Definition keys.hpp:161
@ select
The SELECT key.
Definition keys.hpp:163
@ enter
The ENTER key.
Definition keys.hpp:101
@ text
The xtd::forms::status_bar_panel displays text in the standard font.
Definition status_bar_panel_style.hpp:25
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition texts.hpp:219
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Stores an ordered pair of integers, which specify a height and width.
Definition size.hpp:32