Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielkonge committed Dec 4, 2023
1 parent f9e7e52 commit 63fe57b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
2 changes: 2 additions & 0 deletions docs/config/lua/wezterm.table/clone.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ assert(equal(tbl, tbl_top_clone))
assert(equal(tbl, tbl_deep_clone))

tbl.a = 2
assert(equal(tbl, tbl_ref))
assert(tbl_ref.a == 2)
assert(not equal(tbl, tbl_top_clone))
assert(tbl_top_clone.a == 1)
assert(not equal(tbl, tbl_deep_clone))
Expand Down
10 changes: 6 additions & 4 deletions docs/config/lua/wezterm.table/extend.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ local tbl1 = {
b = {
d = 4,
},
c = 3,
e = 3,
}

local tbl2 = {
Expand All @@ -51,14 +51,16 @@ local tbl3 = {
e = 5,
}

assert(equal(extend { tbl1, tbl2 }, { a = 2, b = { e = 5 }, c = 3, d = 4 }))
assert(equal(extend { tbl1, tbl2 }, { a = 2, b = { e = 5 }, e = 3, d = 4 }))
assert(
equal(
extend({ tbl1, tbl2 }, 'Keep'),
{ a = 1, b = { d = 4 }, c = 3, d = 4 }
{ a = 1, b = { d = 4 }, e = 3, d = 4 }
)
)
-- This will return an error: extend({tbl1, tbl2}, 'Error')
local ok, msg = pcall(function() extend({tbl1, tbl3}, 'Error') end)
assert(not ok and
msg == "error runtime error: The key 'e' is in more than one of the tables.")

assert(equal(extend { tbl2, tbl3 }, { a = 2, b = { e = 5 }, d = 4, e = 5 }))
assert(
Expand Down
24 changes: 15 additions & 9 deletions docs/config/lua/wezterm.table/get.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

{{since('nightly')}}

This function accepts a Lua table `table` and a key `key`.
It returns the value at the table entry `key` if `table` contains a key equal
to `key` (with non-nil value) and it returns `nil` otherwise.
This function can be used to resolve the value for a key in a table. In its most basic form
it is equivalent to the built-in table indexing operator:
```lua
assert(wezterm.table.get(tbl, key) == tbl[key])
```
You may pass a sequence of keys that will be used to successively resolve
nested tables:
```lua
wezterm.table.get(tbl, 'a', 'b', 'c') == tbl['a']['b']['c']
```

*Note:* In the above `tbl['a']['b']['c']` might cause an error, since we might be indexing a nil value,
but `wezterm.table.get(tbl, 'a', 'b', 'c')` won't error in this case; instead it will return nil.

The function accepts an optional arbitrary number of extra arguments, that will
all be intepreted as extra keys to check for recursively in the table. I.e., to
get the value of `table` at `table.a.b.c`, we can use
`wezterm.table.get(table, 'a', 'b', 'c')`.

```lua
local wezterm = require 'wezterm'
local has_key = wezterm.table.has_key
local get = wezterm.table.get

local tbl1 = {
a = 1,
Expand All @@ -27,7 +33,7 @@ local tbl1 = {
local arr1 = { 'a', 'b', 'c' }

assert(get(tbl1, 'a') == 1)
assert(get(tbl1, 'b') == tbl1.b) -- note: we get the table address of tbl1.b here
assert(get(tbl1, 'b') == tbl1.b) -- note: we get the table reference of tbl1.b here
assert(get(tbl1, 'b', 'c', 'd') == 4)
assert(get(tbl1, 'c') == nil)

Expand Down
4 changes: 2 additions & 2 deletions lua-api-crates/table-funcs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn extend<'lua>(
tbl.set(key, value)?;
} else if behavior == ConflictMode::Error {
return Err(mlua::Error::runtime(format!(
"The key {} is in more than one of the tables.",
"The key '{}' is in more than one of the tables.",
key.to_string()?
)));
}
Expand Down Expand Up @@ -106,7 +106,7 @@ fn deep_extend<'lua>(
tbl.set(key, value)?;
} else if behavior == ConflictMode::Error {
return Err(mlua::Error::runtime(format!(
"The key {} is in more than one of the tables.",
"The key '{}' is in more than one of the tables.",
key.to_string()?
)));
}
Expand Down

0 comments on commit 63fe57b

Please sign in to comment.