yum-slop/TaSTT
Free self-hosted STT for VRChat.
git clone https://git.yummers.dev/yum-slop/TaSTT
f48ae0f
v0.19.2
1#!/usr/bin/env python3 2 3class TextWrapper : 4def __init__ ( self , rows , cols ) : 5self . rows = rows 6self . cols = cols 7 8# Split `msg` along line boundaries. Long words tend to just go onto new 9# lines. Words that are too long to fit on any line are hyphenated and 10# split. 11# Lines are padded with space (" ") characters so they're all `self.cols` 12# characters long. Pages are padded with lines full of space characters so 13# they're all `self.rows` lines long. 14def wrap ( self , msg : str ) -> list [ list [ str ]] : 15pages = [] 16lines = [] 17line = "" 18for word in msg . split () : 19if len ( line ) + 1 + len ( word ) <= self . cols : 20if len ( line ) : 21line += " " 22line += word 23continue 24# Word won't fit onto this line. End the line. 25if len ( line ) : 26line += " " * ( self . cols - len ( line )) 27lines . append ( line ) 28line = "" 29while len ( word ) > self . cols : 30prefix = word [ 0 : self . cols - 1 ] + "-" 31lines . append ( prefix ) 32suffix = word [ self . cols - 1 : ] 33word = suffix 34if len ( word ) : 35line = word 36if len ( line ) : 37line += " " * ( self . cols - len ( line )) 38lines . append ( line ) 39while len ( lines ) : 40pages . append ( lines [ 0 : self . rows ]) 41lines = lines [ self . rows : ] 42if len ( pages ) : 43num_extra_lines = ( self . rows - ( len ( pages [ - 1 ]) % self . rows )) % self . rows 44pages [ - 1 ] += [ " " * self . cols ] * num_extra_lines 45return pages 46 47if __name__ == "__main__" : 48w = TextWrapper ( 2 , 5 ) 49 50assert ( w . wrap ( "foo" ) == [[ "foo " , " " ]]) 51assert ( w . wrap ( "foo bar" ) == [[ "foo " , "bar " ]]) 52assert ( w . wrap ( "bagel" ) == [[ "bagel" , " " ]]) 53assert ( w . wrap ( "bagels" ) == [[ "bage-" , "ls " ]]) 54assert ( w . wrap ( "hot bagels" ) == [[ "hot " , "bage-" ] , [ "ls " , " " ]]) 55