/extman/scite_lua/prompt.lua

http://github.com/davidm/lua-inspect · Lua · 91 lines · 78 code · 10 blank · 3 comment · 20 complexity · 9568e6c5e1ae260c7cf52f31ce3bf389 MD5 · raw file

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