aboutsummaryrefslogtreecommitdiff
path: root/core/ext/pm/file_browser.lua
blob: caae33f6773e3df57d8a59ff5b52309e19538f9d (plain)
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
-- Copyright 2007-2008 Mitchell mitchell<att>caladbolg.net. See LICENSE.

---
-- File browser for the Textadept project manager.
-- It is enabled by providing the absolute path to a directory in the project
-- manager entry field.
module('textadept.pm.browsers.file', package.seeall)

function matches(entry_text)
  if not WIN32 then
    return entry_text:sub(1, 1) == '/'
  else
    return entry_text:match('[A-Za-z]:[\\/]')
  end
end

function get_contents_for(full_path)
  local dirpath = table.concat(full_path, '/')
  local dir = {}
  if not WIN32 then
    local p = io.popen('ls -1p "'..dirpath..'"')
    local out = p:read('*all')
    p:close()
    if #out == 0 then
      error('No such directory: '..dirpath)
      return dir
    end
    for entry in out:gmatch('[^\n]+') do
      if entry:sub(-1, -1) == '/' then
        local name = entry:sub(1, -2)
        dir[name] = {
          parent = true,
          text = name,
          pixbuf = 'gtk-directory'
        }
      else
        dir[entry] = { text = entry }
      end
    end
  else
    local p = io.popen('dir /A:D /B "'..dirpath..'"')
    local out = p:read('*all')
    p:close()
    if out:match('^File Not Found') then
      error('No such directory: '..dirpath)
      return dir
    end
    for name in out:gmatch('[^\n]+') do
      dir[name] = {
        parent = true,
        text = name,
        pixbuf = 'gtk-directory'
      }
    end
    p = io.popen('dir /A:-D /B "'..dirpath..'"')
    out = p:read('*all')
    p:close()
    for entry in out:gmatch('[^\n]+') do
      dir[entry] = { text = entry }
    end
  end
  return dir
end

function perform_action(selected_item)
  local filepath = table.concat(selected_item, '/')
  textadept.io.open(filepath)
  view:focus()
end

function get_context_menu(selected_item)
  return { '_Change Directory', 'File _Details' }
end

function perform_menu_action(menu_item, selected_item)
  local filepath = table.concat(selected_item, '/')
  if menu_item == 'Change Directory' then
    textadept.pm.entry_text = filepath
    textadept.pm.activate()
  elseif menu_item == 'File Details' then
    if not WIN32 then
      local p = io.popen('ls -dhl "'..filepath..'"')
      local out = p:read('*all')
      p:close()
      local perms, num_dirs, owner, group, size, mod_date =
        out:match('^(%S+) (%S+) (%S+) (%S+) (%S+) (%S+ %S)')
      out = 'File details for:\n'..filepath..'\n'..
            'Perms:\t'..perms..'\n'..
            '#Dirs:\t'..num_dirs..'\n'..
            'Owner:\t'..owner..'\n'..
            'Group:\t'..group..'\n'..
            'Size:\t'..size..'\n'..
            'Date:\t'..mod_date
      text_input(out, nil, false, 250, 250)
    end
  end
end