Last active
March 14, 2019 15:48
-
-
Save steinuil/f558156b30ed6ff2a144c77e1822afbb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function system(cmd) | |
local proc = io.popen(cmd) | |
local body = proc:read('*all') | |
local ok = proc:close() | |
return ok, body | |
end | |
local function trim(str) | |
str = str:gsub('^%s+', '') | |
str = str:gsub('%s+$', '') | |
return str | |
end | |
local function tmpname_windows() | |
return os.getenv('TEMP') .. os.tmpname() .. '_lua' | |
end | |
local function prompt_linux(default_msg) | |
return system( | |
'zenity --text "Tweet body" --title "mpv-livetweet" ' .. | |
'--ok-label "Tweet" ' .. | |
'--entry --entry-text "' .. default_msg .. '"' | |
) | |
end | |
local function prompt_macos(default_msg) | |
return system( | |
[[osascript ]] .. | |
[[-e 'set tweet to text returned of (]] .. | |
[[display dialog "Tweet body" with title "mpv-livetweet" ]] .. | |
[[buttons {"Cancel", "Tweet"} ]] .. | |
[[default button "Tweet" cancel button "Cancel" ]] .. | |
[[default answer "]] .. default_msg .. [["]] .. | |
[[)' ]] .. | |
[[-e 'do shell script "echo " & quoted form of tweet']] | |
) | |
end | |
local function prompt_windows(default_msg) | |
local script_file = tmpname_windows() .. '.vbs' | |
local script = | |
'body = InputBox("Tweet body", "mpv-livetweet", "' .. default_msg .. '")\r\n' .. | |
'If IsEmpty(body) Then ' .. | |
'WScript.Quit(-1) ' .. | |
'Else ' .. | |
'WScript.Stdout.Write(body) ' .. | |
'End If' | |
local f = io.open(script_file, 'w') | |
f:write(script) | |
f:close() | |
local ok, body = system('cscript /Nologo ' .. script_file) | |
os.remove(script_file) | |
return ok, body | |
end | |
local function prompt(OS, default_msg) | |
assert(OS) | |
local ok, body | |
if OS == 'linux' then | |
ok, body = prompt_linux(default_msg) | |
elseif OS == 'macos' then | |
ok, body = prompt_macos(default_msg) | |
elseif OS == 'windows' then | |
ok, body = prompt_windows(default_msg) | |
end | |
if ok then | |
return trim(body) | |
end | |
end | |
return { | |
prompt = prompt | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment