101 lines
2.0 KiB
Go
101 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"9fans.net/go/acme"
|
|
)
|
|
|
|
var plantumlJar = os.Getenv("HOME") + "/.local/share/plantuml/plantuml.jar"
|
|
|
|
func min(a, b int) int {
|
|
if a < b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|
|
|
|
func main() {
|
|
tmpDir := os.Getenv("TMPDIR")
|
|
if tmpDir == "" {
|
|
tmpDir = "/tmp"
|
|
}
|
|
|
|
var input string
|
|
|
|
// Check if input is from argument (2-1 chord), stdin, or Acme window
|
|
if len(os.Args) > 1 {
|
|
input = strings.Join(os.Args[1:], " ")
|
|
} else if stat, _ := os.Stdin.Stat(); (stat.Mode() & os.ModeCharDevice) == 0 {
|
|
data, err := io.ReadAll(os.Stdin)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
input = string(data)
|
|
} else {
|
|
winid := os.Getenv("winid")
|
|
if winid == "" {
|
|
log.Fatal("no input provided and not running in acme window")
|
|
}
|
|
|
|
win, err := acme.Open(0, nil)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer win.CloseFiles()
|
|
|
|
body, err := win.ReadAll("body")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
input = string(body)
|
|
}
|
|
|
|
if len(input) == 0 {
|
|
log.Fatal("no input received")
|
|
}
|
|
|
|
// Create temp input file
|
|
inputFile, err := os.CreateTemp(tmpDir, "plantuml-*.puml")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer os.Remove(inputFile.Name())
|
|
|
|
if _, err := inputFile.WriteString(input); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
inputFile.Close()
|
|
|
|
// Run plantuml
|
|
cmd := exec.Command("java", "-jar", plantumlJar, "-tpng", inputFile.Name())
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
log.Fatalf("plantuml failed: %v\nOutput: %s", err, output)
|
|
}
|
|
if len(output) > 0 {
|
|
fmt.Fprintf(os.Stderr, "PlantUML output: %s\n", output)
|
|
}
|
|
|
|
// PlantUML creates output with .png extension
|
|
outputFile := strings.TrimSuffix(inputFile.Name(), ".puml") + ".png"
|
|
|
|
// Check if output file was created
|
|
if _, err := os.Stat(outputFile); err != nil {
|
|
log.Fatalf("output file not created: %v", err)
|
|
}
|
|
|
|
// Plumb the image
|
|
if err := exec.Command("plumb", outputFile).Run(); err != nil {
|
|
log.Fatalf("plumb failed: %v", err)
|
|
}
|
|
|
|
fmt.Print(input)
|
|
fmt.Fprintf(os.Stderr, "%s\n", outputFile)
|
|
}
|