forked from dail8859/LuaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_console.lua
More file actions
Latest commit
113 lines (93 loc) · 2.79 KB
/
Copy pathfix_console.lua
File metadata and controls
113 lines (93 loc) · 2.79 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
-- Unfold lines which copied from windows console
-- обрабатывает вставленный вывод консоли, склеивая перенесенные строки в одну
localfunctionGetEOLchar()
localEOLchar
ifeditor.EOLMode==0then
EOLchar="\r\n"
elseifeditor.EOLMode==1then
EOLchar="\r"
elseifeditor.EOLMode==2then
EOLchar="\n"
end
returnEOLchar
end
-- split without skip empty lines
localfunctionsplit(str, delimiter)
localresult= { }
localfrom=1
localdelim_from, delim_to=string.find( str, delimiter, from )
whiledelim_fromdo
table.insert( result, string.sub( str, from , delim_from-1 ) )
from=delim_to+1
delim_from, delim_to=string.find( str, delimiter, from )
end
table.insert( result, string.sub( str, from ) )
-- clean only space strings
fori=1, #resultdo
ifnotstring.find(result[i], '%S') then
result[i] =''
end
end
returnresult
end
localfunctionget_max_line_len(lines)
localres
for_, lineinipairs(lines) do
ifnotresorres<#linethen
res=#line
end
end
returnres
end
localfunctionmerge_lines(line_len_max, lines)
localres= {}
localcur_line
fori=1, #lines+1do
localline=lines[i] or''
--print(cur_line and #cur_line, #line, line)
if#line+1>=line_len_maxthen
if#line+1==line_len_maxthen
line=line..''
end
cur_line= (cur_lineor'') ..line
else
ifcur_linethen
line=cur_line..line
cur_line=nil
end
table.insert(res, line)
end
end
returnres
end
-- =========================================
npp.AddShortcut("Fix Concole Output", "", function()
editor:BeginUndoAction() -- Preserve Undo Capability
localtext=editor:GetSelText() -- Get any selected text
localSelectionStart, SelectionEnd
if#text>=1then
--read the selection points for restore after operation
SelectionStart=editor.SelectionStart
SelectionEnd=editor.SelectionEnd
else
text=editor:GetText() -- read selected text or entire document
end
localEOLchar=GetEOLchar() -- Preserve EOL Mode when we send back the reordered lines
--------------------
-- process the text
--------------------
localrawtextlines=split(text, EOLchar) -- split the text into an array of lines
localmax_len=get_max_line_len(rawtextlines)
print('max_len =', max_len)
rawtextlines=merge_lines(max_len, rawtextlines)
text=table.concat(rawtextlines, EOLchar) -- output to result var
-- replace selected text or entire document
ifSelectionStartthen
editor:ReplaceSel(text)
-- restore selection
editor:SetSel(SelectionStart, SelectionEnd)
else
editor:SetText(text)
end
editor:EndUndoAction()
end)