Nodes/Comfy Text Stats/Text -> Line Context Counter
ComfyUI Node

Text -> Line Context Counter

Step through captions one line at a time, with the neighbors attached

By scofano·Created 11 months ago·Updated 3 months ago· 3
Text -> Line Context Counter
    • counter
    • current_line_text
    • previous_lines_text
    • next_lines_text
    • combined_context_text
    • all_lines_text
    text
    set_counter1
    previous_lines1
    next_lines1
    current_prefixCurrent line: {line}
    previous_prefixPrevious {count} lines: {lines}
    next_prefixNext {count} lines: {lines}
    all_lines_prefixAll the content:

    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_text and next_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 the all_lines_prefix applied.

    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 = 0 and all-empty strings, which is easy to mistake for a failure.
    • It wraps rather than clamps. set_counter = 6 on four lines selects line 2. That's deliberate, and it only bites if you assumed out-of-range means "stop."
    CategoryText/Utils

    Inputs (8)

    NameTypeDefaultDescription
    textSTRING
    set_counterINT1
    previous_linesINT1
    next_linesINT1
    current_prefixSTRINGCurrent line: {line}
    previous_prefixSTRINGPrevious {count} lines: {lines}
    next_prefixSTRINGNext {count} lines: {lines}
    all_lines_prefixSTRINGAll the content:

    Outputs (6)

    NameTypeDescription
    counterINT
    current_line_textSTRING
    previous_lines_textSTRING
    next_lines_textSTRING
    combined_context_textSTRING
    all_lines_textSTRING