SHARE
TWEET

Untitled

a guest Mar 31st, 2015 1 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma comment (lib, "libuv.lib")
  2. #pragma comment (lib, "ws2_32.lib")
  3. #pragma comment (lib, "psapi.lib")
  4. #pragma comment (lib, "Iphlpapi.lib")
  5.  
  6.  
  7. #include <iostream>
  8. //#include <stdio.h>
  9. #include <string.h>
  10. //#include <unistd.h>
  11. //#include <io.h>
  12. #include <uv.h>
  13.  
  14.  
  15.  
  16. #include <regex>
  17.  
  18.  
  19.  
  20.  
  21. std::string stripAnsiEscapeCodes(std::string input) {
  22.         // from: https://github.com/sindresorhus/ansi-regex/blob/47fb974630af70998157b30fad6eb5e5bd7c7cd6/index.js
  23.         std::regex reg("(?:(?:\033\\[)|\233)(?:(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m])|\033[A-M]");
  24.         std::string output = std::regex_replace(input, reg, "");
  25.  
  26.         return output;
  27. }
  28.  
  29. void ccout(std::string message, uv_write_t* req, uv_stream_t* handle) {
  30.         // If the terminal doesn't support ANSI, strip it for clarity
  31.         if (uv_guess_handle(1) != UV_TTY) {
  32.                 message = stripAnsiEscapeCodes(message);
  33.         }
  34.  
  35.         uv_buf_t buf;
  36.         buf.base = (char*)message.c_str();
  37.         buf.len = strlen(buf.base);
  38.         uv_write(req, handle, &buf, 1, NULL);
  39. };
  40.  
  41.  
  42.  
  43.  
  44. uv_loop_t *loop;
  45. uv_tty_t tty;
  46. int main() {
  47.         loop = uv_default_loop();
  48.  
  49.         uv_tty_init(loop, &tty, 1, 0);
  50.         uv_tty_set_mode(&tty, UV_TTY_MODE_NORMAL);
  51.  
  52.         /* * /
  53.         if (uv_guess_handle(1) == UV_TTY) {
  54.                 uv_write_t req;
  55.                 uv_buf_t buf;
  56.                 buf.base = "\033[41;37m";
  57.                 buf.len = strlen(buf.base);
  58.                 uv_write(&req, (uv_stream_t*)&tty, &buf, 1, NULL);
  59.         }
  60.  
  61.         uv_write_t req;
  62.         uv_buf_t buf;
  63.         buf.base = "Hello TTY\n";
  64.         buf.len = strlen(buf.base);
  65.         uv_write(&req, (uv_stream_t*)&tty, &buf, 1, NULL);
  66.         /* */
  67.  
  68.        
  69.         ccout("\033[41;37mHello TTY\n\033[0m", &uv_write_t(), (uv_stream_t*)&tty);
  70.  
  71.         ccout("asdf\n", &uv_write_t(), (uv_stream_t*)&tty);
  72.         ccout("qwer\n", &uv_write_t(), (uv_stream_t*)&tty);
  73.  
  74.         ccout("\033[32mPass\n\033[0m", &uv_write_t(), (uv_stream_t*)&tty);
  75.         ccout("\033[31mFail\n\033[0m", &uv_write_t(), (uv_stream_t*)&tty);
  76.  
  77.  
  78.  
  79.         // To be called when the program exits. Resets TTY settings to default values for the next process to take over.
  80.         uv_tty_reset_mode();
  81.         return uv_run(loop, UV_RUN_DEFAULT);
  82.  
  83.         return 0;
  84. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top