Text -> Line Context Counter
Step through captions one line at a time, with the neighbors attached
- counter
- current_line_text
- previous_lines_text
- next_lines_text
- combined_context_text
- all_lines_text
This is the fiddliest and most interesting node in the Comfy Text Stats pack. Where Line Batch Chunk pages through a list blindly, this one steps through it one line at a time and hands you the current line plus a configurable window of neighbors - previous lines, next lines, and a combined context block. If you're feeding captions or subtitles to an LLM and want each entry together with some surrounding context, this is the node. Pure Python, part of the same scofano pack, nothing extra to install.
The visible counter
Everything revolves around set_counter, an INT widget with control_after_generate = "increment" - the same seed-like behavior as a seed field. Run the graph and the counter ticks up one, selecting the next line. The counter output shows the current 1-based line number so you can see where you are in the list. And it wraps: set it past the number of lines and it loops back around using the modulo of the line count, so it never runs off the end.
The context window
previous_lines and next_lines (both default to 1) set how many neighbors get pulled in. At the edges you just get less - no error, no padding.
The prefix fields are templates, and they're where the real power hides. The defaults look like Current line: {line} and Previous {count} lines: {lines}, but you can use the placeholders {line}, {lines}, {count}, {actual_count}, {line_number}, and {total_lines} - or leave a prefix blank to get the raw line content with no decoration at all.
The six outputs
counter- the current line number, or 0 if the input is empty.current_line_text- the selected line, templated.previous_lines_textandnext_lines_text- the window on each side, templated; empty when nothing exists on that side or the count is 0.combined_context_text- previous + current + next joined with newlines, skipping empties. This is the one you'll usually wire into an LLM.all_lines_text- the whole input text with theall_lines_prefixapplied.
Installing it
It's in the Comfy Text Stats pack. ComfyUI Manager: Manager → Custom Nodes Manager → search "Comfy Text Stats" → Install, then restart ComfyUI. Or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/scofano/comfy-text-stats
Restart after. No dependencies to pip, no models to download - the pack is stdlib-only, which keeps it out of the dependency-conflict pile that tends to break ComfyUI installs.
Where people get burned
- The increment behavior will confuse you exactly once. If the current line keeps changing between runs with the same inputs, that's the counter auto-advancing, not a bug - set it manually to jump or reset.
- Empty input returns
counter = 0and all-empty strings, which is easy to mistake for a failure. - It wraps rather than clamps.
set_counter = 6on four lines selects line 2. That's deliberate, and it only bites if you assumed out-of-range means "stop."
Inputs (8)
| Name | Type | Default | Description |
|---|---|---|---|
| text | STRING | — | |
| set_counter | INT | 1 | — |
| previous_lines | INT | 1 | — |
| next_lines | INT | 1 | — |
| current_prefix | STRING | Current line: {line} | — |
| previous_prefix | STRING | Previous {count} lines: {lines} | — |
| next_prefix | STRING | Next {count} lines: {lines} | — |
| all_lines_prefix | STRING | All the content: | — |
Outputs (6)
| Name | Type | Description |
|---|---|---|
| counter | INT | — |
| current_line_text | STRING | — |
| previous_lines_text | STRING | — |
| next_lines_text | STRING | — |
| combined_context_text | STRING | — |
| all_lines_text | STRING | — |