irc: restore last active channel on reconnect; auto-switch from console on first JOIN; persist last target per server

Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
2025-09-20 01:56:50 +03:00
parent 8ec5f38d07
commit b57a19fd51

View File

@@ -73,6 +73,11 @@ function IrcChatView:init()
self:initHistory()
-- default to server console target
self:switchTarget("*")
-- Restore last active target if available
local last = self:loadLastTarget()
if last and last ~= "" then
self:switchTarget(last)
end
self:updateTitle()
TextViewer.init(self)
-- Start connection after UI init so we can show logs
@@ -117,6 +122,24 @@ function IrcChatView:writeHistory(target, line)
end
end
function IrcChatView:saveLastTarget(target)
local path = string.format("%s/.last", self._history_server_dir)
local f = io.open(path, "w")
if f then
f:write(target or "")
f:close()
end
end
function IrcChatView:loadLastTarget()
local path = string.format("%s/.last", self._history_server_dir)
local f = io.open(path, "r")
if not f then return nil end
local s = f:read("*l")
f:close()
return s
end
function IrcChatView:readLastLines(path, max_lines)
local f = io.open(path, "r")
if not f then return nil end
@@ -192,6 +215,8 @@ function IrcChatView:switchTarget(target)
end
-- reset unread counter when focusing this target
self._unread[target] = 0
-- persist last active target for this server
self:saveLastTarget(target)
end
function IrcChatView:appendLine(line, target)
@@ -447,7 +472,8 @@ function IrcChatView:handleLine(line)
self:appendLine(string.format("* %s joined %s", nick, ch), ch)
if nick == self._nick then
self:ensureBuffer(ch)
if not self._current_target then
-- Auto-switch to first joined channel if we're still on console
if self._current_target == "*" then
self:switchTarget(ch)
end
end