Krobys

[Util] Char Width Taker Program

Mar 24th, 2023 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.96 KB | Gaming | 0 0
  1. local component = require("component")
  2. local bridge = component.openperipheral_bridge
  3. local text = require("text")
  4. local unicode = require("unicode")
  5. local event = require("event")
  6. local serialization = require("serialization")
  7. --local fs = require("filesystem")
  8.  
  9. local lineWidth = 1
  10.  
  11. local letters = {
  12.     "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
  13.     "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
  14.     "А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж", "З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ы", "Ь", "Э", "Ю", "Я",
  15.     "а", "б", "в", "г", "д", "е", "ё", "ж", "з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы", "ь", "э", "ю", "я"
  16. }
  17. local symbols = {"!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+", "[", "]", "{", "}", ";", ":", "'", "\"", ",",
  18.                  ".", "/", "<", ">", "?", "\\"}
  19.  
  20. local checkedChars = {}
  21. local all_chars = {}
  22.  
  23. for i, value in ipairs(letters) do
  24.     table.insert(all_chars, value)
  25. end
  26.  
  27. for i, value in ipairs(symbols) do
  28.     table.insert(all_chars, value)
  29. end
  30.  
  31. local currentCharIndex = 1
  32.  
  33. local yLine = 11
  34.  
  35. local path = "/home/checkedChars.txt"
  36.  
  37. local function saveCharsToFile()
  38.     local serialised = serialization.serialize(checkedChars)
  39.  
  40.     print()
  41.     local file = io.open(path, "w")
  42.  
  43.     file:write(serialised)
  44.     file:close()
  45.     print("Записано в файл "..path)
  46. end
  47.  
  48. local function continueFromFile()
  49.     local file = io.open(path, "r")
  50.     if file then
  51.         local string = file:read("*all")
  52.         if string and #string > 0 then
  53.             print("файл прочитан"..#checkedChars)
  54.             checkedChars = serialization.unserialize(string)
  55.             if #checkedChars > 0 then
  56.                 currentCharIndex = #checkedChars + 1
  57.             end
  58.         else
  59.             print("Ошибка чтения с файла, файл или пустой или невалидные данные")
  60.         end
  61.         file:close()
  62.     else
  63.         print("Ошибка чтения с файла, файл или пустой или невалидные данные")
  64.     end
  65.  
  66. end
  67.  
  68. continueFromFile()
  69.  
  70. bridge.clear()
  71. local currSize = bridge.addText(20, 20, tostring(lineWidth), 0x00ffa2)
  72. local charsBox = bridge.addText(10, 10, all_chars[currentCharIndex] or all_chars[#all_chars], 0xFFFFFF)
  73. local box = bridge.addBox(10, yLine, lineWidth, 1, 0x00ffa2)
  74. bridge.sync()
  75.  
  76. local function handleKeyEvent(...)
  77.     local args = {...}
  78.     local eventName = args[1]
  79.     local code = args[5]
  80.     print(eventName.. " "..code)
  81.     if eventName == "glasses_key_down" and code == 12 or code == 13 or code == 28 or code == 42 or code == 200 or code == 208 or code == 205 or code == 203 then
  82.         if code == 12 then -- минус
  83.             lineWidth = lineWidth - 1
  84.         elseif code == 13 then --плюс
  85.             lineWidth = lineWidth + 1
  86.         elseif code == 28 then --ентер
  87.             table.insert(checkedChars,currentCharIndex,{char = all_chars[currentCharIndex], charWidth = lineWidth})
  88.             currentCharIndex = #checkedChars + 1
  89.             if currentCharIndex > #all_chars then
  90.                 saveCharsToFile()
  91.             end
  92.             print("Символ \'"..all_chars[currentCharIndex].."\' записан")
  93.         elseif code == 42 then --шифт
  94.             saveCharsToFile()
  95.         elseif code == 200 then --стрелочка вверх
  96.             yLine = yLine - 1
  97.         elseif code == 208 then --стрклочка вниз
  98.             yLine = yLine + 1
  99.         elseif code == 205 then --стрелочка вправо
  100.             if currentCharIndex < #checkedChars then
  101.                 print("next")
  102.                 currentCharIndex = currentCharIndex + 1
  103.                 lineWidth = checkedChars[currentCharIndex].charWidth or 1
  104.             end
  105.         elseif code == 203 then --стрелочка влево
  106.             if currentCharIndex > 1 then
  107.                 currentCharIndex = currentCharIndex - 1
  108.                 lineWidth = checkedChars[currentCharIndex].charWidth or 1
  109.             end
  110.             print("prev")
  111.         end
  112.         currSize.setText(tostring(lineWidth))
  113.         box.setWidth(lineWidth)
  114.         box.setY(yLine)
  115.         charsBox.setText(all_chars[currentCharIndex])
  116.         bridge.sync()
  117.     end
  118. end
  119.  
  120. event.listen("glasses_capture", function()
  121.     local uuid = bridge.getUsers()[1].uuid -- можно использовать только если один пользователь использует терминал
  122.     local capture = bridge.getCaptureControl(uuid)
  123.     capture.setBackground(0x000000, 0.05)
  124. end)
  125.  
  126. event.listen("glasses_key_down", handleKeyEvent)
Advertisement
Add Comment
Please, Sign In to add comment