Repository: zyedidia/micro
File: cmd/micro/micro.go (Go)

Code before:

	command := buffer.Command{
		StartCursor:      flagStartPos,
		SearchRegex:      searchText,
		SearchAfterStart: searchIndex > posIndex,
	}

	btype := buffer.BTDefault

	if len(files) > 0 {
		// Option 1
		// We go through each file and load it
		for i := 0; i < len(files); i++ {
			buf, err := buffer.NewBufferFromFileWithCommand(files[i], btype, command)
			if err != nil {
				screen.TermMessage(err)
				continue
			}
			// If the file didn't exist, input will be empty, and we'll open an empty buffer
			buffers = append(buffers, buf)
		}
	} else if !isatty.IsTerminal(os.Stdin.Fd()) {
		// Option 2
		// The input is not a terminal, so something is being piped in
		// and we should read from stdin
		btype = buffer.BTStdout
		input, err = io.ReadAll(os.Stdin)
		if err != nil {
			screen.TermMessage("Error reading from stdin: ", err)
			input = []byte{}
		}
		buffers = append(buffers, buffer.NewBufferFromStringWithCommand(string(input), filename, btype, command))
	} else {
		// Option 3, just open an empty buffer
		buffers = append(buffers, buffer.NewBufferFromStringWithCommand(string(input), filename, btype, command))
	}

	return buffers
}

func checkBackup(name string) error {
	target := filepath.Join(config.ConfigDir, name)
	backup := target + util.BackupSuffix
	if info, err := os.Stat(backup); err == nil {
		input, err := os.ReadFile(backup)
		if err == nil {
			t := info.ModTime()
			msg := fmt.Sprintf(buffer.BackupMsg, target, t.Format("Mon Jan _2 at 15:04, 2006"), backup)
			choice := screen.TermPrompt(msg, []string{"r", "i", "a", "recover", "ignore", "abort"}, true)

			if choice%3 == 0 {

Review comment (refactor):
But setting it here unconditionally to `BTStdout` might not be intended in every use case too, right?
We should set it to `BTStdout` only in case there are no files given AND the stdout is not a terminal, right?
Then this should simplify to:
```diff
diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go
index f3f3be27..8cbe6fbb 100644
--- a/cmd/micro/micro.go
+++ b/cmd/micro/micro.go
@@ -163,11 +163,6 @@ func LoadInput(args []string) []*buffer.Buffer {
 	var err error
 	buffers := make([]*buffer.Buffer, 0, len(args))
 
-	btype := buffer.BTDefault
-	if !isatty.IsTerminal(os.Stdout.Fd()) {
-		btype = buffer.BTStdout
-	}
-
 	files := make([]string, 0, len(args))
 
 	flagStartPos := buffer.Loc{-1, -1}
@@ -218,6 +213,8 @@ func LoadInput(args []string) []*buffer.Buffer {
 		SearchAfterStart: searchIndex > posIndex,
 	}
 
+	btype := buffer.BTDefault
+
 	if len(files) > 0 {
 		// Option 1
 		// We go through each file and load it
@@ -230,18 +227,22 @@ func LoadInput(args []string) []*buffer.Buffer {
 			// If the file didn't exist, input will be empty, and we'll open an empty buffer
 			buffers = append(buffers, buf)
 		}
-	} else if !isatty.IsTerminal(os.Stdin.Fd()) {
-		// Option 2
-		// The input is not a terminal, so something is being piped in
-		// and we should read from stdin
-		input, err = io.ReadAll(os.Stdin)
-		if err != nil {
-			screen.TermMessage("Error reading from stdin: ", err)
-			input = []byte{}
-		}
-		buffers = append(buffers, buffer.NewBufferFromStringWithCommand(string(input), filename, btype, command))
 	} else {
-		// Option 3, just open an empty buffer
+		if !isatty.IsTerminal(os.Stdin.Fd()) {
+			// Option 2
+			// The input is not a terminal, so something is being piped in
+			// and we should read from stdin
+			input, err = io.ReadAll(os.Stdin)
+			if err != nil {
+				screen.TermMessage("Error reading from stdin: ", err)
+				input = []byte{}
+			}
+		}
+		// else Option 3, just open an empty buffer
+		if !isatty.IsTerminal(os.Stdout.Fd()) {
+			// The output is not a terminal, so redirect the buffer to stdout
+			btype = buffer.BTStdout
+		}
 		buffers = append(buffers, buffer.NewBufferFromStringWithCommand(string(input), filename, btype, command))
 	}
 
```

Code after:
		SearchRegex:      searchText,
		SearchAfterStart: searchIndex > posIndex,
	}

	if len(files) > 0 {
		// Option 1
		// We go through each file and load it
		for i := 0; i < len(files); i++ {
			buf, err := buffer.NewBufferFromFileWithCommand(files[i], buffer.BTDefault, command)
			if err != nil {
				screen.TermMessage(err)
				continue
			}
			// If the file didn't exist, input will be empty, and we'll open an empty buffer
			buffers = append(buffers, buf)
		}
	} else {
		btype := buffer.BTDefault
		if !isatty.IsTerminal(os.Stdout.Fd()) {
			btype = buffer.BTStdout
		}

		if !isatty.IsTerminal(os.Stdin.Fd()) {
			// Option 2
			// The input is not a terminal, so something is being piped in
			// and we should read from stdin
			input, err := io.ReadAll(os.Stdin)
			if err != nil {
				screen.TermMessage("Error reading from stdin: ", err)
				input = []byte{}
			}
			buffers = append(buffers, buffer.NewBufferFromStringWithCommand(string(input), "", btype, command))
		} else {
			// Option 3, just open an empty buffer
			buffers = append(buffers, buffer.NewBufferFromStringWithCommand("", "", btype, command))
		}
	}

	return buffers
}

func checkBackup(name string) error {
	target := filepath.Join(config.ConfigDir, name)
	backup := target + util.BackupSuffix
	if info, err := os.Stat(backup); err == nil {
		input, err := os.ReadFile(backup)
		if err == nil {
			t := info.ModTime()
			msg := fmt.Sprintf(buffer.BackupMsg, target, t.Format("Mon Jan _2 at 15:04, 2006"), backup)
			choice := screen.TermPrompt(msg, []string{"r", "i", "a", "recover", "ignore", "abort"}, true)