]> git.lizzy.rs Git - micro.git/commitdiff
If stdout is a pipe, output to the pipe
authorZachary Yedidia <zyedidia@gmail.com>
Thu, 27 Feb 2020 17:39:19 +0000 (12:39 -0500)
committerZachary Yedidia <zyedidia@gmail.com>
Thu, 27 Feb 2020 17:39:19 +0000 (12:39 -0500)
If you run micro as `micro | cat` for example, micro will disallow
you from saving the file, and when you quit the buffer, the contents
will be sent to the pipe. This allows one to use micro as part of
an interactive unix pipeline.

Closes #1524

cmd/micro/micro.go
internal/buffer/buffer.go
internal/util/util.go

index 950e88888ec211b2876d96a39d3ffbba8dc0d053..966f252cb36f96614bd10bef9e53f8098a2b3eeb 100644 (file)
@@ -147,11 +147,16 @@ func LoadInput() []*buffer.Buffer {
        args := flag.Args()
        buffers := make([]*buffer.Buffer, 0, len(args))
 
+       btype := buffer.BTDefault
+       if !isatty.IsTerminal(os.Stdout.Fd()) {
+               btype = buffer.BTStdout
+       }
+
        if len(args) > 0 {
                // Option 1
                // We go through each file and load it
                for i := 0; i < len(args); i++ {
-                       buf, err := buffer.NewBufferFromFile(args[i], buffer.BTDefault)
+                       buf, err := buffer.NewBufferFromFile(args[i], btype)
                        if err != nil {
                                screen.TermMessage(err)
                                continue
@@ -168,17 +173,22 @@ func LoadInput() []*buffer.Buffer {
                        screen.TermMessage("Error reading from stdin: ", err)
                        input = []byte{}
                }
-               buffers = append(buffers, buffer.NewBufferFromString(string(input), filename, buffer.BTDefault))
+               buffers = append(buffers, buffer.NewBufferFromString(string(input), filename, btype))
        } else {
                // Option 3, just open an empty buffer
-               buffers = append(buffers, buffer.NewBufferFromString(string(input), filename, buffer.BTDefault))
+               buffers = append(buffers, buffer.NewBufferFromString(string(input), filename, btype))
        }
 
        return buffers
 }
 
 func main() {
-       defer os.Exit(0)
+       defer func() {
+               if util.Stdout.Len() > 0 {
+                       fmt.Fprint(os.Stdout, util.Stdout.String())
+               }
+               os.Exit(0)
+       }()
 
        // runtime.SetCPUProfileRate(400)
        // f, _ := os.Create("micro.prof")
index f08f532350f38c2710879bceec32399b0226e687..2093e94b49b815e919bb178277c5e021bf22301d 100644 (file)
@@ -5,6 +5,7 @@ import (
        "bytes"
        "crypto/md5"
        "errors"
+       "fmt"
        "io"
        "io/ioutil"
        "os"
@@ -60,6 +61,9 @@ var (
        BTRaw = BufType{4, false, true, false}
        // BTInfo is a buffer for inputting information
        BTInfo = BufType{5, false, true, false}
+       // BTStdout is a buffer that only writes to stdout
+       // when closed
+       BTStdout = BufType{6, false, true, true}
 
        // ErrFileTooLarge is returned when the file is too large to hash
        // (fastdirty is automatically enabled)
@@ -82,6 +86,8 @@ type SharedBuffer struct {
        // Name of the buffer on the status line
        name string
 
+       toStdout bool
+
        // Settings customized by the user
        Settings map[string]interface{}
 
@@ -355,6 +361,10 @@ func (b *Buffer) Fini() {
                b.Serialize()
        }
        b.RemoveBackup()
+
+       if b.Type == BTStdout {
+               fmt.Fprint(util.Stdout, string(b.Bytes()))
+       }
 }
 
 // GetName returns the name that should be displayed in the statusline
index aebe8e17bc754e974af819c0381fff45b3e00755..70f1fd2acd9912ff35344e91169b515b3d037e46 100644 (file)
@@ -1,6 +1,7 @@
 package util
 
 import (
+       "bytes"
        "errors"
        "fmt"
        "os"
@@ -34,6 +35,9 @@ var (
        // FakeCursor is used to disable the terminal cursor and have micro
        // draw its own (enabled for windows consoles where the cursor is slow)
        FakeCursor = false
+
+       // Stdout is a buffer that is written to stdout when micro closes
+       Stdout *bytes.Buffer
 )
 
 func init() {
@@ -46,6 +50,7 @@ func init() {
        if runtime.GOOS == "windows" {
                FakeCursor = true
        }
+       Stdout = new(bytes.Buffer)
 }
 
 // SliceEnd returns a byte slice where the index is a rune index