Scontrini-TUI-Client/main.go

378 lines
8.2 KiB
Go

package main
import (
"fmt"
"os"
"strconv"
"strings"
"Scontrini-TUI-Client/api"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
const (
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()
}
}
/**
* If text gets piped from terminal, send it to the API
*/
func printFromStdIn() {
text := ""
fmt.Scanf("%s", &text)
_, err := api.SendPrintCommand(nil, text)
if err != nil {
os.Exit(1)
}
}
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 text
*/
func createPrintPage(width, height int) *tview.Grid {
print_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() {
_, err := api.SendPrintCommand(nil, textarea.GetText())
if err != nil {
os.Exit(1) // TODO: add error page
}
})
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
})
print_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 print_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")
// get a list of all the pizzas
socket, err := api.SendGetPizzasCommand(nil)
if err != nil {
os.Exit(1) // TODO: error page
}
_, pizzas_json, err := api.ReceivePizzasCommand(socket)
row_index := 0
table := tview.NewTable().
InsertRow(row_index).
InsertColumn(0).
InsertColumn(1).
InsertColumn(2)
table.SetCellSimple(0, 0, "Pizza")
table.SetCellSimple(0, 1, "Prezzo")
table.SetCellSimple(0, 2, "Quantità")
// make the rows selectable
table.SetSelectable(true, false)
// populate the table
for _, v := range pizzas_json.Pizzas {
row_index += 1
table.InsertRow(row_index)
table.SetCellSimple(row_index, 0, v.Name)
table.SetCellSimple(row_index, 1, strconv.Itoa(v.Price))
table.SetCellSimple(row_index, 2, "0")
}
setCommonBoxAttributes(table.Box, "")
print_button := tview.NewButton("Stampa").
SetSelectedFunc(func() {
Pages.SwitchToPage("print")
})
pizza_button := tview.NewButton("Pizza").
SetSelectedFunc(func() {
sendOrder(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
}
// increase/decrease the quantity column of the selected row
if event.Rune() == '+' || event.Rune() == '-' {
selected_row, _ := table.GetSelection()
if selected_row == 0 {
return event
}
current_quantity, err := strconv.Atoi(table.GetCell(selected_row, 2).Text)
if err != nil {
os.Exit(1)
}
if event.Rune() == '+' {
table.SetCellSimple(selected_row, 2, strconv.Itoa(current_quantity+1))
} else {
if current_quantity > 0 {
table.SetCellSimple(selected_row, 2, strconv.Itoa(current_quantity-1))
}
}
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
}
/**
* Return a page with a Table used to show the pizza order
*/
func createOrderPage(width, height int, pizzas []api.Pizza, price int) *tview.Grid {
order_page := tview.NewGrid()
textview := tview.NewTextView().
SetText("[green]IL TUO ORDINE\n\nPrezzo Totale: [red]" + strconv.Itoa(price) + "€[white]").
SetTextAlign(tview.AlignCenter).
SetDynamicColors(true)
setCommonBoxAttributes(textview.Box, "Scontrini")
row_index := 0
table := tview.NewTable().
InsertRow(row_index).
InsertColumn(0).
InsertColumn(1)
table.SetCellSimple(0, 0, "Pizza")
table.SetCellSimple(0, 1, "Quantità")
// populate the table
for _, v := range pizzas {
row_index += 1
table.InsertRow(row_index)
table.SetCellSimple(row_index, 0, v.Name)
table.SetCellSimple(row_index, 1, strconv.Itoa(v.Quantity))
}
setCommonBoxAttributes(table.Box, "")
print_button := tview.NewButton("Stampa").
SetSelectedFunc(func() {
Pages.RemovePage("order")
Pages.SwitchToPage("print")
})
pizza_button := tview.NewButton("Pizza").
SetSelectedFunc(func() {
Pages.RemovePage("order")
Pages.SwitchToPage("pizza")
})
// 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
}
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
})
order_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 order_page
}
/**
* Format the pizza order from the table and send it to the API
*/
func sendOrder(table *tview.Table) {
pizzas := make([]api.Pizza, 0)
// populate the slice with pizzas from the table
for i := 1; i < table.GetRowCount(); i++ {
name := table.GetCell(i, 0).Text
quantity, err := strconv.Atoi(table.GetCell(i, 2).Text)
if err != nil {
os.Exit(1)
}
if quantity > 0 {
pizzas = append(pizzas, api.Pizza{name, 0, quantity})
}
}
socket, err := api.SendPizzaCommand(nil, pizzas)
if err != nil {
os.Exit(1) // TODO: add error page
}
_, price, err := api.ReceivePriceCommand(socket)
if err != nil {
os.Exit(1) // TODO: add error page
}
order_page := createOrderPage(MAIN_WIDTH, MAIN_HEIGHT, pizzas, price.TotalPrice)
Pages.AddAndSwitchToPage("order", order_page, true)
}
/**
* 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)
}