Repository navigation
Expand file tree
/
Copy pathsimple-awk.py
More file actions
59 lines (52 loc) · 1.73 KB
/
Copy pathsimple-awk.py
File metadata and controls
59 lines (52 loc) · 1.73 KB
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
#2024/09/07
def BufferToList(bufferID=None):
if bufferID != None:
currentBufferID = notepad.getCurrentBufferID()
notepad.activateBufferID(bufferID)
counter = 0
items = []
while True:
try:
line = editor.getLine(counter)
except IndexError:
break
items.append(line.rstrip('\r\n'))
counter += 1
if bufferID != None:
notepad.activateBufferID(currentBufferID)
if len(items) > 1 and items[-1] == '':
items = items[:-1]
return items
def GetEOL(bufferID=None):
if bufferID != None:
currentBufferID = notepad.getCurrentBufferID()
notepad.activateBufferID(bufferID)
line = editor.getLine(0)
if bufferID != None:
notepad.activateBufferID(currentBufferID)
if line.endswith('\r\n'):
return '\r\n'
elif line.endswith('\r'):
return '\r'
elif line.endswith('\n'):
return '\n'
else:
raise Exception('Unknown EOL marker')
def SimpleAwk():
separator = notepad.prompt('Separator (default space character):', 'Provide a separator', ' ')
template = notepad.prompt('Template ({1}, {2}, ...):', 'Provide a template', '{1}')
options = notepad.prompt('Options (n for new document):', 'Provide options', '')
if separator == None or template == None or options == None:
return
currentList = BufferToList()
currentEOL = GetEOL()
newList = []
for item in currentList:
line = template
for index, field in enumerate(item.split(separator)):
line = line.replace('{%d}' % (index + 1), field)
newList.append(line)
if options == 'n':
notepad.new()
editor.setText(currentEOL.join(newList + ['']))
SimpleAwk()