提交 21fafdc6 编写于 作者: J jonathan pickett

issue 111: ANSI escape sequences not rendering on Windows terminal

上级 88606c12
......@@ -110,6 +110,7 @@
#ifdef _WIN32
#include "../../src/win32_Interop/win32fixes.h"
#define REDIS_NOTUSED(V) ((void) V)
#include "../../src/win32_Interop/win32_ANSI.h"
#endif
#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100
......
此差异已折叠。
/*
* Copyright (c), Microsoft Open Technologies, Inc.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <Windows.h>
#ifdef __cplusplus
extern "C" {
#endif
BOOL ParseAndPrintANSIString(HANDLE hDev, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten);
void ANSI_printf(char *format, ...);
// include this file after stdio.h in order to redirect printf to the one that supports ANSI escape sequences
#define printf ANSI_printf
#ifdef __cplusplus
}
#endif
......@@ -27,6 +27,7 @@
#include <sys/stat.h>
#include "Win32_fdapi_crt.h"
#include "Win32_variadicFunctor.h"
#include "Win32_ANSI.h"
#include <string>
using namespace std;
......@@ -516,11 +517,29 @@ ssize_t redis_write_impl(int fd, const void *buf, size_t count) {
} else {
int posixFD = RFDMap::getInstance().lookupPosixFD( fd );
if( posixFD != -1 ) {
int retval = crt_write(posixFD, buf,(unsigned int)count);
if(retval == -1) {
errno = GetLastError();
if (posixFD == fileno(stdout)) {
DWORD bytesWritten = 0;
if (FALSE != ParseAndPrintANSIString(GetStdHandle(STD_OUTPUT_HANDLE), buf, count, &bytesWritten)) {
return (int)bytesWritten;
} else {
errno = GetLastError();
return 0;
}
} else if (posixFD == fileno(stderr)) {
DWORD bytesWritten = 0;
if (FALSE != ParseAndPrintANSIString(GetStdHandle(STD_ERROR_HANDLE), buf, count, &bytesWritten)) {
return (int)bytesWritten;
} else {
errno = GetLastError();
return 0;
}
} else {
int retval = crt_write(posixFD, buf, (unsigned int)count);
if (retval == -1) {
errno = GetLastError();
}
return retval;
}
return retval;
}
else {
errno = EBADF;
......
......@@ -21,6 +21,7 @@
<ItemGroup>
<ClCompile Include="..\RedisLog.c" />
<ClCompile Include="win32fixes.c" />
<ClCompile Include="Win32_ANSI.c" />
<ClCompile Include="Win32_dlmalloc.c" />
<ClCompile Include="Win32_EventLog.cpp" />
<ClCompile Include="Win32_FDAPI.cpp" />
......@@ -35,6 +36,7 @@
<ItemGroup>
<ClInclude Include="..\redisLog.h" />
<ClInclude Include="win32fixes.h" />
<ClInclude Include="Win32_ANSI.h" />
<ClInclude Include="Win32_dlmalloc.h" />
<ClInclude Include="Win32_EventLog.h" />
<ClInclude Include="Win32_FDAPI.h" />
......
......@@ -38,6 +38,7 @@
#include <sys/ioctl.h>
#else
#include "win32_Interop/win32fixes.h"
#include "win32_Interop/win32_ANSI.h"
#endif
#include "config.h"
......
......@@ -51,6 +51,7 @@
#define STDIN_FILENO (_fileno(stdin))
#endif
#include "win32_Interop/win32fixes.h"
#include "win32_Interop/Win32_ANSI.h"
#include <windows.h>
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
......@@ -64,8 +65,6 @@
#ifndef STDIN_FILENO
#define STDIN_FILENO (_fileno(stdin))
#endif
#include "win32_Interop/win32fixes.h"
#include <windows.h>
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#define strtoull _strtoui64
......@@ -237,6 +236,21 @@ static void cliOutputCommandHelp(struct commandHelp *help, int group) {
/* Print generic help. */
static void cliOutputGenericHelp() {
sds version = cliVersion();
#ifdef _WIN32
int i = 0;
int groupslen = sizeof(commandGroups) / sizeof(char*);
printf(
"\x1b[0mredis-cli %s\r\n"
"Type: \"help @<group>\" to get a list of commands in <group>\r\n",
version );
printf(" Where <group> is one of:\n");
for (i = 0; i < groupslen; i++) {
printf(" \x1b[1m%s\x1b[0m\n", commandGroups[i]);
}
printf(
" \"help <command>\" for help on <command>\r\n"
" \"quit\" to exit\r\n" );
#else
printf(
"redis-cli %s\r\n"
"Type: \"help @<group>\" to get a list of commands in <group>\r\n"
......@@ -245,6 +259,7 @@ static void cliOutputGenericHelp() {
" \"quit\" to exit\r\n",
version
);
#endif
sdsfree(version);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册