Fix UI freeze on long URLs and restore ASCII [x] checkboxes.
Bullet hanging-wrap never advanced past overlong tokens, so pasting a YouTube URL into a list hung the preview. Checked boxes print as [x] again (√ looked like CJK on the printer). Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -93,10 +93,10 @@ Wrapped lines use a hanging indent matching the bullet prefix width:
93
93
| Syntax | Printed result |
94
94
|--------|----------------|
95
95
| `[]` or `[ ]` | `[ ]` |
96
| `[x]` / `[X]` / `[x]` | `[x]` (CP437 byte `0xFB`) |
96
| `[x]` / `[X]` | `[x]` |
97
97
| `$[Name]` | `[ ]` or `[x]` from a bool parameter |
98
98
99
ASCII brackets + CP437 `x` stay inline in the native font. `${Name}` and `$[Name]` cannot share a name.
99
ASCII brackets + `x` stay inline in the native CP437 font. `${Name}` and `$[Name]` cannot share a name.
100
100
101
101
Parameters work inside any text, and inside barcode/QR/image directives (below).
102
102
@@ -93,10 +93,10 @@ Wrapped lines use a hanging indent matching the bullet prefix width:93
| Syntax | Printed result |93
| Syntax | Printed result |94
|--------|----------------|94
|--------|----------------|95
| `[]` or `[ ]` | `[ ]` |95
| `[]` or `[ ]` | `[ ]` |96
| `[x]` / `[X]` / `[x]` | `[x]` (CP437 byte `0xFB`) |96
| `[x]` / `[X]` | `[x]` |97
| `$[Name]` | `[ ]` or `[x]` from a bool parameter |97
| `$[Name]` | `[ ]` or `[x]` from a bool parameter |98
98
99
ASCII brackets + CP437 `x` stay inline in the native font. `${Name}` and `$[Name]` cannot share a name.99
ASCII brackets + `x` stay inline in the native CP437 font. `${Name}` and `$[Name]` cannot share a name.100
100
101
Parameters work inside any text, and inside barcode/QR/image directives (below).101
Parameters work inside any text, and inside barcode/QR/image directives (below).102
102
@@ -98,12 +98,12 @@ Long items wrap with a hanging indent:
98
98
99
99
### Checkboxes
100
100
101
Inline checkboxes use ASCII brackets plus a CP437 check mark so they print in the native font:
101
Inline checkboxes use ASCII brackets so they print in the native font:
102
102
103
103
| Syntax | Result |
104
104
|--------|--------|
105
105
| `[]` or `[ ]` | `[ ]` |
106
| `[x]`, `[X]`, or `[x]` | `[x]` (CP437 `x`) |
106
| `[x]` or `[X]` | `[x]` |
107
107
108
108
```markdown
109
109
[] Unchecked
@@ -98,12 +98,12 @@ Long items wrap with a hanging indent:98
98
99
### Checkboxes99
### Checkboxes100
100
101
Inline checkboxes use ASCII brackets plus a CP437 check mark so they print in the native font:101
Inline checkboxes use ASCII brackets so they print in the native font:102
102
103
| Syntax | Result |103
| Syntax | Result |104
|--------|--------|104
|--------|--------|105
| `[]` or `[ ]` | `[ ]` |105
| `[]` or `[ ]` | `[ ]` |106
| `[x]`, `[X]`, or `[x]` | `[x]` (CP437 `x`) |106
| `[x]` or `[X]` | `[x]` |107
107
108
```markdown108
```markdown109
[] Unchecked109
[] Unchecked@@ -29,7 +29,7 @@ Target **Epson ESC/POS** compatible printers (58 mm / 80 mm thermal).
29
29
| Line feed | LF (text lines), ESC d (`@feed` / blank lines) | Text ends with LF; blank/`@feed` use ESC d |
30
30
| Raster flush | LF after GS v 0 | Ensures spacing after images/barcodes/QR rasters |
31
31
| List bullet | `*` top-level, `-` nested | `- item` / ` - nested`; hanging wrap |
32
| Inline checkbox | ASCII `[ ]` / `[x]` (CP437 `0xFB`) | `[]` / `[x]` / `$[Name]` — same font as text |
32
| Inline checkbox | ASCII `[ ]` / `[x]` | `[]` / `[x]` / `$[Name]` — same font as text |
33
33
| Partial cut | GS V 66 0 | `@cut` in template only |
34
34
| Barcode | GS k | Native symbologies; raster via GS v 0 |
35
35
| QR Code | GS ( k | Model 2; or raster GS v 0 |
@@ -29,7 +29,7 @@ Target **Epson ESC/POS** compatible printers (58 mm / 80 mm thermal).29
| Line feed | LF (text lines), ESC d (`@feed` / blank lines) | Text ends with LF; blank/`@feed` use ESC d |29
| Line feed | LF (text lines), ESC d (`@feed` / blank lines) | Text ends with LF; blank/`@feed` use ESC d |30
| Raster flush | LF after GS v 0 | Ensures spacing after images/barcodes/QR rasters |30
| Raster flush | LF after GS v 0 | Ensures spacing after images/barcodes/QR rasters |31
| List bullet | `*` top-level, `-` nested | `- item` / ` - nested`; hanging wrap |31
| List bullet | `*` top-level, `-` nested | `- item` / ` - nested`; hanging wrap |32
| Inline checkbox | ASCII `[ ]` / `[x]` (CP437 `0xFB`) | `[]` / `[x]` / `$[Name]` — same font as text |32
| Inline checkbox | ASCII `[ ]` / `[x]` | `[]` / `[x]` / `$[Name]` — same font as text |33
| Partial cut | GS V 66 0 | `@cut` in template only |33
| Partial cut | GS V 66 0 | `@cut` in template only |34
| Barcode | GS k | Native symbologies; raster via GS v 0 |34
| Barcode | GS k | Native symbologies; raster via GS v 0 |35
| QR Code | GS ( k | Model 2; or raster GS v 0 |35
| QR Code | GS ( k | Model 2; or raster GS v 0 |@@ -475,9 +475,8 @@ impl PosPrintApp {
475
475
.lock_focus(autocomplete_active)
476
476
.show(ui);
477
477
478
if autocomplete_active {
479
output.response.request_focus();
480
}
478
// Do not call request_focus every frame — that fights paste/IME and can freeze the UI.
479
// lock_focus(true) above is enough to keep Tab-completion working.
481
480
482
481
self.editor_cursor = output
483
482
.cursor_range
@@ -475,9 +475,8 @@ impl PosPrintApp {475
.lock_focus(autocomplete_active)
475
.lock_focus(autocomplete_active)
476
.show(ui);
476
.show(ui);
477
477
478
if autocomplete_active {
478
// Do not call request_focus every frame — that fights paste/IME and can freeze the UI.
479
output.response.request_focus();
479
// lock_focus(true) above is enough to keep Tab-completion working.
480
}
481
480
482
self.editor_cursor = output
481
self.editor_cursor = output
483
.cursor_range
482
.cursor_range
@@ -145,16 +145,16 @@ pub enum Inline {
145
145
Param(String),
146
146
/// Boolean checkbox parameter: `$[Name]` → `[ ]` or `[x]` at print time.
147
147
CheckboxParam(String),
148
/// Inline checkbox: `[]` / `[ ]` (empty) or `[x]` / `[X]` / `[x]` (checked).
148
/// Inline checkbox: `[]` / `[ ]` (empty) or `[x]` / `[X]` (checked).
149
149
Checkbox {
150
150
checked: bool,
151
151
},
152
152
}
153
153
154
/// Checkbox glyphs for preview / wrap width. Print maps `x` (U+221A) to CP437 `0xFB`.
154
/// ASCII checkbox glyphs — CP437-safe and inline with the native printer font.
155
155
pub fn checkbox_glyph(checked: bool) -> &'static str {
156
156
if checked {
157
"[\u{221A}]" // x — CP437 byte 0xFB when encoded for the printer
157
"[x]"
158
158
} else {
159
159
"[ ]"
160
160
}
@@ -145,16 +145,16 @@ pub enum Inline {145
Param(String),
145
Param(String),
146
/// Boolean checkbox parameter: `$[Name]` → `[ ]` or `[x]` at print time.
146
/// Boolean checkbox parameter: `$[Name]` → `[ ]` or `[x]` at print time.
147
CheckboxParam(String),
147
CheckboxParam(String),
148
/// Inline checkbox: `[]` / `[ ]` (empty) or `[x]` / `[X]` / `[x]` (checked).
148
/// Inline checkbox: `[]` / `[ ]` (empty) or `[x]` / `[X]` (checked).
149
Checkbox {
149
Checkbox {
150
checked: bool,
150
checked: bool,
151
},
151
},
152
}
152
}
153
153
154
/// Checkbox glyphs for preview / wrap width. Print maps `x` (U+221A) to CP437 `0xFB`.
154
/// ASCII checkbox glyphs — CP437-safe and inline with the native printer font.
155
pub fn checkbox_glyph(checked: bool) -> &'static str {
155
pub fn checkbox_glyph(checked: bool) -> &'static str {
156
if checked {
156
if checked {
157
"[\u{221A}]" // x — CP437 byte 0xFB when encoded for the printer
157
"[x]"
158
} else {
158
} else {
159
"[ ]"
159
"[ ]"
160
}
160
}
@@ -720,7 +720,7 @@ fn try_parse_checkbox(chars: &[char]) -> Option<(bool, usize)> {
720
720
match chars.get(1) {
721
721
Some(']') => Some((false, 2)),
722
722
Some(' ') if chars.get(2) == Some(&']') => Some((false, 3)),
723
Some('x' | 'X' | '\u{221A}') if chars.get(2) == Some(&']') => Some((true, 3)),
723
Some('x' | 'X') if chars.get(2) == Some(&']') => Some((true, 3)),
724
724
_ => None,
725
725
}
726
726
}
@@ -720,7 +720,7 @@ fn try_parse_checkbox(chars: &[char]) -> Option<(bool, usize)> {720
match chars.get(1) {
720
match chars.get(1) {
721
Some(']') => Some((false, 2)),
721
Some(']') => Some((false, 2)),
722
Some(' ') if chars.get(2) == Some(&']') => Some((false, 3)),
722
Some(' ') if chars.get(2) == Some(&']') => Some((false, 3)),
723
Some('x' | 'X' | '\u{221A}') if chars.get(2) == Some(&']') => Some((true, 3)),
723
Some('x' | 'X') if chars.get(2) == Some(&']') => Some((true, 3)),
724
_ => None,
724
_ => None,
725
}
725
}
726
}
726
}
@@ -224,6 +224,9 @@ pub fn floor_char_boundary(s: &str, mut index: usize) -> usize {
224
224
225
225
/// If the cursor is after `@` on the current line, return (start_index, typed_prefix).
226
226
/// `cursor` is a UTF-8 **byte** offset.
227
///
228
/// Only letter prefixes are treated as directive autocomplete (so pasting a URL
229
/// like `https://…` after `@`, or any non-directive junk, does not lock the editor).
227
230
pub fn at_directive_prefix(body: &str, cursor: usize) -> Option<(usize, String)> {
228
231
let cursor = floor_char_boundary(body, cursor.min(body.len()));
229
232
let before = &body[..cursor];
@@ -237,6 +240,13 @@ pub fn at_directive_prefix(body: &str, cursor: usize) -> Option<(usize, String)>
237
240
if fragment.contains(' ') {
238
241
return None;
239
242
}
243
if !fragment.chars().all(|c| c.is_ascii_alphabetic()) {
244
return None;
245
}
246
// Bound prefix length to longest directive name; longer text is not a directive.
247
if fragment.len() > DIRECTIVES.iter().map(|d| d.len()).max().unwrap_or(0) {
248
return None;
249
}
240
250
Some((line_start + at, fragment.to_string()))
241
251
}
242
252
@@ -293,6 +303,18 @@ mod tests {
293
303
assert_eq!(frag, "bar");
294
304
}
295
305
306
#[test]
307
fn url_after_at_is_not_directive_prefix() {
308
let body = "@https://www.youtube.com/watch?v=9gIMZ0WyY88";
309
assert!(at_directive_prefix(body, body.len()).is_none());
310
}
311
312
#[test]
313
fn long_letter_prefix_is_not_directive() {
314
let body = "@notadirectivename";
315
assert!(at_directive_prefix(body, body.len()).is_none());
316
}
317
296
318
#[test]
297
319
fn inserts_snippet() {
298
320
let (text, cur) = insert_at_cursor("ab", 1, "X");
@@ -224,6 +224,9 @@ pub fn floor_char_boundary(s: &str, mut index: usize) -> usize {224
224
225
/// If the cursor is after `@` on the current line, return (start_index, typed_prefix).225
/// If the cursor is after `@` on the current line, return (start_index, typed_prefix).226
/// `cursor` is a UTF-8 **byte** offset.226
/// `cursor` is a UTF-8 **byte** offset.227
///228
/// Only letter prefixes are treated as directive autocomplete (so pasting a URL229
/// like `https://…` after `@`, or any non-directive junk, does not lock the editor).227
pub fn at_directive_prefix(body: &str, cursor: usize) -> Option<(usize, String)> {230
pub fn at_directive_prefix(body: &str, cursor: usize) -> Option<(usize, String)> {228
let cursor = floor_char_boundary(body, cursor.min(body.len()));231
let cursor = floor_char_boundary(body, cursor.min(body.len()));229
let before = &body[..cursor];232
let before = &body[..cursor];@@ -237,6 +240,13 @@ pub fn at_directive_prefix(body: &str, cursor: usize) -> Option<(usize, String)>237
if fragment.contains(' ') {240
if fragment.contains(' ') {238
return None;241
return None;239
}242
}243
if !fragment.chars().all(|c| c.is_ascii_alphabetic()) {244
return None;245
}246
// Bound prefix length to longest directive name; longer text is not a directive.247
if fragment.len() > DIRECTIVES.iter().map(|d| d.len()).max().unwrap_or(0) {248
return None;249
}240
Some((line_start + at, fragment.to_string()))250
Some((line_start + at, fragment.to_string()))241
}251
}242
252
@@ -293,6 +303,18 @@ mod tests {293
assert_eq!(frag, "bar");303
assert_eq!(frag, "bar");294
}304
}295
305
306
#[test]307
fn url_after_at_is_not_directive_prefix() {308
let body = "@https://www.youtube.com/watch?v=9gIMZ0WyY88";309
assert!(at_directive_prefix(body, body.len()).is_none());310
}311
312
#[test]313
fn long_letter_prefix_is_not_directive() {314
let body = "@notadirectivename";315
assert!(at_directive_prefix(body, body.len()).is_none());316
}317
296
#[test]318
#[test]297
fn inserts_snippet() {319
fn inserts_snippet() {298
let (text, cur) = insert_at_cursor("ab", 1, "X");320
let (text, cur) = insert_at_cursor("ab", 1, "X");@@ -178,19 +178,15 @@ impl EscPosEncoder {
178
178
}
179
179
180
180
fn writeln(&mut self, text: &str) {
181
self.push_cp437(text);
182
self.buffer.push(b'\n');
183
}
184
185
/// Map text to CP437 bytes. ASCII passes through; `√` (U+221A) → `0xFB`.
186
fn push_cp437(&mut self, text: &str) {
181
// ESC/POS text path is CP437; keep output to ASCII bytes.
187
182
for ch in text.chars() {
188
match ch {
189
'\u{221A}' => self.buffer.push(0xFB),
190
c if c.is_ascii() => self.buffer.push(c as u8),
191
_ => self.buffer.push(b'?'),
183
if ch.is_ascii() {
184
self.buffer.push(ch as u8);
185
} else {
186
self.buffer.push(b'?');
192
187
}
193
188
}
189
self.buffer.push(b'\n');
194
190
}
195
191
196
192
fn feed(&mut self, lines: u8) {
@@ -420,19 +416,14 @@ mod tests {
420
416
}
421
417
422
418
#[test]
423
fn encodes_inline_checkboxes_as_ascii_with_cp437_check() {
419
fn encodes_inline_checkboxes_as_ascii() {
424
420
let doc = parse("[] hello [x]").unwrap();
425
421
let mut enc = EscPosEncoder::new(PosMetadata::default());
426
422
let bytes = enc.encode_document(&doc).unwrap();
427
// Empty: "[ ] hello " then checked "[√]" as '[' 0xFB ']'
428
assert!(bytes.windows(9).any(|w| w == b"[ ] hello"));
429
assert!(
430
bytes.windows(3).any(|w| w == [b'[', 0xFB, b']']),
431
"checked glyph should use CP437 sqrt byte 0xFB"
432
);
423
let text = String::from_utf8_lossy(bytes);
433
424
assert!(
434
!bytes.windows(3).any(|w| w == b"[x]"),
435
"should not emit ASCII x for checked boxes"
425
text.contains("[ ] hello [x]\n"),
426
"expected ASCII checkbox glyphs, got: {text:?}"
436
427
);
437
428
}
438
429
@@ -178,19 +178,15 @@ impl EscPosEncoder {178
}
178
}
179
179
180
fn writeln(&mut self, text: &str) {
180
fn writeln(&mut self, text: &str) {
181
self.push_cp437(text);
181
// ESC/POS text path is CP437; keep output to ASCII bytes.
182
self.buffer.push(b'\n');
183
}
184
185
/// Map text to CP437 bytes. ASCII passes through; `√` (U+221A) → `0xFB`.
186
fn push_cp437(&mut self, text: &str) {
187
for ch in text.chars() {
182
for ch in text.chars() {
188
match ch {
183
if ch.is_ascii() {
189
'\u{221A}' => self.buffer.push(0xFB),
184
self.buffer.push(ch as u8);
190
c if c.is_ascii() => self.buffer.push(c as u8),
185
} else {
191
_ => self.buffer.push(b'?'),
186
self.buffer.push(b'?');
192
}
187
}
193
}
188
}
189
self.buffer.push(b'\n');
194
}
190
}
195
191
196
fn feed(&mut self, lines: u8) {
192
fn feed(&mut self, lines: u8) {
@@ -420,19 +416,14 @@ mod tests {420
}
416
}
421
417
422
#[test]
418
#[test]
423
fn encodes_inline_checkboxes_as_ascii_with_cp437_check() {
419
fn encodes_inline_checkboxes_as_ascii() {
424
let doc = parse("[] hello [x]").unwrap();
420
let doc = parse("[] hello [x]").unwrap();
425
let mut enc = EscPosEncoder::new(PosMetadata::default());
421
let mut enc = EscPosEncoder::new(PosMetadata::default());
426
let bytes = enc.encode_document(&doc).unwrap();
422
let bytes = enc.encode_document(&doc).unwrap();
427
// Empty: "[ ] hello " then checked "[√]" as '[' 0xFB ']'
423
let text = String::from_utf8_lossy(bytes);
428
assert!(bytes.windows(9).any(|w| w == b"[ ] hello"));
429
assert!(
430
bytes.windows(3).any(|w| w == [b'[', 0xFB, b']']),
431
"checked glyph should use CP437 sqrt byte 0xFB"
432
);
433
assert!(
424
assert!(
434
!bytes.windows(3).any(|w| w == b"[x]"),
425
text.contains("[ ] hello [x]\n"),
435
"should not emit ASCII x for checked boxes"
426
"expected ASCII checkbox glyphs, got: {text:?}"
436
);
427
);
437
}
428
}
438
429
@@ -396,7 +396,7 @@ mod tests {
396
396
&[Inline::CheckboxParam("Paid".into())],
397
397
&values
398
398
),
399
"[\u{221A}]"
399
"[x]"
400
400
);
401
401
}
402
402
@@ -396,7 +396,7 @@ mod tests {396
&[Inline::CheckboxParam("Paid".into())],
396
&[Inline::CheckboxParam("Paid".into())],
397
&values
397
&values
398
),
398
),
399
"[\u{221A}]"
399
"[x]"
400
);
400
);
401
}
401
}
402
402
@@ -91,6 +91,8 @@ fn wrap_content_var_budget(text: &str, first_budget: usize, next_budget: usize)
91
91
current.push_str(word);
92
92
word_idx += 1;
93
93
} else {
94
// Hard-break overlong tokens (URLs, etc.). Must advance word_idx
95
// or this loop never terminates.
94
96
let mut remaining = word;
95
97
while !remaining.is_empty() {
96
98
let chunk = take_char_prefix(remaining, budget);
@@ -106,6 +108,7 @@ fn wrap_content_var_budget(text: &str, first_budget: usize, next_budget: usize)
106
108
.unwrap_or(remaining.len())..];
107
109
budget = next_budget.max(1);
108
110
}
111
word_idx += 1;
109
112
}
110
113
} else {
111
114
let current_len = current.chars().count();
@@ -249,4 +252,29 @@ mod tests {
249
252
let lines = wrap_bullet_item_at_depth("this is a long line", 16, 1);
250
253
assert_eq!(lines, vec![" - this is a", " long line"]);
251
254
}
255
256
#[test]
257
fn hanging_wrap_hard_breaks_url_without_hanging() {
258
let url = "https://www.youtube.com/watch?v=9gIMZ0WyY88";
259
let lines = wrap_bullet_item(url, 32);
260
assert!(lines.len() >= 2, "expected hard-break into multiple lines: {lines:?}");
261
assert!(lines[0].starts_with("* https://"));
262
let joined: String = lines
263
.iter()
264
.map(|l| {
265
l.strip_prefix("* ")
266
.or_else(|| l.strip_prefix(" "))
267
.unwrap_or(l)
268
})
269
.collect();
270
assert_eq!(joined, url);
271
}
272
273
#[test]
274
fn plain_wrap_hard_breaks_url() {
275
let url = "https://www.youtube.com/watch?v=9gIMZ0WyY88";
276
let lines = wrap_text(url, 32);
277
assert!(lines.len() >= 2);
278
assert_eq!(lines.concat(), url);
279
}
252
280
}
@@ -91,6 +91,8 @@ fn wrap_content_var_budget(text: &str, first_budget: usize, next_budget: usize)91
current.push_str(word);
91
current.push_str(word);
92
word_idx += 1;
92
word_idx += 1;
93
} else {
93
} else {
94
// Hard-break overlong tokens (URLs, etc.). Must advance word_idx
95
// or this loop never terminates.
94
let mut remaining = word;
96
let mut remaining = word;
95
while !remaining.is_empty() {
97
while !remaining.is_empty() {
96
let chunk = take_char_prefix(remaining, budget);
98
let chunk = take_char_prefix(remaining, budget);
@@ -106,6 +108,7 @@ fn wrap_content_var_budget(text: &str, first_budget: usize, next_budget: usize)106
.unwrap_or(remaining.len())..];
108
.unwrap_or(remaining.len())..];
107
budget = next_budget.max(1);
109
budget = next_budget.max(1);
108
}
110
}
111
word_idx += 1;
109
}
112
}
110
} else {
113
} else {
111
let current_len = current.chars().count();
114
let current_len = current.chars().count();
@@ -249,4 +252,29 @@ mod tests {249
let lines = wrap_bullet_item_at_depth("this is a long line", 16, 1);
252
let lines = wrap_bullet_item_at_depth("this is a long line", 16, 1);
250
assert_eq!(lines, vec![" - this is a", " long line"]);
253
assert_eq!(lines, vec![" - this is a", " long line"]);
251
}
254
}
255
256
#[test]
257
fn hanging_wrap_hard_breaks_url_without_hanging() {
258
let url = "https://www.youtube.com/watch?v=9gIMZ0WyY88";
259
let lines = wrap_bullet_item(url, 32);
260
assert!(lines.len() >= 2, "expected hard-break into multiple lines: {lines:?}");
261
assert!(lines[0].starts_with("* https://"));
262
let joined: String = lines
263
.iter()
264
.map(|l| {
265
l.strip_prefix("* ")
266
.or_else(|| l.strip_prefix(" "))
267
.unwrap_or(l)
268
})
269
.collect();
270
assert_eq!(joined, url);
271
}
272
273
#[test]
274
fn plain_wrap_hard_breaks_url() {
275
let url = "https://www.youtube.com/watch?v=9gIMZ0WyY88";
276
let lines = wrap_text(url, 32);
277
assert!(lines.len() >= 2);
278
assert_eq!(lines.concat(), url);
279
}
252
}
280
}