Skip to content

Commit

Permalink
support ASCII escape code in Windows terminals
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Sep 13, 2023
1 parent 5938319 commit 0722c09
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "src/pybind11"]
path = src/pybind11
url = https:/pybind/pybind11
url = https:/pybind/pybind11.git
28 changes: 28 additions & 0 deletions src/mcx_tictoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,31 @@ void sleep_ms(int milliseconds) {
usleep(milliseconds * 1000);
#endif
}


#if defined(_WIN32) && defined(USE_OS_TIMER)

int EnableVTMode() {
// Set output mode to handle virtual terminal sequences
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

if (hOut == INVALID_HANDLE_VALUE) {
return 0;
}

DWORD dwMode = 0;

if (!GetConsoleMode(hOut, &dwMode)) {
return 0;
}

dwMode |= ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING;

if (!SetConsoleMode(hOut, dwMode)) {
return 0;
}

return 1;
}

#endif
4 changes: 4 additions & 0 deletions src/mcx_tictoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ unsigned int StartTimer ();
unsigned int GetTimeMillis ();
void sleep_ms(int milliseconds);

#if defined(_WIN32) && defined(USE_OS_TIMER)
int EnableVTMode();
#endif

#ifdef __cplusplus
}
#endif
Expand Down
9 changes: 7 additions & 2 deletions src/mcx_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "mcx_shapes.h"
#include "mcx_const.h"
#include "mcx_bench.h"
#include "mcx_tictoc.h"

#ifndef MCX_CONTAINER
#include "zmat/zmatlib.h"
Expand Down Expand Up @@ -2919,7 +2920,7 @@ void mcx_savejdata(char* filename, Config* cfg) {
void mcx_loadvolume(char* filename, Config* cfg, int isbuf) {
unsigned int i, datalen, res;
unsigned char* inputvol = NULL;
FILE* fp;
FILE* fp = NULL;

if (!isbuf) {
if (strstr(filename, ".json") != NULL) {
Expand Down Expand Up @@ -3933,6 +3934,10 @@ void mcx_parsecmd(int argc, char* argv[], Config* cfg) {
char logfile[MAX_PATH_LENGTH] = {0};
float np = 0.f;

#if defined(_WIN32) && defined(USE_OS_TIMER)
EnableVTMode();
#endif

if (argc <= 1) {
mcx_usage(cfg, argv[0]);
exit(0);
Expand Down Expand Up @@ -4309,7 +4314,7 @@ void mcx_parsecmd(int argc, char* argv[], Config* cfg) {
} else {
MCX_FPRINTF(cfg->flog, "Built-in benchmarks:\n");

for (int i = 0; i < sizeof(benchname) / sizeof(char*) -1; i++) {
for (int i = 0; i < sizeof(benchname) / sizeof(char*) - 1; i++) {
MCX_FPRINTF(cfg->flog, "\t%s\n", benchname[i]);
}

Expand Down

0 comments on commit 0722c09

Please sign in to comment.