Skip to content

Commit 4fab203

Browse files
authored
Consolidate globals in main stackline.lua file. Remove unnecessary custom table.insert() fn (#77)
Potential fix for #76
1 parent d24a040 commit 4fab203

File tree

6 files changed

+13
-44
lines changed

6 files changed

+13
-44
lines changed

lib/utils.lua

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22
local log = hs.logger.new('utils', 'info')
33
log.i('Loading module: utils')
44

5-
local function length(t) -- {{{
6-
if type(t)~='table' then return 0 end
7-
local count = 0
8-
for _ in pairs(t) do
9-
count = count + 1
10-
end
11-
return count
12-
end -- }}}
13-
145
-- === Extend builtins ===
156
function string:split(p) -- {{{
167
-- Splits the string [s] into substrings wherever pattern [p] occurs.
@@ -47,32 +38,6 @@ function string:trim() -- {{{
4738
:gsub('%s+$', '') -- trim trailing whitespace
4839
end -- }}}
4940

50-
table.rawinsert = table.insert -- Store the OG fn just in case
51-
52-
table.insert = function(tbl, key, val) -- luacheck: ignore 122 {{{
53-
if (tbl==nil) then return {} end -- bail right away if tbl is nil
54-
55-
local insert = table.rawinsert -- luacheck: ignore 143
56-
local no_val = val==nil
57-
local numeric_key = type(key)=='number'
58-
local is_array = type(tbl)=='table' and tbl[1]~=nil and tbl[length(tbl)]~=nil
59-
60-
if no_val then
61-
val = key -- We must only have *two* args,so val is actually *key*
62-
key = #tbl + 1 -- set a sequntial numeric key to append to array-like tbl
63-
end
64-
65-
if is_array or numeric_key then
66-
key = (key < 0) and (#tbl+2+key) or key -- table.insert(x,0,'val') -> insert last
67-
key = math.min(key, #tbl+1) -- handle key that's out of bounds
68-
insert(tbl, key, val) -- insert via OG table.insert
69-
else
70-
tbl[key] = val
71-
end
72-
73-
return tbl -- unlike OG table.insert, you get the table back
74-
end -- }}}
75-
7641
function table.slice(t, from, to) -- {{{
7742
-- Returns a partial table sliced from t, equivalent to t[x:y] in certain languages.
7843
-- Negative indices will be used to access the table from the other end.
@@ -99,9 +64,7 @@ function table.slice(t, from, to) -- {{{
9964
end -- }}}
10065

10166
-- === utils module ===
102-
_G.u = {} -- access utils under global 'u'
103-
104-
u.length = length
67+
local u = {}
10568

10669
-- Alias hs.fnutils methods
10770
u.map = hs.fnutils.map
@@ -118,6 +81,15 @@ u.copy = hs.fnutils.copy
11881
u.sortByKeys = hs.fnutils.sortByKeys
11982
u.sortByValues = hs.fnutils.sortByKeyValues
12083

84+
function u.length(t) -- {{{
85+
if type(t)~='table' then return 0 end
86+
local count = 0
87+
for _ in next, t do
88+
count = count + 1
89+
end
90+
return count
91+
end -- }}}
92+
12193
function u.reverse(tbl) -- {{{
12294
-- Reverses values in a given array. The passed-in array should not be sparse.
12395
local res = {}

stackline/configmanager.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- https://github.com/erento/lua-schema-validation
22
local log = hs.logger.new('configmgr', 'info')
3-
local v = require 'stackline.lib.valid' -- Validators & type lookup
3+
local v = require 'lib.valid' -- Validators & type lookup
44
local o = v.optional
55

66
local is_color = v.is_table {

stackline/query.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local u = require 'stackline.lib.utils'
21
local log = hs.logger.new('query', 'info')
32
log.i('Loading module: query')
43

stackline/stack.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local u = require 'stackline.lib.utils'
2-
31
local Stack = {}
42

53
function Stack:new(stackedWindows) -- {{{

stackline/stackline.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
-- luacheck: globals table.merge
22
-- luacheck: globals u
3-
--luacheck: ignore 112
3+
-- luacheck: ignore 112
44
local wf = hs.window.filter
55
local timer = hs.timer.delayed
66
local log = hs.logger.new('stackline', 'info')
77
local click = hs.eventtap.event.types['leftMouseDown'] -- fyi, print hs.eventtap.event.types to see all event types
88

99
log.i'Loading module: stackline'
10+
_G.u = require 'lib.utils'
1011
_G.stackline = {} -- access stackline under global 'stackline'
1112
stackline.config = require'stackline.configmanager'
1213
stackline.window = require'stackline.window'

stackline/window.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local u = require 'stackline.lib.utils'
21
local log = hs.logger.new('window', 'info')
32
log.i('Loading module: window')
43

0 commit comments

Comments
 (0)