4a0a055234
git-subtree-dir: faxmachine git-subtree-split: d23200bcfdedb9f8cc57e6a3c65b5ef93fcbfd19
38 lines
628 B
Go
38 lines
628 B
Go
package printer
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
type Printer struct {
|
|
devicePath string
|
|
}
|
|
|
|
func New(devicePath string) *Printer {
|
|
return &Printer{
|
|
devicePath: devicePath,
|
|
}
|
|
}
|
|
|
|
func (p *Printer) PrintText(text string) error {
|
|
lp, err := os.OpenFile(p.devicePath, os.O_WRONLY, 0)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open: %v", err)
|
|
}
|
|
defer lp.Close()
|
|
|
|
cmd := exec.Command("./printtext", text)
|
|
cmd.Stdout = lp
|
|
|
|
return cmd.Run()
|
|
}
|
|
|
|
func (p *Printer) PrintImage(data io.Reader, caption string) error {
|
|
cmd := exec.Command("./printimg", p.devicePath, caption)
|
|
cmd.Stdin = data
|
|
|
|
return cmd.Run()
|
|
}
|