File: pack.lua

package info (click to toggle)
vifm 0.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,252 kB
  • sloc: ansic: 179,567; sh: 5,445; makefile: 723; perl: 347; python: 76; xml: 26
file content (70 lines) | stat: -rw-r--r-- 2,429 bytes parent folder | download | duplicates (2)
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
function pack(info) -- <<<
   local files = vifm.expand('%"f')
   if #files == 0 or
         files == '.' or files == './' or
         files == '..' or files == '../' then
      vifm.sb.error('Error: no files are selected')
      return
   end

   local outfile
   local ext = 'tar.gz' -- default extension
   if #info.argv == 1 then
      outfile = unescape_name(vifm.expand(info.argv[1]))
      ext = vifm.fnamemodify(outfile, ':e')
      if vifm.fnamemodify(outfile, ':r:e') == 'tar' then
         ext = 'tar.'..ext
      end
   else
      local singlefile = (vifm.expand('%"c') == files)
      local basename = singlefile and vifm.expand('%c:r:r') or vifm.expand('%d:t')
      outfile = string.format("%s.%s", unescape_name(basename), ext)
   end
   if vifm.exists(outfile) then
      vifm.errordialog(":Pack", string.format("File already exists: %s", outfile))
      return
   end

   local eoutfile = vifm.escape(outfile)

   local cmd
   if ext == 'tar.gz' or ext == 'tgz' then
      cmd = string.format("tar --force-local -cvzf %s %s", eoutfile, files)
   elseif ext == 'tar.bz2' or ext == 'tbz2' then
      cmd = string.format("tar --force-local -cvjf %s %s", eoutfile, files)
   elseif ext == 'tar.xz' or ext == 'txz' then
      cmd = string.format("tar --force-local -cvJf %s %s", eoutfile, files)
   elseif ext == 'tar.zst' or ext == 'tzst' then
      cmd = string.format("tar -I 'zstd -19' --force-local -cvf %s %s",
                          eoutfile, files)
   elseif ext == "tar" then
      cmd = string.format("tar --force-local -cvf %s %s", eoutfile, files)
   elseif ext == "7z" or
         ext == "lz4" or
         ext == "zip" then
      cmd = string.format("7z a %s %s", eoutfile, files)
   else -- TODO: rar, gzip, bzip2, xz, zst etc
      vifm.sb.error(string.format("Unknown format: %s", ext))
      return
   end

   local job = vifm.startjob {
      cmd = cmd,
      description = "Packing: "..outfile,
      visible = true,
      iomode = '', -- ignore output to not block
      onexit = function(job)
          if job:exitcode() ~= 0 then
             local errors = job:errors()
             if #errors == 0 then
                vifm.errordialog('Packing failed',
                                 'Error message is not available.')
             else
                vifm.errordialog('Packing failed', errors)
             end
          end
      end
   }
end -- >>>

-- vim: set et ts=3 sts=3 sw=3: