Fix tty/console output prompt when piping to a file.
This commit is contained in:
parent
d82d144e43
commit
feedccb427
@ -17,6 +17,7 @@
|
||||
|
||||
#include "textinput/TerminalDisplayUnix.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
// putenv not in cstdlib on Solaris
|
||||
#include <stdlib.h>
|
||||
@ -114,17 +115,33 @@ namespace textinput {
|
||||
if (TERM && strstr(TERM, "256")) {
|
||||
fNColors = 256;
|
||||
}
|
||||
fOutputID = STDOUT_FILENO;
|
||||
if (::isatty(::fileno(stdin)) && !::isatty(fOutputID)) {
|
||||
// Display prompt, even if stdout is going somewhere else
|
||||
fOutputID = ::open("/dev/tty", O_WRONLY);
|
||||
SetIsTTY(true);
|
||||
}
|
||||
}
|
||||
|
||||
static void syncOut(int fd) {
|
||||
::fsync(fd);
|
||||
//if (fd != STDOUT_FILENO)
|
||||
::fflush(stdout);
|
||||
}
|
||||
|
||||
TerminalDisplayUnix::~TerminalDisplayUnix() {
|
||||
Detach();
|
||||
if (fOutputID != STDOUT_FILENO) {
|
||||
syncOut(fOutputID);
|
||||
::close(fOutputID);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
TerminalDisplayUnix::HandleResizeSignal() {
|
||||
#ifdef TIOCGWINSZ
|
||||
struct winsize sz;
|
||||
int ret = ioctl(fileno(stdout), TIOCGWINSZ, (char*)&sz);
|
||||
int ret = ioctl(fOutputID, TIOCGWINSZ, (char*)&sz);
|
||||
if (!ret && sz.ws_col) {
|
||||
SetWidth(sz.ws_col);
|
||||
|
||||
@ -245,7 +262,7 @@ namespace textinput {
|
||||
/// \param[in] len length of the raw string
|
||||
void
|
||||
TerminalDisplayUnix::WriteRawString(const char *text, size_t len) {
|
||||
if (write(fileno(stdout), text, len) == -1) {
|
||||
if (write(fOutputID, text, len) == -1) {
|
||||
// Silence Ubuntu's "unused result". We don't care if it fails.
|
||||
}
|
||||
}
|
||||
@ -264,7 +281,7 @@ namespace textinput {
|
||||
TerminalDisplayUnix::Attach() {
|
||||
// set to noecho
|
||||
if (fIsAttached) return;
|
||||
fflush(stdout);
|
||||
syncOut(fOutputID);
|
||||
TerminalConfigUnix::Get().Attach();
|
||||
fWritePos = Pos();
|
||||
fWriteLen = 0;
|
||||
@ -274,7 +291,7 @@ namespace textinput {
|
||||
void
|
||||
TerminalDisplayUnix::Detach() {
|
||||
if (!fIsAttached) return;
|
||||
fflush(stdout);
|
||||
syncOut(fOutputID);
|
||||
TerminalConfigUnix::Get().Detach();
|
||||
TerminalDisplay::Detach();
|
||||
fIsAttached = false;
|
||||
|
@ -50,6 +50,7 @@ namespace textinput {
|
||||
private:
|
||||
bool fIsAttached; // whether tty is configured
|
||||
size_t fNColors; // number of colors supported by output
|
||||
int fOutputID; // Prompt output file descriptor
|
||||
};
|
||||
}
|
||||
#endif // TEXTINPUT_TERMINALDISPLAYUNIX_H
|
||||
|
Loading…
Reference in New Issue
Block a user