From c80e5cd97f74ca2971159f636db344e2a9912d25 Mon Sep 17 00:00:00 2001 From: Andy Kluger Date: Thu, 13 Nov 2025 12:50:30 -0500 Subject: [PATCH 1/4] Only set buffer type to stdout when no file args are passed Fixes #2600 --- cmd/micro/micro.go | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index f3f3be27..f4564ede 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} @@ -222,7 +217,7 @@ func LoadInput(args []string) []*buffer.Buffer { // 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) + buf, err := buffer.NewBufferFromFileWithCommand(files[i], buffer.BTDefault, command) if err != nil { screen.TermMessage(err) continue @@ -230,19 +225,26 @@ 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 - buffers = append(buffers, buffer.NewBufferFromStringWithCommand(string(input), filename, btype, command)) + 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), filename, btype, command)) + } else { + // Option 3, just open an empty buffer + buffers = append(buffers, buffer.NewBufferFromStringWithCommand(string(input), filename, btype, command)) + } } return buffers From 4debd29ccf7d195bb86a81977a76de74bb400a94 Mon Sep 17 00:00:00 2001 From: Andy Kluger Date: Mon, 1 Dec 2025 13:26:19 -0500 Subject: [PATCH 2/4] Make the buffer creation when (no file args, terminal stdin) explicitly distinct . . . but functionally equivalent. --- cmd/micro/micro.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index f4564ede..8e5e500a 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -243,7 +243,7 @@ func LoadInput(args []string) []*buffer.Buffer { 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)) + buffers = append(buffers, buffer.NewBufferFromStringWithCommand("", filename, btype, command)) } } From 70d9b643016270f7a5bb5e9f680296c8c1fb7d1c Mon Sep 17 00:00:00 2001 From: Andy Kluger Date: Tue, 2 Dec 2025 14:59:45 -0500 Subject: [PATCH 3/4] LoadInput: reduce variable scope (input, err) Move input and err variable declarations to their usage point. --- cmd/micro/micro.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 8e5e500a..8631fe5d 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -159,8 +159,6 @@ func LoadInput(args []string) []*buffer.Buffer { // should be opened var filename string - var input []byte - var err error buffers := make([]*buffer.Buffer, 0, len(args)) files := make([]string, 0, len(args)) @@ -235,7 +233,7 @@ func LoadInput(args []string) []*buffer.Buffer { // 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) + input, err := io.ReadAll(os.Stdin) if err != nil { screen.TermMessage("Error reading from stdin: ", err) input = []byte{} From 331c43ebb5c16a03f63b0b41188f8c3e30b8d0eb Mon Sep 17 00:00:00 2001 From: Andy Kluger Date: Wed, 3 Dec 2025 15:10:36 -0500 Subject: [PATCH 4/4] Replace never-assigned var filename with literal empty strings --- cmd/micro/micro.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 8631fe5d..bf4d088f 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -158,7 +158,6 @@ func LoadInput(args []string) []*buffer.Buffer { // 3. If there is no input file and the input is a terminal, an empty buffer // should be opened - var filename string buffers := make([]*buffer.Buffer, 0, len(args)) files := make([]string, 0, len(args)) @@ -238,10 +237,10 @@ func LoadInput(args []string) []*buffer.Buffer { screen.TermMessage("Error reading from stdin: ", err) input = []byte{} } - buffers = append(buffers, buffer.NewBufferFromStringWithCommand(string(input), filename, btype, command)) + buffers = append(buffers, buffer.NewBufferFromStringWithCommand(string(input), "", btype, command)) } else { // Option 3, just open an empty buffer - buffers = append(buffers, buffer.NewBufferFromStringWithCommand("", filename, btype, command)) + buffers = append(buffers, buffer.NewBufferFromStringWithCommand("", "", btype, command)) } }