tui.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env lua5.3
  2. tui_path="/usr/milis/ayguci/tui"
  3. package.cpath = tui_path.."/?.so" .. ";".. package.cpath
  4. package.path = tui_path.."/?.lua" .. ";".. package.path
  5. local serpent = require('serpent')
  6. local json = require('json')
  7. local ms = require ("minisock")
  8. track_menu=""
  9. last_url="module/list"
  10. function _err(msg)
  11. print("error:",msg)
  12. os.exit(1)
  13. end
  14. function start(request)
  15. sockpath = "/tmp/ayguci.sock"
  16. addr = "\1\0" .. sockpath .. "\0\0\0\0\0"
  17. if request then
  18. req=request
  19. else
  20. req = "module/list"
  21. end
  22. -- ensure server has enough time to start listening
  23. ms.msleep(50)
  24. sfd, msg = ms.connect(addr)
  25. if not sfd then _err("socket is not available") end
  26. -- write request to socket
  27. r, msg = ms.write(sfd, req)
  28. print("client sent: ".. sfd .. ":", req)
  29. -- get response from socket
  30. resp, msg = ms.read(sfd)
  31. print("client recv: ".. sfd .. ":", resp,":@:")
  32. --generate ui from response
  33. print("gelen ui:",serpent.line(json.decode(resp)["ui"]))
  34. local f = require('form')
  35. local ui_data={}
  36. if json.decode(resp).ui then
  37. ui_data=json.decode(resp).ui
  38. else
  39. print("hazır olmayan form")
  40. ui_data=f.msgbox
  41. end
  42. local ok, r, v,value = xpcall(f.generate, debug.traceback,ui_data)
  43. if not ok then
  44. print(r)
  45. _err("form is not available")
  46. else
  47. print("serialize:",serpent.line(value))
  48. print("json:",json.encode(value))
  49. --
  50. print("r:",r)
  51. print("v:",v:Text())
  52. print("ok",ok)
  53. --önce özel işlevli tuşlar işlenecek
  54. if v:Text() == 'Exit' then
  55. ms.close(sfd)
  56. os.exit()
  57. elseif v:Text() == 'Geri' then
  58. print("last:",last_url)
  59. start(last_url)
  60. -- sonra post_url var mı bakılacak.
  61. elseif next(value.post_url) ~= nil then
  62. -- modul listesi mi
  63. if value.modul then
  64. for _,e in pairs(ui_data) do
  65. if e.type=="list" then
  66. track_menu=e.val[value.modul]
  67. end
  68. end
  69. last_url=value.post_url["modul"]
  70. start(value.post_url["modul"].." "..json.encode(value))
  71. -- işlev listesi mi
  72. elseif value.func then
  73. for _,e in pairs(ui_data) do
  74. if e.type=="list" then
  75. track_menu=track_menu.."/"..e.val[value.func]
  76. end
  77. end
  78. --track_menu=track_menu.."/"..ui_data.val
  79. start(track_menu)
  80. -- Aktif edilen düğme var mı
  81. elseif v:Text() then
  82. -- if there is redirecting url
  83. -- start new form recursively until value.post is not exist
  84. start(value.post_url[v:Text()].." "..json.encode(value))
  85. -- Liste olup seçilen var mı
  86. elseif next(value.post_url) ~= nil then
  87. -- if there is redirecting url
  88. -- start new form recursively until value.post is not exist
  89. local _,url = next(value.post_url)
  90. start(url.." "..json.encode(value))
  91. -- Belirsiz post
  92. else
  93. print("value.post_url içerik bulunamadı!")
  94. os.exit()
  95. end
  96. --elseif v:Text() == 'Çıkış' or v:Text() == 'İptal' or v:Text() == 'Tamam' then
  97. else
  98. ms.close(sfd)
  99. os.exit()
  100. end
  101. end
  102. -- close socket
  103. r, msg = ms.close(sfd)
  104. end
  105. start(arg[1])