PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/extman/scite_lua/prompt.lua

http://github.com/davidm/lua-inspect
Lua | 91 lines | 78 code | 10 blank | 3 comment | 16 complexity | 9568e6c5e1ae260c7cf52f31ce3bf389 MD5 | raw file
 1 scite_Command('Last Command|do_command_list|Ctrl+Alt+P')
 2
 3 local prompt = '> '
 4 local history_len = 4
 5 local prompt_len = string.len(prompt)
 6 print 'Scite/Lua'
 7 trace(prompt)
 8
 9 function load(file)
10	if not file then file = props['FilePath'] end
11	dofile(file)
12 end
13
14 function edit(file)
15	scite.Open(file)
16 end
17
18 local sub = string.sub
19 local commands = {}
20
21 local function strip_prompt(line)
22   if sub(line,1,prompt_len) == prompt then
23        line = sub(line,prompt_len+1)
24    end	
25	return line
26 end
27
28-- obviously table.concat is much more efficient, but requires that the table values
29-- be strings.
30function join(tbl,delim,start,finish)
31	local n = table.getn(tbl)
32	local res = ''
33	-- this is a hack to work out if a table is 'list-like' or 'map-like'
34	local index1 = n > 0 and tbl[1]
35	local index2 = n > 1 and tbl[2]
36	if index1 and index2 then
37		for i,v in ipairs(tbl) do
38			res = res..delim..tostring(v)
39		end
40	else
41		for i,v in pairs(tbl) do
42			res = res..delim..tostring(i)..'='..tostring(v)
43		end
44	end
45	return string.sub(res,2)
46end
47
48function pretty_print(...)
49	for i,val in ipairs(arg) do
50	if type(val) == 'table' then
51		print('{'..join(val,',',1,20)..'}')
52	elseif type(val) == 'string' then
53		print("'"..val.."'")
54	else
55		print(val)
56	end
57	end
58end
59
60 scite_OnOutputLine (function (line)
61	line = strip_prompt(line)
62   table.insert(commands,1,line)
63    if table.getn(commands) > history_len then
64        table.remove(commands,history_len+1)
65    end
66    if sub(line,1,1) == '=' then
67        line = 'pretty_print('..sub(line,2)..')'
68    end
69    local f,err = loadstring(line,'local')
70    if not f then
71      print(err)
72    else
73      local ok,res = pcall(f)
74      if ok then
75         if res then print('result= '..res) end
76      else
77         print(res)
78      end
79    end
80    trace(prompt)
81    return true
82end)
83
84function insert_command(cmd)
85	output:AppendText(cmd)
86	output:GotoPos(output.Length)
87end
88
89function do_command_list()
90     scite_UserListShow(commands,1,insert_command)
91end