Skip to content

Commit 607a97a

Browse files
committed
fix(levels): handle invalid level titles gracefully
Signed-off-by: Guennadi Maximov C <g.maxc.fox@protonmail.com>
1 parent 6d36351 commit 607a97a

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

lua/triforce/levels.lua

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
---@field unlocked boolean
1515
---@field title string
1616

17+
local ERROR = vim.log.levels.ERROR
1718
local util = require('triforce.util')
1819

1920
---@return LevelTitles titles
@@ -91,19 +92,29 @@ function Levels.get_all_levels(stats)
9192
return res
9293
end
9394

94-
---Get Zelda-themed title based on level
95+
---Get title mased on given level
9596
---@param level integer
9697
---@return string title
9798
function Levels.get_level_title(level)
9899
util.validate({ level = { level, { 'number' } } })
100+
if not util.is_int(level) then
101+
error('Parameter level is not an integer!', ERROR)
102+
end
99103

104+
local res_title = ''
105+
local max_lvl
100106
for lvl, title in pairs(Levels.levels) do
107+
max_lvl = lvl
101108
if level == lvl then
102-
return ('%s %s'):format(title.icon, title.title)
109+
res_title = ('%s %s'):format(title.icon, title.title)
110+
break
103111
end
104112
end
113+
if res_title == '' and level >= max_lvl then
114+
res_title = '💫 Eternal Legend' -- Max title for level > 300
115+
end
105116

106-
return '💫 Eternal Legend' -- Max title for level > 300
117+
return res_title
107118
end
108119

109120
return Levels

0 commit comments

Comments
 (0)