|
1 | 1 | local M = {} |
2 | 2 |
|
3 | 3 | function M:peek() |
4 | | - local output, code = Command("transmission-show") |
| 4 | + -- launch process |
| 5 | + local process, code = Command("transmission-show") |
5 | 6 | :args({ |
6 | 7 | tostring(self.file.url), |
7 | 8 | }) |
8 | 9 | :stdout(Command.PIPED) |
9 | | - :output() |
| 10 | + :spawn() |
10 | 11 |
|
11 | | - local p |
12 | | - p = ui.Paragraph.parse(self.area, output.stdout) |
| 12 | + local limit = self.area.h |
| 13 | + -- read and count lines from process |
| 14 | + local i, lines = 0, "" |
| 15 | + repeat |
| 16 | + local next, event = process:read_line() |
| 17 | + if event ~= 0 then |
| 18 | + break |
| 19 | + end |
13 | 20 |
|
14 | | - ya.preview_widgets(self, { p:wrap(ui.Paragraph.WRAP) }) |
| 21 | + i = i + 1 |
| 22 | + -- only concatenate lines that are past 'skip' number of STDOUT |
| 23 | + if i > self.skip then |
| 24 | + lines = lines .. next |
| 25 | + end |
| 26 | + until i >= self.skip + limit -- until reader reaches max number of lines on screen plus skip |
| 27 | + |
| 28 | + process:start_kill() |
| 29 | + |
| 30 | + -- if paged below all output, run peek again with smaller skip |
| 31 | + if self.skip > 0 and i < self.skip + limit then |
| 32 | + ya.manager_emit( |
| 33 | + "peek", |
| 34 | + { tostring(math.max(0, i - limit)), only_if = tostring(self.file.url), upper_bound = "" } |
| 35 | + ) |
| 36 | + -- preview torrent |
| 37 | + else |
| 38 | + ya.preview_widgets(self, { ui.Paragraph.parse(self.area, lines):wrap(ui.Paragraph.WRAP) }) |
| 39 | + end |
15 | 40 | end |
16 | 41 |
|
17 | | -function M:seek() end |
| 42 | +function M:seek(units) |
| 43 | + local h = cx.active.current.hovered |
| 44 | + if h and h.url == self.file.url then |
| 45 | + local step = math.floor(units * self.area.h / 10) |
| 46 | + ya.manager_emit("peek", { |
| 47 | + math.max(0, cx.active.preview.skip + step), |
| 48 | + only_if = self.file.url, |
| 49 | + }) |
| 50 | + end |
| 51 | +end |
18 | 52 |
|
19 | 53 | return M |
0 commit comments