-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.lua
More file actions
141 lines (114 loc) · 2.88 KB
/
main.lua
File metadata and controls
141 lines (114 loc) · 2.88 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
local prepare_rsync = ya.sync(function()
local yanked = cx.yanked
local operation = {
urls = {},
target_dir = tostring(cx.active.current.cwd),
is_cut_operation = yanked.is_cut,
}
if #yanked == 0 then
return operation
end
for _, url in pairs(yanked) do
table.insert(operation.urls, tostring(url))
end
return operation
end)
local update_rsync_paste_progress = ya.sync(function(_, str)
Header._rsync_paste_progress = str
ya.render()
end)
local render_rsync_paste_progress = ya.sync(function(_)
Header._rsync_paste_progress = ''
function Header:rsync_paste_progress()
return ui.Span(Header._rsync_paste_progress):style(ui.Style():fg('yellow'))
end
local component_id = Header:children_add('rsync_paste_progress', 1500, Header.RIGHT)
ya.render()
return component_id
end)
local remove_rsync_paste_progress = ya.sync(function(_, id)
Header._rsync_paste_progress = nil
Header:children_remove(id, Header.RIGHT)
ya.render()
end)
return {
entry = function()
local rsync_operation = prepare_rsync()
if #rsync_operation.urls == 0 then
ya.notify({
title = 'rsync-paste',
content = 'No files to move/copy',
timeout = 5,
level = 'warn',
})
return
end
local action = rsync_operation.is_cut_operation and 'Moving' or 'Copying'
ya.notify({
title = 'rsync-paste',
content = string.format('%s %d files...', action, #rsync_operation.urls),
timeout = 5,
level = 'info',
})
local component_id = render_rsync_paste_progress()
local child, _ = Command('rsync')
:arg({
'-av',
'--info=progress2',
})
:arg(rsync_operation.urls)
:arg(rsync_operation.target_dir)
---@diagnostic disable: undefined-field
:stdout(Command.PIPED)
---@diagnostic disable: undefined-field
:spawn()
while true do
---@diagnostic disable: need-check-nil
local data, event = child:read_line()
---@diagnostic disable: need-check-nil
if event == 2 then
break
elseif event == 0 then
if data:find('%%') then
local percentage = data:match('(%d+)%%')
if percentage then
local str = string.format(' %s files: %s%% ', action, percentage)
update_rsync_paste_progress(str)
end
end
end
end
local status, _ = child:wait()
if status and not status.success then
ya.notify({
title = 'rsync-paste',
content = string.format('%s to %s error', action, rsync_operation.target_dir),
timeout = 5,
level = 'error',
})
remove_rsync_paste_progress()
return
else
if rsync_operation.is_cut_operation then
local _, _ = Command('rm')
:arg({
'-rf',
})
:arg(rsync_operation.urls)
:status()
end
end
ya.notify({
title = 'rsync-paste',
content = string.format(
'%s %d files to %s done',
action,
#rsync_operation.urls,
rsync_operation.target_dir
),
timeout = 5,
level = 'info',
})
remove_rsync_paste_progress(component_id)
end,
}