Skip to content

Commit

Permalink
Use table method from esc-pos-encoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsegura committed Mar 5, 2024
1 parent 589ff21 commit ec8fe41
Showing 1 changed file with 10 additions and 32 deletions.
42 changes: 10 additions & 32 deletions src/utils/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@ import i18n from '../i18n'

import { formatPriceWithCode } from './formatting'

function splitter(str, l){
var strs = [];
while (str.length > l){
var pos = str.substring(0, l).lastIndexOf(' ');
pos = pos <= 0 ? l : pos;
strs.push(str.substring(0, pos));
var i = str.indexOf(' ', pos) + 1;
if (i < pos || i > pos + l)
{i = pos;}
str = str.substring(i);
}
strs.push(str);
return strs;
}

const CODEPAGE = 'auto'

export function encodeForPrinter(order) {
Expand Down Expand Up @@ -75,27 +60,20 @@ export function encodeForPrinter(order) {

order.items.forEach((item) => {

let price = ` ${formatPriceWithCode(item.total)}`
let name = diacritics.remove(item.name)

name = `${item.quantity} x ${name}`
const price = formatPriceWithCode(item.total)

let padding = price.length
let maxLength = maxChars - padding
encoder.table(
[
{ width: (maxChars - price.length - 2), align: 'left', marginRight: 2 },
{ width: price.length, align: 'right' }
],
[
[ `${item.quantity} x ${diacritics.remove(item.name)}`, price ],
]
)

encoder.align('left')

let lines = splitter(name, maxLength)

lines.forEach((line, index) => {
if (index === 0) {
line = line.padEnd(maxLength, ' ')
encoder.line(`${line}${price}`)
} else {
encoder.line(line)
}
})

if (item.adjustments.hasOwnProperty('menu_item_modifier')) {
item.adjustments.menu_item_modifier.forEach((adjustment) => {
encoder.line(`- ${adjustment.label}`)
Expand Down

0 comments on commit ec8fe41

Please sign in to comment.