~emersion/hut-dev

graphql: test STDIN for terminal-ness instead of STDOUT v1 PROPOSED

Jens Schmidt: 1
 graphql: test STDIN for terminal-ness instead of STDOUT

 2 files changed, 11 insertions(+), 1 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/hut-dev/patches/51760/mbox | git am -3
Learn more about email & git

[PATCH] graphql: test STDIN for terminal-ness instead of STDOUT Export this patch

Since graphql needs the terminal-ness to decide whether to read
*input* from STDIN or from $EDITOR it seems more logical to also
test STDIN for terminal-ness.  See also [1].

[1]: https://lists.sr.ht/~sircmpwn/sr.ht-discuss/%3Cb902e7d6-4d10-44e0-ba04-41f755b736ef@vodafonemail.de%3E
---
 graphql.go            |  2 +-
 termfmt/formatting.go | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/graphql.go b/graphql.go
index 3add7ec..bd8f327 100644
--- a/graphql.go
+++ b/graphql.go
@@ -30,7 +30,7 @@ func newGraphqlCommand() *cobra.Command {
		c := createClient(service, cmd)

		// Disable $EDITOR support when not in interactive terminal
		if !termfmt.IsTerminal() {
		if !termfmt.IsStdinTerminal() {
			stdin = true
		}

diff --git a/termfmt/formatting.go b/termfmt/formatting.go
index a46d9bc..a092499 100644
--- a/termfmt/formatting.go
+++ b/termfmt/formatting.go
@@ -11,6 +11,8 @@ import (
)

var isTerminal = term.IsTerminal(int(os.Stdout.Fd()))
var isStdinTerminal = term.IsTerminal(int(os.Stdin.Fd()))
var isStdoutTerminal = term.IsTerminal(int(os.Stdout.Fd()))

type Style string

@@ -116,6 +118,14 @@ func IsTerminal() bool {
	return isTerminal
}

func IsStdinTerminal() bool {
	return isStdinTerminal
}

func IsStdoutTerminal() bool {
	return isStdoutTerminal
}

func Bell() {
	if isTerminal {
		fmt.Print("\a")
-- 
2.39.2
After the initial "I-found-it-I-fixed-it" euphoria I noticed
the following:

- There is option "--stdin" that could be also used to force
  reading from STDIN.

  I'd prefer my solution, since it feels more natural:  If
  STDIN is not on a terminal, there is most likely something
  to read available on it, so just let's use that.

- There are quite a number of other commands that use the same
  logic (check STDOUT to decide whether to use STDIN or
  EDITOR for reading).

  I can extend the patch to these as well.