Skip to content

Commit 1155ca9

Browse files
committed
Basic and interactive example source files.
1 parent 6b1d26e commit 1155ca9

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

examples/basic_example.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2024 - Nathanne Isip
3+
* This file is part of MyShell.
4+
*
5+
* N8 is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published
7+
* by the Free Software Foundation, either version 3 of the License,
8+
* or (at your option) any later version.
9+
*
10+
* N8 is distributed in the hope that it will be useful, but
11+
* WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with N8. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include <chrono>
20+
#include <iostream>
21+
#include <myshell.hpp>
22+
23+
int main() {
24+
try {
25+
// Create shell instance withcommand
26+
MyShell shell(
27+
#ifdef _WIN32
28+
"dir"
29+
#else
30+
"ls -ral"
31+
#endif
32+
);
33+
34+
// Give some time for output to be collected
35+
std::this_thread::sleep_for(std::chrono::milliseconds(20));
36+
37+
// Read output
38+
std::string output = shell.readShellOutputStream();
39+
std::string error = shell.readShellErrorStream();
40+
41+
// Wait for process completion
42+
while(!shell.hasExited());
43+
44+
// Print results
45+
std::cout << "Exit Code: " << shell.exitCode() << std::endl;
46+
std::cout << "Output:\n" << output << std::endl;
47+
48+
if(!error.empty())
49+
std::cout << "Error:\n" << error << std::endl;
50+
}
51+
catch(const std::exception& e) {
52+
std::cerr << "Error: " << e.what() << std::endl;
53+
}
54+
}

examples/interactive_example.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2024 - Nathanne Isip
3+
* This file is part of MyShell.
4+
*
5+
* N8 is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published
7+
* by the Free Software Foundation, either version 3 of the License,
8+
* or (at your option) any later version.
9+
*
10+
* N8 is distributed in the hope that it will be useful, but
11+
* WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with N8. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include <iostream>
20+
#include <myshell.hpp>
21+
#include <thread>
22+
23+
int main() {
24+
try {
25+
// Create shell instance withcommand
26+
MyShell shell("TERM=dumb vim -u NONE -n test.txt");
27+
28+
shell.writeToShell("i"); // Insert mode
29+
shell.writeToShell("Hi"); // Write the text
30+
shell.writeToShell("\u001b"); // Exit insert mode (ESC key)
31+
shell.writeToShell(":wq"); // Write command to save and quit
32+
shell.writeToShell("\n");
33+
34+
// Wait for process completion
35+
while(!shell.hasExited());
36+
37+
std::cout << "Process exited with code: " << shell.exitCode() << std::endl;
38+
}
39+
catch(const std::exception& e) {
40+
std::cerr << "Error: " << e.what() << std::endl;
41+
}
42+
}

0 commit comments

Comments
 (0)