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
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")
"bytes"
"crypto/md5"
"errors"
+ "fmt"
"io"
"io/ioutil"
"os"
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)
// Name of the buffer on the status line
name string
+ toStdout bool
+
// Settings customized by the user
Settings map[string]interface{}
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
package util
import (
+ "bytes"
"errors"
"fmt"
"os"
// 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() {
if runtime.GOOS == "windows" {
FakeCursor = true
}
+ Stdout = new(bytes.Buffer)
}
// SliceEnd returns a byte slice where the index is a rune index