225 lines
4.6 KiB
Go
225 lines
4.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
"github.com/rivo/tview"
|
|
)
|
|
|
|
const (
|
|
USB_SERIAL_PORT = "/dev/ttyUSB0"
|
|
MAIN_WIDTH = 240
|
|
MAIN_HEIGHT = 55
|
|
)
|
|
|
|
var (
|
|
App *tview.Application
|
|
Pages *tview.Pages
|
|
)
|
|
|
|
func main() {
|
|
stat, _ := os.Stdin.Stat()
|
|
|
|
// check if data is being piped from stdin
|
|
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
|
printFromStdIn()
|
|
} else {
|
|
startTui()
|
|
}
|
|
}
|
|
|
|
func writeToPrinter(text string) {
|
|
// move outside
|
|
serial_port, err := os.OpenFile(USB_SERIAL_PORT, os.O_WRONLY, 0755)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Fprintln(serial_port, text)
|
|
}
|
|
|
|
/**
|
|
* If text gets piped from terminal, print it
|
|
*/
|
|
func printFromStdIn() {
|
|
text := ""
|
|
fmt.Scanf("%s", text)
|
|
writeToPrinter(text)
|
|
}
|
|
|
|
func startTui() {
|
|
App = tview.NewApplication()
|
|
|
|
print_page := createPrintPage(MAIN_WIDTH, MAIN_HEIGHT)
|
|
pizza_page := createPizzaPage(MAIN_WIDTH, MAIN_HEIGHT)
|
|
|
|
Pages = tview.NewPages().
|
|
AddPage("print", print_page, true, true).
|
|
AddPage("pizza", pizza_page, true, false)
|
|
|
|
if err := App.SetRoot(Pages, true).Run(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Return a page with a TextView used to print
|
|
*/
|
|
func createPrintPage(width, height int) *tview.Grid {
|
|
page := tview.NewGrid()
|
|
|
|
textview := tview.NewTextView().
|
|
SetText("[green]SCRIVI QUELLO CHE VUOI STAMPARE[white]").
|
|
SetTextAlign(tview.AlignCenter).
|
|
SetDynamicColors(true)
|
|
setCommonBoxAttributes(textview.Box, "Scontrini")
|
|
|
|
textarea := tview.NewTextArea()
|
|
setCommonBoxAttributes(textarea.Box, "")
|
|
|
|
print_button := tview.NewButton("Stampa").
|
|
SetSelectedFunc(func() {
|
|
writeToPrinter(textarea.GetText())
|
|
})
|
|
|
|
pizza_button := tview.NewButton("Pizza").
|
|
SetSelectedFunc(func() {
|
|
Pages.SwitchToPage("pizza")
|
|
})
|
|
|
|
// define keys and behaviour for selected events
|
|
textarea.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
if event.Key() == tcell.KeyTAB {
|
|
App.SetFocus(print_button)
|
|
return nil
|
|
}
|
|
|
|
return event
|
|
})
|
|
|
|
print_button.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
if event.Key() == tcell.KeyTAB {
|
|
App.SetFocus(pizza_button)
|
|
return nil
|
|
}
|
|
|
|
return event
|
|
})
|
|
|
|
pizza_button.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
if event.Key() == tcell.KeyTAB {
|
|
App.SetFocus(textarea)
|
|
return nil
|
|
}
|
|
|
|
return event
|
|
})
|
|
|
|
page.SetColumns(1/5, 1/5, 1/5, 1/5, 1/5).
|
|
SetRows(1/5, 1/5, 1/5, 1/5, 1/5).
|
|
AddItem(textview, 1, 1, 1, 3, 0, 0, false).
|
|
AddItem(textarea, 2, 1, 1, 3, 0, 0, true).
|
|
AddItem(print_button, 3, 1, 1, 1, 0, 0, false).
|
|
AddItem(pizza_button, 3, 3, 1, 1, 0, 0, false)
|
|
|
|
return page
|
|
}
|
|
|
|
/**
|
|
* Return a page with a Table used to print the pizza order
|
|
*/
|
|
func createPizzaPage(width, height int) *tview.Grid {
|
|
pizza_page := tview.NewGrid()
|
|
|
|
textview := tview.NewTextView().
|
|
SetText("[green]STAMPA LE PIZZE[white]").
|
|
SetTextAlign(tview.AlignCenter).
|
|
SetDynamicColors(true)
|
|
setCommonBoxAttributes(textview.Box, "Scontrini")
|
|
|
|
row_index := 0
|
|
|
|
table := tview.NewTable().
|
|
SetBorders(true).
|
|
InsertRow(row_index).
|
|
InsertColumn(0).
|
|
InsertColumn(1)
|
|
setCommonBoxAttributes(table.Box, "")
|
|
|
|
table.SetCellSimple(0, 0, "Quantità")
|
|
table.SetCellSimple(0, 1, "Pizza")
|
|
|
|
print_button := tview.NewButton("Stampa").
|
|
SetSelectedFunc(func() {
|
|
Pages.SwitchToPage("print")
|
|
})
|
|
|
|
pizza_button := tview.NewButton("Pizza").
|
|
SetSelectedFunc(func() {
|
|
printPizza(table)
|
|
})
|
|
|
|
// define keys and behaviour for selected events
|
|
table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
if event.Key() == tcell.KeyTAB {
|
|
App.SetFocus(print_button)
|
|
return nil
|
|
}
|
|
|
|
if event.Key() == tcell.KeyEnter {
|
|
row_index += 1
|
|
table.InsertRow(row_index)
|
|
App.Draw()
|
|
return nil
|
|
}
|
|
|
|
return event
|
|
})
|
|
|
|
print_button.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
if event.Key() == tcell.KeyTAB {
|
|
App.SetFocus(pizza_button)
|
|
return nil
|
|
}
|
|
|
|
return event
|
|
})
|
|
|
|
pizza_button.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
if event.Key() == tcell.KeyTAB {
|
|
App.SetFocus(table)
|
|
return nil
|
|
}
|
|
|
|
return event
|
|
})
|
|
|
|
pizza_page.SetColumns(1/5, 1/5, 1/5, 1/5, 1/5).
|
|
SetRows(1/5, 1/5, 1/5, 1/5, 1/5).
|
|
AddItem(textview, 1, 1, 1, 3, 0, 0, false).
|
|
AddItem(table, 2, 2, 1, 1, 0, 0, true).
|
|
AddItem(print_button, 3, 1, 1, 1, 0, 0, false).
|
|
AddItem(pizza_button, 3, 3, 1, 1, 0, 0, false)
|
|
|
|
return pizza_page
|
|
}
|
|
|
|
/**
|
|
* Format the pizza order table and print it
|
|
*/
|
|
func printPizza(table *tview.Table) {
|
|
// TODO
|
|
}
|
|
|
|
/**
|
|
* Create borders around the Box and add the title
|
|
*/
|
|
func setCommonBoxAttributes(box *tview.Box, title string) {
|
|
box.SetBorder(true).
|
|
SetTitle(strings.ToUpper(title)).
|
|
SetBorderPadding(2, 2, 4, 4)
|
|
}
|