-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathIFDisplayTextNode.py
91 lines (76 loc) · 3.11 KB
/
IFDisplayTextNode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import sys
import logging
from typing import Optional, Union, List
# Initialize logger
logger = logging.getLogger(__name__)
class IFDisplayText:
def __init__(self):
self.type = "output"
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text": ("STRING", {"forceInput": True}),
"select": ("INT", {
"default": 0,
"min": 0,
"max": sys.maxsize, # No practical upper limit
"step": 1,
"tooltip": "Select which line to output (cycles through available lines)"
}),
},
"hidden": {},
}
RETURN_TYPES = ("STRING", "STRING", "INT", "STRING")
RETURN_NAMES = ("text", "text_list", "count", "selected")
OUTPUT_IS_LIST = (False, True, False, False)
FUNCTION = "display_text"
OUTPUT_NODE = True
CATEGORY = "ImpactFrames💥🎞️"
def display_text(self, text: Optional[Union[str, List[str]]], select):
if text is None:
logger.error("Received None for text input in display_text.")
return "", [], 0, ""
print("==================")
print("IF_AI_tool_output:")
print("==================")
print(text)
# Initialize variables
text_list = []
if isinstance(text, list):
# Handle list of strings
for idx, item in enumerate(text):
if isinstance(item, str):
lines = [line.strip() for line in item.split('\n') if line.strip()]
text_list.extend(lines)
else:
logger.warning(f"Expected string in text list at index {idx}, but got {type(item)}")
elif isinstance(text, str):
# Handle single string
text_list = [line.strip() for line in text.split('\n') if line.strip()]
else:
logger.error(f"Unexpected type for text: {type(text)}")
return "", [], 0, ""
count = len(text_list)
# Select line using modulo to handle cycling
if count == 0:
selected = text if isinstance(text, str) else ""
else:
selected = text_list[select % count]
# Prepare UI update
if isinstance(text, list):
ui_text = text # Pass the list directly
else:
ui_text = [text] # Wrap single string in a list
# Return both UI update and the multiple outputs
return {
"ui": {"string": ui_text},
"result": (
text, # complete text (string or list)
text_list, # list of individual lines as separate string outputs
count, # number of lines
selected # selected line based on select input
)
}
NODE_CLASS_MAPPINGS = {"IF_DisplayText": IFDisplayText}
NODE_DISPLAY_NAME_MAPPINGS = {"IF_DisplayText": "IF Display Text📟"}