From 4401f33c19f6f63371b9d79bf38b0cd8c30ee592 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Sat, 19 Dec 2020 23:53:29 +0100 Subject: [PATCH] Move HOME expand/encode into common module --- data/core/commands/core.lua | 35 +++++++++++++---------------------- data/core/common.lua | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/data/core/commands/core.lua b/data/core/commands/core.lua index 404642c3..6457601e 100644 --- a/data/core/commands/core.lua +++ b/data/core/commands/core.lua @@ -7,31 +7,22 @@ local LogView = require "core.logview" local fullscreen = false -local function home_encode(paths) - if not HOME then return paths end - local home = HOME - local t = {} - local n = #home - for i = 1, #paths do - if paths[i]:sub(1, n) == home and paths[i]:sub(n + 1, n + 1):match("[/\\\\]") then - t[i] = "~" .. paths[i]:sub(#home + 1) - else - t[i] = paths[i] - end - end - return t -end - -local function home_expand(text) +local function home_encode_list(paths) if HOME then - return text:gsub("^~", HOME) + local t = {} + for i = 1, #paths do + t[i] = common.home_encode(paths[i]) + end + return t end - return text + return paths end local function suggest_directory(text) - text = home_expand(text) - return home_encode(text == "" and core.recent_projects or common.dir_path_suggest(text)) + if HOME then + text = common.home_expand(text) + end + return home_encode_list(text == "" and core.recent_projects or common.dir_path_suggest(text)) end command.add(nil, { @@ -153,7 +144,7 @@ command.add(nil, { ["core:change-project-folder"] = function() core.command_view:enter("Change Project Folder", function(text) - text = home_expand (text) + text = common.home_expand(text) local path_stat = system.get_file_info(text) if not path_stat or path_stat.type ~= 'dir' then core.error("Cannot open folder %q", text) @@ -167,7 +158,7 @@ command.add(nil, { ["core:open-project-folder"] = function() core.command_view:enter("Open Project", function(text) - text = home_expand (text) + text = common.home_expand(text) local path_stat = system.get_file_info(text) if not path_stat or path_stat.type ~= 'dir' then core.error("Cannot open folder %q", text) diff --git a/data/core/common.lua b/data/core/common.lua index 659cf244..eabfeedd 100644 --- a/data/core/common.lua +++ b/data/core/common.lua @@ -152,4 +152,18 @@ function common.bench(name, fn, ...) end +function common.home_encode(text) + local n = #HOME + if text:sub(1, n) == HOME and text:sub(n + 1, n + 1):match("[/\\\\]") then + return "~" .. text:sub(n + 1) + end + return text +end + + +function common.home_expand(text) + return text:gsub("^~", HOME) +end + + return common