waisbrot

Untitled

Jan 18th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. local SETTINGS_FILE = "pull.settings"
  3.  
  4. local SETTINGS = {"orgname", "reponame", "clobber"}
  5. local SETTING_DEFAULTS = {"", "", true}
  6.  
  7. function error(str)
  8.   term.setTextColor(colors.red)
  9.   print(str)
  10.   term.setTextColor(colors.white)
  11. end
  12.  
  13. function newline(win)
  14.   local _,cY = win.getCursorPos()
  15.   win.setCursorPos(1, cY+1)
  16. end
  17.  
  18. function config_init()
  19.   settings.clear()
  20.   for i=1,#SETTINGS do
  21.     settings.set(SETTINGS[i], SETTING_DEFAULTS[i])
  22.   end
  23.   settings.save(SETTINGS_FILE)
  24. end
  25.  
  26. function print_config_item(win, selected, name)
  27.   local val = settings.get(name)
  28.   if selected then
  29.     win.write("> ")
  30.   else
  31.     win.write("  ")
  32.   end
  33.   win.write(name .. ": ")
  34.   win.setTextColor(colors.yellow)
  35.   win.write(tostring(val))
  36.   win.setTextColor(colors.white)
  37.   newline(win)
  38. end
  39.  
  40. function print_config_menu(win, selected)
  41.   win.clear()
  42.   win.setCursorPos(1, 1)
  43.   win.setTextColor(colors.green)
  44.   win.write("up/down to select, space to edit")
  45.   newline(win)
  46.   win.write("enter to save changes")
  47.   newline(win)
  48.   win.setTextColor(colors.white)
  49.   for i=1,#SETTINGS do
  50.     print_config_item(win, selected == i, SETTINGS[i])
  51.   end
  52. end
  53.  
  54. function config_menu()
  55.   local w, h = term.getSize()
  56.   local win = window.create(term.current(), 1, 1, w - 2, h - 2)
  57.   selected = 1
  58.   local done = false
  59.   while not done do
  60.     print_config_menu(win, selected)
  61.     local event, key, isHeld = os.pullEvent("key")
  62.     if key == keys.up then
  63.       selected = (selected - 1) % #SETTINGS
  64.     elseif key == keys.down then
  65.       selected = (selected + 1) % #SETTINGS
  66.     elseif key == keys.enter then
  67.       done = true
  68.     elseif key == keys.space then
  69.       newline(win)
  70.       win.setTextColor(colors.green)
  71.       win.write(SETTINGS[selected] .. ": ")
  72.       local val = read()
  73.       val = val:gsub("^%s*(.-)%s*$", "%1")
  74.       settings.set(SETTINGS[selected], val)
  75.     else
  76.       done = true
  77.       print("ERROR bad key " .. key)
  78.     end
  79.     if selected == 0 then
  80.       selected = #SETTINGS
  81.     end
  82.   end
  83.   settings.save(SETTINGS_FILE)
  84. end
  85.  
  86. function configure()
  87.   if not settings.load(SETTINGS_FILE) then
  88.     config_init()
  89.   end
  90.   config_menu()
  91.   print("goodbye")
  92. end
  93.  
  94. function pull(file)
  95.   local url = "https://raw.githubusercontent.com/" .. settings.get("orgname") .. "/" .. settings.get("reponame") .. "/master/" .. file .. ".lua"
  96.   print("Fetching from " .. url)
  97.   local dataHandle = http.get(url)
  98.   local outHandle = fs.open(file, "w")
  99.   outHandle.write(dataHandle.readAll())
  100.   outHandle.close()
  101.   dataHandle.close()
  102.   print("Wrote " .. file)
  103. end
  104.  
  105. function main()
  106.   if #arg == 1 then
  107.   else
  108.     return error("Usage: pull ( name | --config )")
  109.   end
  110.   if arg[1] == "--config" then
  111.     configure()
  112.   else
  113.     pull(arg[1])
  114.   end
  115. end
  116.  
  117. main()
Advertisement
Add Comment
Please, Sign In to add comment