Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. local function system(cmd)
  2. local proc = io.popen(cmd)
  3. local body = proc:read('*all')
  4. local ok = proc:close()
  5. return ok, body
  6. end
  7.  
  8.  
  9. local function trim(str)
  10. str = str:gsub('^%s+', '')
  11. str = str:gsub('%s+$', '')
  12. return str
  13. end
  14.  
  15.  
  16. local function tmpname_windows()
  17. return os.getenv('TEMP') .. os.tmpname() .. '_lua'
  18. end
  19.  
  20.  
  21. local function prompt_linux(default_msg)
  22. return system(
  23. 'zenity --text "Tweet body" --title "mpv-livetweet" ' ..
  24. '--ok-label "Tweet" ' ..
  25. '--entry --entry-text "' .. default_msg .. '"'
  26. )
  27. end
  28.  
  29.  
  30. local function prompt_macos(default_msg)
  31. return system(
  32. [[osascript ]] ..
  33. [[-e 'set tweet to text returned of (]] ..
  34. [[display dialog "Tweet body" with title "mpv-livetweet" ]] ..
  35. [[buttons {"Cancel", "Tweet"} ]] ..
  36. [[default button "Tweet" cancel button "Cancel" ]] ..
  37. [[default answer "]] .. default_msg .. [["]] ..
  38. [[)' ]] ..
  39. [[-e 'do shell script "echo " & quoted form of tweet']]
  40. )
  41. end
  42.  
  43.  
  44. local function prompt_windows(default_msg)
  45. local script_file = tmpname_windows() .. '.vbs'
  46.  
  47. local script =
  48. 'body = InputBox("Tweet body", "mpv-livetweet", "' .. default_msg .. '")\r\n' ..
  49. 'If IsEmpty(body) Then ' ..
  50. 'WScript.Quit(-1) ' ..
  51. 'Else ' ..
  52. 'WScript.Stdout.Write(body) ' ..
  53. 'End If'
  54. local f = io.open(script_file, 'w')
  55. f:write(script)
  56. f:close()
  57.  
  58. local ok, body = system('cscript /Nologo ' .. script_file)
  59.  
  60. os.remove(script_file)
  61.  
  62. return ok, body
  63. end
  64.  
  65.  
  66. local function prompt(OS, default_msg)
  67. assert(OS)
  68. local ok, body
  69.  
  70. if OS == 'linux' then
  71. ok, body = prompt_linux(default_msg)
  72. elseif OS == 'macos' then
  73. ok, body = prompt_macos(default_msg)
  74. elseif OS == 'windows' then
  75. ok, body = prompt_windows(default_msg)
  76. end
  77.  
  78. if ok then
  79. return trim(body)
  80. end
  81. end
  82.  
  83.  
  84. return {
  85. prompt = prompt
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement