BlueGit

timfa posprint

Add Insert menu, directive autocomplete, and CLI print subcommand.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-07-15 · 4 months ago

Hash
8e5ca252c19975f4747eaf29e35e5c280ef2b799
Parent
0657534d
2 files changed +117 −28
src/app.rs +110 −28
@@ -4,9 +4,13 @@ use std::path::PathBuf;
4 4 use eframe::egui;
5 5
6 6 use posprint::dsl::{self, parse};
7 use posprint::escpos::EscPosEncoder;
7 use posprint::editor_assist::{
8 at_directive_prefix, barcode_insert_options, directive_completions, image_insert_options,
9 insert_at_cursor, qr_insert_options,
10 };
11 use posprint::pipeline;
8 12 use posprint::io::{PosFile, PosMetadata, PreviewMode};
9 use posprint::params::{collect_parameter_names, substitute_document};
13 use posprint::params::collect_parameter_names;
10 14 use posprint::preview::{render_preview, PreviewLine, TextStyle};
11 15 use posprint::print::{list_usb_serial_printers, print_bytes, SerialTransport, PrinterDevice};
12 16 use posprint::HELP_MARKDOWN;
@@ -33,6 +37,8 @@ pub struct PosPrintApp {
33 37 printers: Vec<PrinterDevice>,
34 38 selected_printer: Option<usize>,
35 39 print_error: Option<String>,
40 editor_cursor: usize,
41 autocomplete: Vec<&'static str>,
36 42 }
37 43
38 44 impl Default for PosPrintApp {
@@ -52,6 +58,8 @@ impl Default for PosPrintApp {
52 58 printers: Vec::new(),
53 59 selected_printer: None,
54 60 print_error: None,
61 editor_cursor: 0,
62 autocomplete: Vec::new(),
55 63 }
56 64 }
57 65 }
@@ -135,13 +143,10 @@ impl PosPrintApp {
135 143
136 144 fn execute_print(&mut self) {
137 145 self.print_error = None;
138 let doc = match parse(&self.body) {
139 Ok(d) => d,
140 Err(e) => {
141 self.print_error = Some(e.to_string());
142 return;
143 }
144 };
146 if parse(&self.body).is_err() {
147 self.print_error = Some("Template has parse errors".into());
148 return;
149 }
145 150
146 151 for name in &self.print_param_names {
147 152 if !self.print_param_inputs.contains_key(name) {
@@ -150,19 +155,14 @@ impl PosPrintApp {
150 155 }
151 156 }
152 157
153 let sub_doc = substitute_document(&doc, &self.print_param_inputs);
154 158 self.last_params = self.print_param_inputs.clone();
155 159
156 let base_dir = self.file_path.as_ref().and_then(|p| p.parent()).map(PathBuf::from);
157 let mut encoder = EscPosEncoder::new(self.metadata.clone());
158 if let Some(dir) = base_dir {
159 encoder = encoder.with_base_dir(dir);
160 }
161
162 let bytes = match encoder.encode_document(&sub_doc) {
163 Ok(b) => b.to_vec(),
160 let base_dir = self.file_path.as_ref().and_then(|p| p.parent());
161 let file = self.pos_file();
162 let bytes = match pipeline::encode_pos_file(&file, base_dir, &self.last_params) {
163 Ok(b) => b,
164 164 Err(e) => {
165 self.print_error = Some(format!("{e:?}"));
165 self.print_error = Some(e.to_string());
166 166 return;
167 167 }
168 168 };
@@ -239,6 +239,41 @@ impl eframe::App for PosPrintApp {
239 239 if ui.button("Print").clicked() {
240 240 self.open_print_dialog();
241 241 }
242
243 ui.menu_button("Insert", |ui| {
244 for opt in image_insert_options() {
245 if ui.button(format!("Image — {}", opt.label)).clicked() {
246 self.insert_snippet(&opt.snippet);
247 ui.close_menu();
248 }
249 }
250 ui.separator();
251 ui.menu_button("Barcode", |ui| {
252 for opt in barcode_insert_options() {
253 if ui.button(&opt.label).clicked() {
254 self.insert_snippet(&opt.snippet);
255 ui.close_menu();
256 }
257 }
258 });
259 ui.menu_button("QR code", |ui| {
260 for opt in qr_insert_options() {
261 if ui.button(&opt.label).clicked() {
262 self.insert_snippet(&opt.snippet);
263 ui.close_menu();
264 }
265 }
266 });
267 ui.separator();
268 if ui.button("@feed").clicked() {
269 self.insert_snippet("@feed 3\n");
270 ui.close_menu();
271 }
272 if ui.button("@cut").clicked() {
273 self.insert_snippet("@cut\n");
274 ui.close_menu();
275 }
276 });
242 277 });
243 278
244 279 ui.separator();
@@ -316,18 +351,65 @@ impl eframe::App for PosPrintApp {
316 351 }
317 352
318 353 impl PosPrintApp {
354 fn insert_snippet(&mut self, snippet: &str) {
355 let (body, cursor) = insert_at_cursor(&self.body, self.editor_cursor, snippet);
356 self.body = body;
357 self.editor_cursor = cursor;
358 self.dirty = true;
359 self.tab = Tab::Editor;
360 }
361
319 362 fn show_editor(&mut self, ui: &mut egui::Ui) {
320 let changed = ui
321 .add(
322 egui::TextEdit::multiline(&mut self.body)
323 .desired_width(f32::INFINITY)
324 .desired_rows(30)
325 .font(egui::TextStyle::Monospace),
326 )
327 .changed();
328 if changed {
363 let output = egui::TextEdit::multiline(&mut self.body)
364 .id_salt("main_editor")
365 .desired_width(f32::INFINITY)
366 .desired_rows(30)
367 .font(egui::TextStyle::Monospace)
368 .show(ui);
369
370 self.editor_cursor = output
371 .state
372 .cursor
373 .char_range()
374 .map(|r| r.primary.index)
375 .unwrap_or(self.body.len());
376
377 if output.response.changed() {
329 378 self.dirty = true;
330 379 }
380
381 if let Some((_start, prefix)) = at_directive_prefix(&self.body, self.editor_cursor) {
382 self.autocomplete = directive_completions(&prefix);
383 if !self.autocomplete.is_empty() {
384 egui::Area::new(egui::Id::new("directive_ac"))
385 .order(egui::Order::Foreground)
386 .show(ui.ctx(), |ui| {
387 ui.set_min_width(120.0);
388 ui.label("Directive autocomplete:");
389 for dir in &self.autocomplete {
390 if ui.button(format!("@{dir}")).clicked() {
391 let line_start = self.body[..self.editor_cursor]
392 .rfind('\n')
393 .map(|i| i + 1)
394 .unwrap_or(0);
395 let line_end = self.body[self.editor_cursor..]
396 .find('\n')
397 .map(|i| self.editor_cursor + i)
398 .unwrap_or(self.body.len());
399 let mut line = self.body[line_start..line_end].to_string();
400 if let Some(at) = line.find('@') {
401 line.replace_range(at + 1.., dir);
402 self.body.replace_range(line_start..line_end, &line);
403 self.editor_cursor = line_start + at + 1 + dir.len();
404 self.dirty = true;
405 }
406 }
407 }
408 });
409 }
410 } else {
411 self.autocomplete.clear();
412 }
331 413 }
332 414
333 415 fn show_preview(&mut self, ui: &mut egui::Ui) {
@@ -4,9 +4,13 @@ use std::path::PathBuf;
4use eframe::egui;
4use eframe::egui;
5
5
6use posprint::dsl::{self, parse};
6use posprint::dsl::{self, parse};
7use posprint::escpos::EscPosEncoder;
7use posprint::editor_assist::{
8 at_directive_prefix, barcode_insert_options, directive_completions, image_insert_options,
9 insert_at_cursor, qr_insert_options,
10};
11use posprint::pipeline;
8use posprint::io::{PosFile, PosMetadata, PreviewMode};
12use posprint::io::{PosFile, PosMetadata, PreviewMode};
9use posprint::params::{collect_parameter_names, substitute_document};
13use posprint::params::collect_parameter_names;
10use posprint::preview::{render_preview, PreviewLine, TextStyle};
14use posprint::preview::{render_preview, PreviewLine, TextStyle};
11use posprint::print::{list_usb_serial_printers, print_bytes, SerialTransport, PrinterDevice};
15use posprint::print::{list_usb_serial_printers, print_bytes, SerialTransport, PrinterDevice};
12use posprint::HELP_MARKDOWN;
16use posprint::HELP_MARKDOWN;
@@ -33,6 +37,8 @@ pub struct PosPrintApp {
33 printers: Vec<PrinterDevice>,
37 printers: Vec<PrinterDevice>,
34 selected_printer: Option<usize>,
38 selected_printer: Option<usize>,
35 print_error: Option<String>,
39 print_error: Option<String>,
40 editor_cursor: usize,
41 autocomplete: Vec<&'static str>,
36}
42}
37
43
38impl Default for PosPrintApp {
44impl Default for PosPrintApp {
@@ -52,6 +58,8 @@ impl Default for PosPrintApp {
52 printers: Vec::new(),
58 printers: Vec::new(),
53 selected_printer: None,
59 selected_printer: None,
54 print_error: None,
60 print_error: None,
61 editor_cursor: 0,
62 autocomplete: Vec::new(),
55 }
63 }
56 }
64 }
57}
65}
@@ -135,13 +143,10 @@ impl PosPrintApp {
135
143
136 fn execute_print(&mut self) {
144 fn execute_print(&mut self) {
137 self.print_error = None;
145 self.print_error = None;
138 let doc = match parse(&self.body) {
146 if parse(&self.body).is_err() {
139 Ok(d) => d,
147 self.print_error = Some("Template has parse errors".into());
140 Err(e) => {
148 return;
141 self.print_error = Some(e.to_string());
149 }
142 return;
143 }
144 };
145
150
146 for name in &self.print_param_names {
151 for name in &self.print_param_names {
147 if !self.print_param_inputs.contains_key(name) {
152 if !self.print_param_inputs.contains_key(name) {
@@ -150,19 +155,14 @@ impl PosPrintApp {
150 }
155 }
151 }
156 }
152
157
153 let sub_doc = substitute_document(&doc, &self.print_param_inputs);
154 self.last_params = self.print_param_inputs.clone();
158 self.last_params = self.print_param_inputs.clone();
155
159
156 let base_dir = self.file_path.as_ref().and_then(|p| p.parent()).map(PathBuf::from);
160 let base_dir = self.file_path.as_ref().and_then(|p| p.parent());
157 let mut encoder = EscPosEncoder::new(self.metadata.clone());
161 let file = self.pos_file();
158 if let Some(dir) = base_dir {
162 let bytes = match pipeline::encode_pos_file(&file, base_dir, &self.last_params) {
159 encoder = encoder.with_base_dir(dir);
163 Ok(b) => b,
160 }
161
162 let bytes = match encoder.encode_document(&sub_doc) {
163 Ok(b) => b.to_vec(),
164 Err(e) => {
164 Err(e) => {
165 self.print_error = Some(format!("{e:?}"));
165 self.print_error = Some(e.to_string());
166 return;
166 return;
167 }
167 }
168 };
168 };
@@ -239,6 +239,41 @@ impl eframe::App for PosPrintApp {
239 if ui.button("Print").clicked() {
239 if ui.button("Print").clicked() {
240 self.open_print_dialog();
240 self.open_print_dialog();
241 }
241 }
242
243 ui.menu_button("Insert", |ui| {
244 for opt in image_insert_options() {
245 if ui.button(format!("Image — {}", opt.label)).clicked() {
246 self.insert_snippet(&opt.snippet);
247 ui.close_menu();
248 }
249 }
250 ui.separator();
251 ui.menu_button("Barcode", |ui| {
252 for opt in barcode_insert_options() {
253 if ui.button(&opt.label).clicked() {
254 self.insert_snippet(&opt.snippet);
255 ui.close_menu();
256 }
257 }
258 });
259 ui.menu_button("QR code", |ui| {
260 for opt in qr_insert_options() {
261 if ui.button(&opt.label).clicked() {
262 self.insert_snippet(&opt.snippet);
263 ui.close_menu();
264 }
265 }
266 });
267 ui.separator();
268 if ui.button("@feed").clicked() {
269 self.insert_snippet("@feed 3\n");
270 ui.close_menu();
271 }
272 if ui.button("@cut").clicked() {
273 self.insert_snippet("@cut\n");
274 ui.close_menu();
275 }
276 });
242 });
277 });
243
278
244 ui.separator();
279 ui.separator();
@@ -316,18 +351,65 @@ impl eframe::App for PosPrintApp {
316}
351}
317
352
318impl PosPrintApp {
353impl PosPrintApp {
354 fn insert_snippet(&mut self, snippet: &str) {
355 let (body, cursor) = insert_at_cursor(&self.body, self.editor_cursor, snippet);
356 self.body = body;
357 self.editor_cursor = cursor;
358 self.dirty = true;
359 self.tab = Tab::Editor;
360 }
361
319 fn show_editor(&mut self, ui: &mut egui::Ui) {
362 fn show_editor(&mut self, ui: &mut egui::Ui) {
320 let changed = ui
363 let output = egui::TextEdit::multiline(&mut self.body)
321 .add(
364 .id_salt("main_editor")
322 egui::TextEdit::multiline(&mut self.body)
365 .desired_width(f32::INFINITY)
323 .desired_width(f32::INFINITY)
366 .desired_rows(30)
324 .desired_rows(30)
367 .font(egui::TextStyle::Monospace)
325 .font(egui::TextStyle::Monospace),
368 .show(ui);
326 )
369
327 .changed();
370 self.editor_cursor = output
328 if changed {
371 .state
372 .cursor
373 .char_range()
374 .map(|r| r.primary.index)
375 .unwrap_or(self.body.len());
376
377 if output.response.changed() {
329 self.dirty = true;
378 self.dirty = true;
330 }
379 }
380
381 if let Some((_start, prefix)) = at_directive_prefix(&self.body, self.editor_cursor) {
382 self.autocomplete = directive_completions(&prefix);
383 if !self.autocomplete.is_empty() {
384 egui::Area::new(egui::Id::new("directive_ac"))
385 .order(egui::Order::Foreground)
386 .show(ui.ctx(), |ui| {
387 ui.set_min_width(120.0);
388 ui.label("Directive autocomplete:");
389 for dir in &self.autocomplete {
390 if ui.button(format!("@{dir}")).clicked() {
391 let line_start = self.body[..self.editor_cursor]
392 .rfind('\n')
393 .map(|i| i + 1)
394 .unwrap_or(0);
395 let line_end = self.body[self.editor_cursor..]
396 .find('\n')
397 .map(|i| self.editor_cursor + i)
398 .unwrap_or(self.body.len());
399 let mut line = self.body[line_start..line_end].to_string();
400 if let Some(at) = line.find('@') {
401 line.replace_range(at + 1.., dir);
402 self.body.replace_range(line_start..line_end, &line);
403 self.editor_cursor = line_start + at + 1 + dir.len();
404 self.dirty = true;
405 }
406 }
407 }
408 });
409 }
410 } else {
411 self.autocomplete.clear();
412 }
331 }
413 }
332
414
333 fn show_preview(&mut self, ui: &mut egui::Ui) {
415 fn show_preview(&mut self, ui: &mut egui::Ui) {
src/main.rs +7 −0
@@ -1,6 +1,13 @@
1 1 mod app;
2 2
3 use clap::Parser;
4
3 5 fn main() -> eframe::Result<()> {
6 let cli = posprint::cli::Cli::parse();
7 if cli.command.is_some() {
8 std::process::exit(posprint::cli::run(cli));
9 }
10
4 11 let options = eframe::NativeOptions {
5 12 viewport: eframe::egui::ViewportBuilder::default().with_inner_size([1100.0, 750.0]),
6 13 ..Default::default()
@@ -1,6 +1,13 @@
1mod app;
1mod app;
2
2
3use clap::Parser;
4
3fn main() -> eframe::Result<()> {
5fn main() -> eframe::Result<()> {
6 let cli = posprint::cli::Cli::parse();
7 if cli.command.is_some() {
8 std::process::exit(posprint::cli::run(cli));
9 }
10
4 let options = eframe::NativeOptions {
11 let options = eframe::NativeOptions {
5 viewport: eframe::egui::ViewportBuilder::default().with_inner_size([1100.0, 750.0]),
12 viewport: eframe::egui::ViewportBuilder::default().with_inner_size([1100.0, 750.0]),
6 ..Default::default()
13 ..Default::default()