607683bf7c6c7868416ec8dda0f8f3f1b03ccb4a — Drew DeVault 2 months ago 623439c master
Various fixes

Which I am too lazy to split into seaparate commits
2 files changed, 38 insertions(+), 22 deletions(-)

M lpsh.go
M todo
M lpsh.go => lpsh.go +36 -20
@@ 1,6 1,7 @@
 package main
 
 import (
+	"fmt"
 	"os"
 	"os/exec"
 	"time"


@@ 8,36 9,46 @@ import (
 	"git.sr.ht/~sircmpwn/pty"
 )
 
-// TODO: Talk directly to CUPS/the printer?
-func lpr(data []byte) {
-	lpr := exec.Command("/usr/bin/lpr", "-l")
-	pipe, err := lpr.StdinPipe()
+func lptl(data []byte) []byte {
+	out := make([]byte, len(data))
+	for _, b := range data {
+		switch b {
+			case '\b':
+			out = append(out, []byte("^H")...)
+		default:
+			out = append(out, b)
+		}
+	}
+	return out
+}
+
+func lpmgr(in chan (interface{}), out chan ([]byte)) {
+	// TODO: Runtime configurable option? Discover printers? dunno
+	f, err := os.OpenFile("/dev/usb/lp9", os.O_RDWR, 0755)
 	if err != nil {
 		panic(err)
 	}
-	go func() {
-		defer pipe.Close()
-		pipe.Write(data)
-	}()
-	lpr.Run()
-}
 
-func lprmgr(out chan ([]byte)) {
-	lpr([]byte("\n\n\n\r"))
 	feed := false
+	f.Write([]byte("\n\n\n\r"))
 
+	timeout := 250 * time.Millisecond
 	for {
 		select {
+		case <-in:
+			// Increase the timeout after input
+			timeout = 1 * time.Second
 		case data := <-out:
 			if feed {
-				lpr([]byte("\x1Bj\xD8\x1Bj\xD8\x1Bj\x6C"))
+				f.Write([]byte("\x1Bj\xD8\x1Bj\xD8\x1Bj\x6C"))
 				feed = false
 			}
-			lpr(data)
-		case <-time.After(250 * time.Millisecond):
+			f.Write(lptl(data))
+		case <-time.After(timeout):
+			timeout = 200 * time.Millisecond
 			if !feed {
 				feed = true
-				lpr([]byte("\x1BJ\xD8\x1BJ\xD8\x1BJ\x6C"))
+				f.Write([]byte("\x1BJ\xD8\x1BJ\xD8\x1BJ\x6C"))
 			}
 		}
 	}


@@ 49,16 60,20 @@ func main() {
 		Rows: 24,
 	}
 	cmd := exec.Command("/bin/sh")
-	cmd.Env = append(os.Environ(), "TERM=lp")
+	cmd.Env = append(os.Environ(),
+		"TERM=lp",
+		fmt.Sprintf("COLUMNS=%d", 180))
 	tty, err := pty.StartWithSize(cmd, &winsize)
 	if err != nil {
 		panic(err)
 	}
 
+	// TODO: This could be done with tcsetattr if we cared enough
 	exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
 
-	lprch := make(chan ([]byte))
-	go lprmgr(lprch)
+	inch := make(chan (interface{}))
+	outch := make(chan ([]byte))
+	go lpmgr(inch, outch)
 
 	inbuf := make([]byte, 4096)
 	go func() {


@@ 68,6 83,7 @@ func main() {
 				panic(err)
 			}
 			tty.Write(inbuf[:n])
+			inch <- nil
 		}
 	}()
 


@@ 79,6 95,6 @@ func main() {
 		}
 		b := make([]byte, n)
 		copy(b, outbuf[:n])
-		lprch <- b
+		outch <- b
 	}
 }

M todo => todo +2 -2
@@ 1,3 1,3 @@
-- Write directly to printer
 - Rig into libinput and skip stdin from a parent session
-- Make input timings work more effectively
+- print more escapes, such as ^w and term escapes
+- filter out colors