mirror of
https://github.com/zyedidia/micro.git
synced 2026-02-07 23:50:18 +09:00
Merge pull request #1410 from serge-v/textfilter
Add textfilter command
This commit is contained in:
@@ -58,6 +58,7 @@ func init() {
|
||||
"MemUsage": MemUsage,
|
||||
"Retab": Retab,
|
||||
"Raw": Raw,
|
||||
"TextFilter": TextFilter,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,6 +118,7 @@ func DefaultCommands() map[string]StrCommand {
|
||||
"memusage": {"MemUsage", []Completion{NoCompletion}},
|
||||
"retab": {"Retab", []Completion{NoCompletion}},
|
||||
"raw": {"Raw", []Completion{NoCompletion}},
|
||||
"textfilter": {"TextFilter", []Completion{NoCompletion}},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
35
cmd/micro/textfilter.go
Normal file
35
cmd/micro/textfilter.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// TextFilter command filters the selection through the command.
|
||||
// Selection goes to the command input.
|
||||
// On successfull run command output replaces the current selection.
|
||||
func TextFilter(args []string) {
|
||||
if len(args) == 0 {
|
||||
messenger.Error("usage: textfilter arguments")
|
||||
return
|
||||
}
|
||||
v := CurView()
|
||||
sel := v.Cursor.GetSelection()
|
||||
if sel == "" {
|
||||
v.Cursor.SelectWord()
|
||||
sel = v.Cursor.GetSelection()
|
||||
}
|
||||
var bout, berr bytes.Buffer
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Stdin = strings.NewReader(sel)
|
||||
cmd.Stderr = &berr
|
||||
cmd.Stdout = &bout
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
messenger.Error(err.Error() + " " + berr.String())
|
||||
return
|
||||
}
|
||||
v.Cursor.DeleteSelection()
|
||||
v.Buf.Insert(v.Cursor.Loc, bout.String())
|
||||
}
|
||||
Reference in New Issue
Block a user