Skip to content

Commit

Permalink
Merge pull request #798 from hzeller/move-isatty-one-place
Browse files Browse the repository at this point in the history
Provide IsInteractiveTerminalSession().
  • Loading branch information
hzeller authored Apr 26, 2021
2 parents 2acba6d + b11c647 commit 5325293
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
12 changes: 3 additions & 9 deletions common/strings/patch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@

#include "common/strings/patch.h"

#ifndef _WIN32
#include <unistd.h> // for isatty
#else
#include <io.h>
// MSVC recommends to use _isatty...
#define isatty _isatty
#endif

#include <deque>
#include <iostream>
#include <iterator>
Expand Down Expand Up @@ -378,7 +370,9 @@ LineNumberSet FilePatch::AddedLines() const {

static char PromptHunkAction(std::istream& ins, std::ostream& outs) {
// Suppress prompt in noninteractive mode.
if (isatty(0)) outs << "Apply this hunk? [y,n,a,d,s,q,?] ";
if (file::IsInteractiveTerminalSession()) {
outs << "Apply this hunk? [y,n,a,d,s,q,?] ";
}
char c;
ins >> c; // user will need to hit <enter> after the character
if (ins.eof()) {
Expand Down
10 changes: 1 addition & 9 deletions common/tools/patch_tool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef _WIN32
#include <unistd.h> // for isatty
#else
#include <io.h>
// MSVC recommends to use _isatty...
#define isatty _isatty
#endif

#include <functional>
#include <iostream>
#include <string>
Expand Down Expand Up @@ -101,7 +93,7 @@ static absl::Status StdinTest(const SubcommandArgsRange& args,
for (; file_count < kOpenLimit; ++file_count) {
outs << "==== file " << file_count << " ====" << std::endl;
while (ins) {
if (isatty(0)) outs << "enter text: ";
if (verible::file::IsInteractiveTerminalSession()) outs << "enter text: ";
std::getline(ins, line);
outs << "echo: " << line << std::endl;
}
Expand Down
12 changes: 12 additions & 0 deletions common/util/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
#include "absl/strings/string_view.h"
#include "common/util/logging.h"

#ifndef _WIN32
#include <unistd.h> // for isatty
#else
#include <io.h>
// MSVC recommends to use _isatty...
#define isatty _isatty
#endif

/*
* We're in the transition to use std::filesystem for all file operations.
* However some ancient compilers in releasing/ can't handle that yet. Until
Expand Down Expand Up @@ -371,6 +379,10 @@ absl::StatusOr<Directory> ListDir(absl::string_view dir) {
return d;
}

bool IsInteractiveTerminalSession() {
return isatty(0); // Unix: STDIN_FILENO; windows: _fileno( stdin )
}

namespace testing {

std::string RandomFileBasename(absl::string_view prefix) {
Expand Down
4 changes: 4 additions & 0 deletions common/util/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ absl::Status CreateDir(absl::string_view dir);
// TODO (after bump to c++17) rewrite this function to use std::filesystem
absl::StatusOr<Directory> ListDir(absl::string_view dir);

// Returns if this is likely a terminal session (tests if stdin filedescriptor
// is a terminal).
bool IsInteractiveTerminalSession();

namespace testing {

// Generate a random file name (no directory). This does not create any file.
Expand Down

0 comments on commit 5325293

Please sign in to comment.