mirror of
https://github.com/MichaelMure/git-bug.git
synced 2025-01-05 17:33:12 +03:00
feat: use isatty to detect a Termios instead
This commit is contained in:
parent
1460377334
commit
a73640150d
@ -6,6 +6,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/mattn/go-isatty"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/vbauerster/mpb/v8"
|
"github.com/vbauerster/mpb/v8"
|
||||||
"github.com/vbauerster/mpb/v8/decor"
|
"github.com/vbauerster/mpb/v8/decor"
|
||||||
@ -20,46 +21,29 @@ const RootCommandName = "git-bug"
|
|||||||
|
|
||||||
const gitBugNamespace = "git-bug"
|
const gitBugNamespace = "git-bug"
|
||||||
|
|
||||||
type IOMode int
|
func getIOMode(io *os.File) bool {
|
||||||
|
return !isatty.IsTerminal(io.Fd()) && !isatty.IsCygwinTerminal(io.Fd())
|
||||||
const (
|
|
||||||
UnknownIOMode IOMode = iota
|
|
||||||
TerminalIOMode
|
|
||||||
PipedOrRedirectedIOMode
|
|
||||||
)
|
|
||||||
|
|
||||||
func getIOMode(io *os.File) IOMode {
|
|
||||||
info, err := io.Stat()
|
|
||||||
if err != nil {
|
|
||||||
panic("only os.StdIn or os.Stdout should be passed to this method")
|
|
||||||
}
|
|
||||||
|
|
||||||
if (info.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
|
|
||||||
return TerminalIOMode
|
|
||||||
}
|
|
||||||
|
|
||||||
return PipedOrRedirectedIOMode
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Env is the environment of a command
|
// Env is the environment of a command
|
||||||
type Env struct {
|
type Env struct {
|
||||||
Repo repository.ClockedRepo
|
Repo repository.ClockedRepo
|
||||||
Backend *cache.RepoCache
|
Backend *cache.RepoCache
|
||||||
In io.Reader
|
In io.Reader
|
||||||
InMode IOMode
|
InRedirection bool
|
||||||
Out Out
|
Out Out
|
||||||
OutMode IOMode
|
OutRedirection bool
|
||||||
Err Out
|
Err Out
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEnv() *Env {
|
func NewEnv() *Env {
|
||||||
return &Env{
|
return &Env{
|
||||||
Repo: nil,
|
Repo: nil,
|
||||||
In: os.Stdin,
|
In: os.Stdin,
|
||||||
InMode: getIOMode(os.Stdin),
|
InRedirection: getIOMode(os.Stdin),
|
||||||
Out: out{Writer: os.Stdout},
|
Out: out{Writer: os.Stdout},
|
||||||
OutMode: getIOMode(os.Stdout),
|
OutRedirection: getIOMode(os.Stdout),
|
||||||
Err: out{Writer: os.Stderr},
|
Err: out{Writer: os.Stderr},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,36 +15,36 @@ func TestGetIOMode(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
in *os.File
|
in *os.File
|
||||||
out *os.File
|
out *os.File
|
||||||
expInMode IOMode
|
expInMode bool
|
||||||
expOutMode IOMode
|
expOutMode bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "neither redirected",
|
name: "neither redirected",
|
||||||
in: os.Stdin,
|
in: os.Stdin,
|
||||||
out: os.Stdout,
|
out: os.Stdout,
|
||||||
expInMode: TerminalIOMode,
|
expInMode: false,
|
||||||
expOutMode: TerminalIOMode,
|
expOutMode: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "in redirected",
|
name: "in redirected",
|
||||||
in: w,
|
in: w,
|
||||||
out: os.Stdout,
|
out: os.Stdout,
|
||||||
expInMode: TerminalIOMode,
|
expInMode: true,
|
||||||
expOutMode: TerminalIOMode,
|
expOutMode: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "out redirected",
|
name: "out redirected",
|
||||||
in: os.Stdin,
|
in: os.Stdin,
|
||||||
out: r,
|
out: r,
|
||||||
expInMode: TerminalIOMode,
|
expInMode: false,
|
||||||
expOutMode: TerminalIOMode,
|
expOutMode: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "both redirected",
|
name: "both redirected",
|
||||||
in: w,
|
in: w,
|
||||||
out: r,
|
out: r,
|
||||||
expInMode: PipedOrRedirectedIOMode,
|
expInMode: true,
|
||||||
expOutMode: PipedOrRedirectedIOMode,
|
expOutMode: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,8 +49,6 @@ func NewTestEnv(t *testing.T) *Env {
|
|||||||
|
|
||||||
repo := repository.CreateGoGitTestRepo(t, false)
|
repo := repository.CreateGoGitTestRepo(t, false)
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
|
|
||||||
backend, err := cache.NewRepoCacheNoEvents(repo)
|
backend, err := cache.NewRepoCacheNoEvents(repo)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
@ -61,7 +59,7 @@ func NewTestEnv(t *testing.T) *Env {
|
|||||||
return &Env{
|
return &Env{
|
||||||
Repo: repo,
|
Repo: repo,
|
||||||
Backend: backend,
|
Backend: backend,
|
||||||
Out: &TestOut{buf},
|
Out: &TestOut{&bytes.Buffer{}},
|
||||||
Err: &TestOut{buf},
|
Err: &TestOut{&bytes.Buffer{}},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user