Gracefully handle small terminal

Also fuck it, global terminal size again
This commit is contained in:
Casey 2023-10-20 03:03:10 +03:00
parent 062d16ea69
commit d04eeacb36
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
1 changed files with 32 additions and 11 deletions

View File

@ -27,6 +27,36 @@ settings.define("mplayer.colors.status", {
default = 0x80EF80, -- #80EF80 default = 0x80EF80, -- #80EF80
type = number type = number
}) })
local tw, th = term.getSize()
os.queueEvent("dummy")
while tw < 20 or th < 10 do
local ev = { os.pullEvent() }
term.clear()
term.setCursorPos(1, 1)
printError("Too small: " .. tw .. "x" .. th .. " < 20x10")
printError("Q to exit")
printError("Y to ignore")
local setTextScale = term.current().setTextScale
if setTextScale ~= nil then
printError("S to try rescaling")
end
printError("I'll wait while you're adding more")
if ev[1] == "term_resize" then
tw, th = term.getSize()
elseif ev[1] == "key" and ev[2] == keys.s and setTextScale ~= nil then
setTextScale(0.5)
elseif ev[1] == "key" and ev[2] == keys.y then
break
elseif ev[1] == "key" and ev[2] == keys.q then
return
end
end
local drive = peripheral.find("tape_drive") local drive = peripheral.find("tape_drive")
if not drive then if not drive then
printError("No drive found, starting in dummy mode") printError("No drive found, starting in dummy mode")
@ -146,7 +176,6 @@ local mplayer = {
title = "Help", title = "Help",
scroll = 0, scroll = 0,
render = function(self) render = function(self)
local tw, th = term.getSize()
for i = 1, th - 3 do for i = 1, th - 3 do
local line = help[i + self.screens[1].scroll] or "~" local line = help[i + self.screens[1].scroll] or "~"
term.setCursorPos(1, i + 1) term.setCursorPos(1, i + 1)
@ -165,7 +194,6 @@ local mplayer = {
end end
end, end,
handleKey = function(self, key, repeating) handleKey = function(self, key, repeating)
local _, th = term.getSize()
if key == keys.down or key == keys.j then if key == keys.down or key == keys.j then
self.screens[1].handleScroll(self, 1) self.screens[1].handleScroll(self, 1)
elseif key == keys.up or key == keys.k then elseif key == keys.up or key == keys.k then
@ -177,7 +205,6 @@ local mplayer = {
end end
end, end,
handleScroll = function(self, direction, x, y) handleScroll = function(self, direction, x, y)
local _, th = term.getSize()
self.screens[1].scroll = math.max(0, math.min(th - 1, self.screens[1].scroll + direction)) self.screens[1].scroll = math.max(0, math.min(th - 1, self.screens[1].scroll + direction))
end, end,
}, },
@ -187,7 +214,6 @@ local mplayer = {
cursor = 1, cursor = 1,
textScroll = 0, textScroll = 0,
render = function(self) render = function(self)
local tw, th = term.getSize()
for i = 1, th - 3 do for i = 1, th - 3 do
local song = self.songs[i + self.screens[2].scroll] local song = self.songs[i + self.screens[2].scroll]
local isCurrent = (i + self.screens[2].scroll) == self.currentSong local isCurrent = (i + self.screens[2].scroll) == self.currentSong
@ -214,7 +240,6 @@ local mplayer = {
end end
end, end,
handleKey = function(self, key, repeating) handleKey = function(self, key, repeating)
local _, th = term.getSize()
if key == keys.down or key == keys.j then if key == keys.down or key == keys.j then
self.screens[2].handleScroll(self, 1) self.screens[2].handleScroll(self, 1)
elseif key == keys.up or key == keys.k then elseif key == keys.up or key == keys.k then
@ -231,7 +256,6 @@ local mplayer = {
end end
end, end,
handleScroll = function(self, direction, x, y) handleScroll = function(self, direction, x, y)
local _, th = term.getSize()
self.screens[2].cursor = math.max(1, math.min(#self.songs, self.screens[2].cursor + direction)) self.screens[2].cursor = math.max(1, math.min(#self.songs, self.screens[2].cursor + direction))
if self.screens[2].scroll + 1 > self.screens[2].cursor then if self.screens[2].scroll + 1 > self.screens[2].cursor then
self.screens[2].scroll = self.screens[2].cursor - 1 self.screens[2].scroll = self.screens[2].cursor - 1
@ -252,7 +276,6 @@ local mplayer = {
term.write(string.format("opt = %d, scroll = %d", self.screens[3].cursor, self.screens[3].scroll)) term.write(string.format("opt = %d, scroll = %d", self.screens[3].cursor, self.screens[3].scroll))
end, end,
handleKey = function(self, key, repeating) handleKey = function(self, key, repeating)
local _, th = term.getSize()
if key == keys.down or key == keys.j then if key == keys.down or key == keys.j then
self.screens[3].handleScroll(self, 1) self.screens[3].handleScroll(self, 1)
elseif key == keys.up or key == keys.k then elseif key == keys.up or key == keys.k then
@ -264,7 +287,6 @@ local mplayer = {
end end
end, end,
handleScroll = function(self, direction, x, y) handleScroll = function(self, direction, x, y)
local _, th = term.getSize()
self.screens[3].cursor = math.max(1, math.min(20, self.screens[3].cursor + direction)) self.screens[3].cursor = math.max(1, math.min(20, self.screens[3].cursor + direction))
if self.screens[3].scroll + 1 > self.screens[3].cursor then if self.screens[3].scroll + 1 > self.screens[3].cursor then
self.screens[3].scroll = self.screens[3].cursor - 1 self.screens[3].scroll = self.screens[3].cursor - 1
@ -293,8 +315,6 @@ term.clear()
parallel.waitForAny( parallel.waitForAny(
function() function()
while true do while true do
local tw, th = term.getSize()
-- Current screen -- Current screen
term.setCursorPos(1, 2) term.setCursorPos(1, 2)
mplayer.screens[mplayer.currentScreen].render(mplayer) mplayer.screens[mplayer.currentScreen].render(mplayer)
@ -359,7 +379,6 @@ function()
local pretty = require("cc.pretty") local pretty = require("cc.pretty")
while true do while true do
local _evd = { os.pullEvent() } local _evd = { os.pullEvent() }
local tw, th = term.getSize()
local ev, evd = table.remove(_evd, 1), _evd local ev, evd = table.remove(_evd, 1), _evd
if ev == "key" then if ev == "key" then
mplayer.heldKeys[evd[1]] = evd[2] mplayer.heldKeys[evd[1]] = evd[2]
@ -408,6 +427,8 @@ function()
end end
cx = cx + #caption cx = cx + #caption
end end
elseif ev == "term_resize" then
tw, th = term.getSize()
elseif ev == "tape_removed" then elseif ev == "tape_removed" then
mplayer.songs = {} mplayer.songs = {}
elseif ev == "tape_inserted" then elseif ev == "tape_inserted" then