Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment (lib, "libuv.lib")
- #pragma comment (lib, "ws2_32.lib")
- #pragma comment (lib, "psapi.lib")
- #pragma comment (lib, "Iphlpapi.lib")
- #include <iostream>
- //#include <stdio.h>
- #include <string.h>
- //#include <unistd.h>
- //#include <io.h>
- #include <uv.h>
- #include <regex>
- std::string stripAnsiEscapeCodes(std::string input) {
- // from: https://github.com/sindresorhus/ansi-regex/blob/47fb974630af70998157b30fad6eb5e5bd7c7cd6/index.js
- std::regex reg("(?:(?:\033\\[)|\233)(?:(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m])|\033[A-M]");
- std::string output = std::regex_replace(input, reg, "");
- return output;
- }
- void ccout(std::string message, uv_write_t* req, uv_stream_t* handle) {
- // If the terminal doesn't support ANSI, strip it for clarity
- if (uv_guess_handle(1) != UV_TTY) {
- message = stripAnsiEscapeCodes(message);
- }
- uv_buf_t buf;
- buf.base = (char*)message.c_str();
- buf.len = strlen(buf.base);
- uv_write(req, handle, &buf, 1, NULL);
- };
- uv_loop_t *loop;
- uv_tty_t tty;
- int main() {
- loop = uv_default_loop();
- uv_tty_init(loop, &tty, 1, 0);
- uv_tty_set_mode(&tty, UV_TTY_MODE_NORMAL);
- /* * /
- if (uv_guess_handle(1) == UV_TTY) {
- uv_write_t req;
- uv_buf_t buf;
- buf.base = "\033[41;37m";
- buf.len = strlen(buf.base);
- uv_write(&req, (uv_stream_t*)&tty, &buf, 1, NULL);
- }
- uv_write_t req;
- uv_buf_t buf;
- buf.base = "Hello TTY\n";
- buf.len = strlen(buf.base);
- uv_write(&req, (uv_stream_t*)&tty, &buf, 1, NULL);
- /* */
- ccout("\033[41;37mHello TTY\n\033[0m", &uv_write_t(), (uv_stream_t*)&tty);
- ccout("asdf\n", &uv_write_t(), (uv_stream_t*)&tty);
- ccout("qwer\n", &uv_write_t(), (uv_stream_t*)&tty);
- ccout("\033[32mPass\n\033[0m", &uv_write_t(), (uv_stream_t*)&tty);
- ccout("\033[31mFail\n\033[0m", &uv_write_t(), (uv_stream_t*)&tty);
- // To be called when the program exits. Resets TTY settings to default values for the next process to take over.
- uv_tty_reset_mode();
- return uv_run(loop, UV_RUN_DEFAULT);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement