finish word wrappig, create util toolkit
This commit is contained in:
38
src/fenrir-package/utils/char_utils.py
Normal file
38
src/fenrir-package/utils/char_utils.py
Normal file
@ -0,0 +1,38 @@
|
||||
#!/bin/python
|
||||
|
||||
def getPrevChar(currX,currY, currText):
|
||||
if currText == '':
|
||||
return -1, -1, ''
|
||||
wrappedLines = currText.split('\n')
|
||||
x = currX
|
||||
y = currY
|
||||
if x - 1 < 0:
|
||||
if y - 1 > 0:
|
||||
y -= 1
|
||||
x = len(wrappedLines[y]) - 1
|
||||
else:
|
||||
x -= 1
|
||||
currChar = wrappedLines[y][x]
|
||||
return x, y, currChar
|
||||
|
||||
def getCurrentChar(currX,currY, currText):
|
||||
if currText == '':
|
||||
return -1, -1, ''
|
||||
wrappedLines = currText.split('\n')
|
||||
currChar = wrappedLines[currY][currX]
|
||||
return currX, currY, currChar
|
||||
|
||||
def getNextChar(currX,currY, currText):
|
||||
if currText == '':
|
||||
return -1, -1, ''
|
||||
wrappedLines = currText.split('\n')
|
||||
x = currX
|
||||
y = currY
|
||||
if x + 1 == len(wrappedLines[y]):
|
||||
if y + 1 < len(wrappedLines) - 1:
|
||||
y += 1
|
||||
x = 0
|
||||
else:
|
||||
x += 1
|
||||
currChar = wrappedLines[y][x]
|
||||
return x, y, currChar
|
@ -1,112 +0,0 @@
|
||||
def getPrevWord(currX,currY, currText):
|
||||
if currText == '':
|
||||
return -1, -1, ''
|
||||
x, y, word = getCurrentWord(currX,currY,currText)
|
||||
wrappedLines = currText.split('\n')
|
||||
if (word == ''):
|
||||
return currX, currY, ''
|
||||
while True:
|
||||
if x < 2:
|
||||
if y != 0:
|
||||
y -= 1
|
||||
else:
|
||||
return currX, currY, ''
|
||||
x = len(wrappedLines[y]) - 1
|
||||
else:
|
||||
x -= 1
|
||||
if wrappedLines[y] != '':
|
||||
break
|
||||
x, y, word = getCurrentWord(x, y, currText)
|
||||
if word == '':
|
||||
return currX, currY, ''
|
||||
return x, y, word
|
||||
|
||||
def getCurrentWord(currX,currY, currText):
|
||||
if currText == '':
|
||||
return -1, -1, ''
|
||||
x = currX
|
||||
y = currY
|
||||
wrappedLines = currText.split('\n')
|
||||
wordFound = False
|
||||
currWord = ''
|
||||
currLine = wrappedLines[y].replace("\t"," ")
|
||||
if currLine[x] == ' ' and x > 1:
|
||||
x = x - 2
|
||||
while not wordFound:
|
||||
x = currLine[:x].rfind(" ")
|
||||
if x == -1:
|
||||
x = 0
|
||||
else:
|
||||
x += 1
|
||||
wordEnd = currLine[x + 1:].find(" ")
|
||||
if wordEnd == -1:
|
||||
wordEnd = len(currLine)
|
||||
else:
|
||||
wordEnd += x + 1
|
||||
currWord = currLine[x:wordEnd]
|
||||
wordFound = currWord.strip(" \t\n") != ''
|
||||
if wordFound:
|
||||
break
|
||||
if x == 0:
|
||||
if y != 0:
|
||||
y -= 1
|
||||
currLine = wrappedLines[y].replace("\t"," ")
|
||||
else:
|
||||
return currX, currY, ''
|
||||
x = len(wrappedLines[y]) - 1
|
||||
else:
|
||||
x -= 1
|
||||
return x, y, currWord
|
||||
|
||||
def getNextWord(currX,currY, currText):
|
||||
if currText == '':
|
||||
return -1, -1, ''
|
||||
x = currX
|
||||
y = currY
|
||||
wrappedLines = currText.split('\n')
|
||||
wordFound = False
|
||||
currWord = ''
|
||||
currLine = wrappedLines[y].replace("\t"," ")
|
||||
while not wordFound:
|
||||
xtmp = 0
|
||||
if x + 1 >= len(currLine):
|
||||
if y < len(wrappedLines):
|
||||
y += 1
|
||||
currLine = wrappedLines[y].replace("\t"," ")
|
||||
else:
|
||||
return currX, currY, ''
|
||||
x = 0
|
||||
else:
|
||||
x += 1
|
||||
xtmp = x
|
||||
x = currLine[x:].find(" ")
|
||||
if x == -1:
|
||||
x = len(currLine)
|
||||
continue
|
||||
else:
|
||||
if xtmp <> 0:
|
||||
xtmp += 1
|
||||
x += xtmp
|
||||
if x + 1 < len(currLine):
|
||||
wordEnd = currLine[x + 1:].find(" ")
|
||||
else:
|
||||
wordEnd = -1
|
||||
if wordEnd == -1:
|
||||
wordEnd = len(currLine)
|
||||
else:
|
||||
wordEnd += x + 1
|
||||
if wordEnd >= len(currLine) and y + 1 >= len(wrappedLines):
|
||||
return currX, currY, ''
|
||||
currWord = currLine[x:wordEnd]
|
||||
wordFound = currWord.strip(" \t\n") != ''
|
||||
if not wordFound:
|
||||
x = wordEnd
|
||||
return x, y, currWord
|
||||
|
||||
currText = " das ist ein test\ntest das\ntesttest\n\ntest"
|
||||
currY = 0
|
||||
currX = 4
|
||||
currX, currY, word = getCurrentWord(currX,currY,currText)
|
||||
#currX, currY, word = getPrevWord(currX,currY,currText)
|
||||
#currX, currY, word = getNextWord(currX,currY,currText)
|
||||
print(currX, currY, word)
|
35
src/fenrir-package/utils/line_utils.py
Normal file
35
src/fenrir-package/utils/line_utils.py
Normal file
@ -0,0 +1,35 @@
|
||||
#!/bin/python
|
||||
|
||||
def getPrevLine(currX,currY, currText):
|
||||
if currText == '':
|
||||
return -1, -1, ''
|
||||
wrappedLines = currText.split('\n')
|
||||
x = currX
|
||||
y = currY
|
||||
if y - 1 >= 0:
|
||||
y -= 1
|
||||
x = 0
|
||||
currLine = wrappedLines[y]
|
||||
return x, y, currLine
|
||||
|
||||
def getCurrentLine(currX,currY, currText):
|
||||
if currText == '':
|
||||
return -1, -1, ''
|
||||
wrappedLines = currText.split('\n')
|
||||
x = currX
|
||||
y = currY
|
||||
x = 0
|
||||
currLine = wrappedLines[y]
|
||||
return x, y, currLine
|
||||
|
||||
def getNextLine(currX,currY, currText):
|
||||
if currText == '':
|
||||
return -1, -1, ''
|
||||
wrappedLines = currText.split('\n')
|
||||
x = currX
|
||||
y = currY
|
||||
if y + 1 < len(wrappedLines):
|
||||
y += 1
|
||||
x = 0
|
||||
currLine = wrappedLines[y]
|
||||
return x, y, currLine
|
@ -3,9 +3,9 @@
|
||||
def getPrevWord(currX,currY, currText):
|
||||
if currText == '':
|
||||
return -1, -1, ''
|
||||
x, y, word = getCurrentWord(currX,currY,currText)
|
||||
x, y, currWord = getCurrentWord(currX,currY,currText)
|
||||
wrappedLines = currText.split('\n')
|
||||
if (word == ''):
|
||||
if (currWord == ''):
|
||||
return currX, currY, ''
|
||||
while True:
|
||||
if x < 2:
|
||||
@ -18,10 +18,10 @@ def getPrevWord(currX,currY, currText):
|
||||
x -= 1
|
||||
if wrappedLines[y] != '':
|
||||
break
|
||||
x, y, word = getCurrentWord(x, y, currText)
|
||||
if word == '':
|
||||
x, y, currWord = getCurrentWord(x, y, currText)
|
||||
if currWord == '':
|
||||
return currX, currY, ''
|
||||
return x, y, word
|
||||
return x, y, currWord
|
||||
|
||||
def getCurrentWord(currX,currY, currText):
|
||||
if currText == '':
|
||||
@ -86,7 +86,7 @@ def getNextWord(currX,currY, currText):
|
||||
x = len(currLine)
|
||||
continue
|
||||
else:
|
||||
if xtmp <> 0:
|
||||
if xtmp != 0:
|
||||
xtmp += 1
|
||||
x += xtmp
|
||||
if x + 1 < len(currLine):
|
||||
|
Reference in New Issue
Block a user