Skip to content

Commit e634134

Browse files
authored
feat(ui): display "update all" hint (#1296)
1 parent 9c5edf1 commit e634134

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

lua/mason/ui/components/main/package_list.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ local function PackageComponent(state, pkg, opts)
150150
}
151151
end
152152

153+
local get_outdated_packages_preview = _.if_else(
154+
_.compose(_.lte(4), _.size),
155+
_.compose(_.join ", ", _.map(_.prop "name")),
156+
_.compose(
157+
_.join ", ",
158+
_.converge(_.concat, {
159+
_.compose(_.map(_.prop "name"), _.take(3)),
160+
function(pkgs)
161+
return { ("and %d more…"):format(#pkgs - 3) }
162+
end,
163+
})
164+
)
165+
)
166+
153167
---@param state InstallerUiState
154168
local function Installed(state)
155169
return Ui.Node {
@@ -172,6 +186,19 @@ local function Installed(state)
172186
styling((" "):rep(new_versions_check.percentage_complete * 15)),
173187
}
174188
end),
189+
Ui.When(
190+
not state.packages.new_versions_check.is_checking and #state.packages.outdated_packages > 0,
191+
function()
192+
return Ui.VirtualTextNode {
193+
p.muted "Press ",
194+
p.highlight(settings.current.ui.keymaps.update_all_packages),
195+
p.muted " to update ",
196+
p.highlight(tostring(#state.packages.outdated_packages)),
197+
p.muted(#state.packages.outdated_packages > 1 and " packages " or " package "),
198+
p.Comment(("(%s)"):format(get_outdated_packages_preview(state.packages.outdated_packages))),
199+
}
200+
end
201+
),
175202
},
176203
packages = state.packages.installed,
177204
---@param pkg Package

lua/mason/ui/instance.lua

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ local INITIAL_STATE = {
7777
title_prefix = "", -- for animation
7878
},
7979
packages = {
80+
---@type Package[]
81+
outdated_packages = {},
8082
new_versions_check = {
8183
is_checking = false,
8284
current = 0,
@@ -271,6 +273,8 @@ local function setup_handle(handle)
271273
handle_spawnhandle_change()
272274
mutate_state(function(state)
273275
state.packages.states[handle.package.name] = create_initial_package_state()
276+
state.packages.outdated_packages =
277+
_.filter(_.complement(_.equals(handle.package)), state.packages.outdated_packages)
274278
end)
275279
end
276280

@@ -426,7 +430,7 @@ local function check_new_package_version(pkg)
426430
mutate_state(function(state)
427431
state.packages.states[pkg.name].is_checking_new_version = true
428432
end)
429-
a.wait(function(resolve, reject)
433+
return a.wait(function(resolve, reject)
430434
pkg:check_new_version(function(success, new_version)
431435
mutate_state(function(state)
432436
state.packages.states[pkg.name].is_checking_new_version = false
@@ -467,6 +471,7 @@ local function check_new_visible_package_versions()
467471
)(state.packages.installed)
468472

469473
mutate_state(function(state)
474+
state.packages.outdated_packages = {}
470475
state.packages.new_versions_check.is_checking = true
471476
state.packages.new_versions_check.current = 0
472477
state.packages.new_versions_check.total = #installed_visible_packages
@@ -488,11 +493,14 @@ local function check_new_visible_package_versions()
488493
a.wait_all(_.map(function(package)
489494
return function()
490495
local permit = sem:acquire()
491-
pcall(check_new_package_version, package)
496+
local has_new_version = pcall(check_new_package_version, package)
492497
mutate_state(function(state)
493498
state.packages.new_versions_check.current = state.packages.new_versions_check.current + 1
494499
state.packages.new_versions_check.percentage_complete = state.packages.new_versions_check.current
495500
/ state.packages.new_versions_check.total
501+
if has_new_version then
502+
table.insert(state.packages.outdated_packages, package)
503+
end
496504
end)
497505
permit:forget()
498506
end
@@ -551,14 +559,12 @@ end
551559

552560
local function update_all_packages()
553561
local state = get_state()
554-
_.each(
555-
function(pkg)
556-
pkg:install(pkg)
557-
end,
558-
_.filter(function(pkg)
559-
return state.packages.visible[pkg.name] and state.packages.states[pkg.name].new_version
560-
end, state.packages.installed)
561-
)
562+
_.each(function(pkg)
563+
pkg:install(pkg)
564+
end, state.packages.outdated_packages)
565+
mutate_state(function(state)
566+
state.packages.outdated_packages = {}
567+
end)
562568
end
563569

564570
local function toggle_install_log(event)

0 commit comments

Comments
 (0)