|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from enum import Enum |
| 4 | + |
| 5 | +from ...groups import conditional_group |
| 6 | +from ...node_base import NodeBase |
| 7 | +from ...node_factory import NodeFactory |
| 8 | +from ...properties.inputs import EnumInput, NumberInput, TextInput |
| 9 | +from ...properties.outputs import TextOutput |
| 10 | +from . import category as UtilityCategory |
| 11 | + |
| 12 | + |
| 13 | +class SliceOperation(Enum): |
| 14 | + START = 0 |
| 15 | + START_AND_LENGTH = 1 |
| 16 | + MAX_LENGTH = 2 |
| 17 | + |
| 18 | + |
| 19 | +class SliceAlignment(Enum): |
| 20 | + START = "start" |
| 21 | + END = "end" |
| 22 | + |
| 23 | + |
| 24 | +@NodeFactory.register("chainner:utility:text_slice") |
| 25 | +class TextSliceNode(NodeBase): |
| 26 | + def __init__(self): |
| 27 | + super().__init__() |
| 28 | + self.description = "Creates a slice of a given string of text." |
| 29 | + self.inputs = [ |
| 30 | + TextInput("Text", min_length=0), |
| 31 | + EnumInput( |
| 32 | + SliceOperation, |
| 33 | + label="Operation", |
| 34 | + default_value=SliceOperation.START, |
| 35 | + option_labels={ |
| 36 | + SliceOperation.START: "Start", |
| 37 | + SliceOperation.START_AND_LENGTH: "Start & Length", |
| 38 | + SliceOperation.MAX_LENGTH: "Maximum Length", |
| 39 | + }, |
| 40 | + ).with_id(1), |
| 41 | + conditional_group( |
| 42 | + enum=1, |
| 43 | + condition=( |
| 44 | + SliceOperation.START.value, |
| 45 | + SliceOperation.START_AND_LENGTH.value, |
| 46 | + ), |
| 47 | + )( |
| 48 | + NumberInput("Start", minimum=None, maximum=None, unit="chars"), |
| 49 | + ), |
| 50 | + conditional_group(enum=1, condition=SliceOperation.START_AND_LENGTH.value)( |
| 51 | + NumberInput("Length", minimum=0, maximum=None, unit="chars"), |
| 52 | + ), |
| 53 | + conditional_group(enum=1, condition=SliceOperation.MAX_LENGTH.value)( |
| 54 | + NumberInput("Maximum Length", minimum=0, maximum=None, unit="chars"), |
| 55 | + EnumInput(SliceAlignment, label="Alignment"), |
| 56 | + ), |
| 57 | + ] |
| 58 | + self.outputs = [ |
| 59 | + TextOutput( |
| 60 | + "Output Text", |
| 61 | + output_type=""" |
| 62 | + let text = toString(Input0); |
| 63 | + let operation = Input1; |
| 64 | + let start = Input2; |
| 65 | + let length = Input3; |
| 66 | + let maxLength = Input4; |
| 67 | + let alignment = Input5; |
| 68 | +
|
| 69 | + match operation { |
| 70 | + SliceOperation::Start => string::slice(text, start, inf), |
| 71 | + SliceOperation::StartAndLength => string::slice(text, start, length), |
| 72 | + SliceOperation::MaxLength => { |
| 73 | + match alignment { |
| 74 | + SliceAlignment::Start => string::slice(text, 0, maxLength), |
| 75 | + SliceAlignment::End => { |
| 76 | + match maxLength { |
| 77 | + 0 => "", |
| 78 | + _ as maxLength => string::slice(text, -maxLength, inf), |
| 79 | + } |
| 80 | + }, |
| 81 | + } |
| 82 | + }, |
| 83 | + } |
| 84 | + """, |
| 85 | + ) |
| 86 | + ] |
| 87 | + |
| 88 | + self.category = UtilityCategory |
| 89 | + self.name = "Text Slice" |
| 90 | + self.icon = "MdTextFields" |
| 91 | + self.sub = "Text" |
| 92 | + |
| 93 | + def run( |
| 94 | + self, |
| 95 | + text: str, |
| 96 | + operation: SliceOperation, |
| 97 | + start: int, |
| 98 | + length: int, |
| 99 | + max_length: int, |
| 100 | + alignment: SliceAlignment, |
| 101 | + ) -> str: |
| 102 | + if operation == SliceOperation.START: |
| 103 | + return text[start:] |
| 104 | + elif operation == SliceOperation.START_AND_LENGTH: |
| 105 | + start = max(-len(text), start) |
| 106 | + return text[start : start + length] |
| 107 | + elif operation == SliceOperation.MAX_LENGTH: |
| 108 | + if max_length == 0: |
| 109 | + return "" |
| 110 | + if alignment == SliceAlignment.START: |
| 111 | + return text[:max_length] |
| 112 | + elif alignment == SliceAlignment.END: |
| 113 | + return text[-max_length:] |
0 commit comments