Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding some functions to work with Lua tables. #4336

Open
wants to merge 47 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
514384d
First draft of some table functions.
Sep 25, 2023
f3a6754
Added more table functions + docs.
Sep 25, 2023
ec10ee2
Fixed some typos.
Sep 26, 2023
373386a
Improved flatten and added an option to to_string.
Sep 28, 2023
3e93a48
Merge branch 'main' into tables
Danielkonge Oct 12, 2023
b7a8fc7
Merge branch 'wez:main' into tables
Danielkonge Oct 17, 2023
9292fe0
Added to_string_fallback
Oct 23, 2023
4d48a37
Merge remote-tracking branch 'origin/tables' into tables
Oct 23, 2023
1a00a87
Cargo.lock update
Oct 23, 2023
bfc61a5
Fixed formatting
Oct 23, 2023
87eb457
Simplify flatten
Danielkonge Nov 7, 2023
476e5e2
Update formatting
Danielkonge Nov 24, 2023
b16c573
Apply some suggestions from code review
Danielkonge Dec 1, 2023
0acacad
Simplify `length`
Danielkonge Dec 1, 2023
e42b523
simple `to_string` with `ValuePrinter`
Danielkonge Dec 1, 2023
bd12755
Cleanup errors
Danielkonge Dec 1, 2023
38f0c02
format
Danielkonge Dec 1, 2023
8567fc6
Rename `merge` to `extend` + add `deep_extend`
Danielkonge Dec 1, 2023
cfe4705
`clone` fix + add `equal`
Danielkonge Dec 1, 2023
ee2ecbc
Update .gitignore
Danielkonge Dec 1, 2023
0a61ba0
Delete docs/config/lua/wezterm.table/index.md
Danielkonge Dec 1, 2023
4d634c6
Merge branch 'wez:main' into tables
Danielkonge Dec 1, 2023
716b20a
Improve `equal`
Danielkonge Dec 2, 2023
7e10b66
Cleanup `ConflictMode`
Danielkonge Dec 2, 2023
77e0b3f
Small changes to remove `.clone()` calls
Danielkonge Dec 2, 2023
a1d53c6
Apply some suggestions from code review
Danielkonge Dec 2, 2023
aff4221
Cleanup of to_string and extend functions
Danielkonge Dec 2, 2023
358d8b9
Add optional behavior to flatten and clone
Danielkonge Dec 2, 2023
4be77dc
Added optional extra keys to has_key
Danielkonge Dec 3, 2023
9a509e7
Minor cleanup
Danielkonge Dec 3, 2023
f06582d
Added `get` function
Danielkonge Dec 3, 2023
f76d8e2
Flatten fix + docs updates
Danielkonge Dec 3, 2023
a0cdfbe
Add behavior to `has_value`
Danielkonge Dec 3, 2023
4917e3d
Cleanup
Danielkonge Dec 3, 2023
764bbe0
Apply some suggestions from code review
Danielkonge Dec 3, 2023
f9e7e52
Rename Top to Shallow and simplify `has_value`
Danielkonge Dec 3, 2023
63fe57b
Cleanup
Danielkonge Dec 4, 2023
94de598
Merge branch 'wez:main' into tables
Danielkonge Dec 4, 2023
2d58f3c
Docs update
Danielkonge Dec 4, 2023
46fc06b
Update docs and simplify extend functions
Danielkonge Dec 4, 2023
4a4e3ac
Minor bug fix
Danielkonge Dec 4, 2023
6767325
Some cleanup
Danielkonge Dec 4, 2023
c0c3df4
Docs cleanup
Danielkonge Dec 4, 2023
796ea5f
More docs cleanup
Danielkonge Dec 4, 2023
76e1e70
`deep_extend` fix
Danielkonge Dec 5, 2023
3d766d4
Minor cleanup
Danielkonge Dec 5, 2023
34653a7
Add two example tests
Danielkonge Dec 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ci/generate-docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ def render(self, output, depth=0, mode="mdbook"):
"module: wezterm.color",
"config/lua/wezterm.color",
),
Gen(
"module: wezterm.table",
"config/lua/wezterm.table",
),
Gen(
"module: wezterm.gui",
"config/lua/wezterm.gui",
Expand Down
20 changes: 20 additions & 0 deletions docs/config/lua/wezterm.table/clone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# `wezterm.table.clone(table)`

{{since('nightly')}}

This function clones the Lua table (or array) passed to it.

```lua
local wezterm = require 'wezterm'
local clone = wezterm.table.clone

local tbl1 = {
a = 1,
b = '2',
}

local tbl2 = clone(tbl1)

assert(tbl1 == tbl2)
```

20 changes: 20 additions & 0 deletions docs/config/lua/wezterm.table/flatten.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# `wezterm.table.flatten(array_of_arrays)`

{{since('nightly')}}

This function recursively flattens Lua arrays passed to it in the form of an array.
I.e., to flatten the Lua arrays `arr1` and `arr2` into one array,
we can pass them to the function as `{ arr1, arr2 }`. (See below.)

```lua
local wezterm = require 'wezterm'
local flatten = wezterm.table.flatten

local arr1 = { { 1, 2 }, 3 }

local arr2 = { 'a', { 'b', { 'c' } } }

assert(flatten { arr1, arr2 } == { 1, 2, 3, 'a', 'b', 'c' })
```

See also [merge](merge.md).
24 changes: 24 additions & 0 deletions docs/config/lua/wezterm.table/has_key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# `wezterm.table.has_key(table, key)`

{{since('nightly')}}

This function accepts a Lua table (or array) `table` and a key `key`.
It returns `true` if `table` contains a key equal to `key` (with non-nill value)
and false otherwise.

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

local tbl1 = {
a = 1,
b = '2',
}
local arr1 = { 'a', 'b', 'c' }

assert(has_key(tbl1, 'a'))
assert(not has_key(tbl1, 'c'))

assert(has_key(arr1, 3))
assert(not has_key(arr1, 4))
```
24 changes: 24 additions & 0 deletions docs/config/lua/wezterm.table/has_value.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# `wezterm.table.has_value(table, value)`

{{since('nightly')}}

This function accepts a Lua table (or array) `table` and a value `value`.
It returns `true` if `table` contains an entry with value equal to `value`
and false otherwise.
Danielkonge marked this conversation as resolved.
Show resolved Hide resolved

```lua
local wezterm = require 'wezterm'
local has_value = wezterm.table.has_value

local tbl1 = {
a = 1,
b = '2',
}
local arr1 = { 'a', 'b', 'c' }

assert(has_value(tbl1, 1))
assert(not has_value(tbl1, 'a'))

assert(has_value(arr1, 'a'))
assert(not has_value(arr1, '1'))
```
7 changes: 7 additions & 0 deletions docs/config/lua/wezterm.table/index.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# `wezterm.table` module

{{since('nightly')}}

The `wezterm.table` module exposes functions that work with Lua tables and arrays.

## Available functions
17 changes: 17 additions & 0 deletions docs/config/lua/wezterm.table/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# `wezterm.table` module
Danielkonge marked this conversation as resolved.
Show resolved Hide resolved

{{since('nightly')}}

The `wezterm.table` module exposes functions that work with Lua tables and arrays.

## Available functions


- [clone](clone.md)
- [flatten](flatten.md)
- [has_key](has_key.md)
- [has_value](has_value.md)
- [length](length.md)
- [merge](merge.md)
- [to_string](to_string.md)
- [to_string_fallback](to_string_fallback.md)
26 changes: 26 additions & 0 deletions docs/config/lua/wezterm.table/length.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# `wezterm.table.length(table)`

{{since('nightly')}}

This function returns the length of a Lua table (or array) passed to it.

Note: The Lua function `#` also returns the length of an array, but
`#` only works for arrays and not tables (with non-integer keys).
Danielkonge marked this conversation as resolved.
Show resolved Hide resolved

```lua
local wezterm = require 'wezterm'
local length = wezterm.table.length

local tbl1 = {
a = 1,
b = '2',
}
local arr1 = { 1, 'a', 2, 'abc' }

assert(2 == length(tbl1))
assert(4 == length(arr1))

assert(0 == #tbl1)
assert(4 == #arr1)
```

36 changes: 36 additions & 0 deletions docs/config/lua/wezterm.table/merge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# `wezterm.table.merge(array_of_tables [, keep_first])`

{{since('nightly')}}

This function merges Lua tables passed to it in the form of an array.
Danielkonge marked this conversation as resolved.
Show resolved Hide resolved
I.e., to merge the Lua tables `tbl1` and `tbl2`, we can pass them to
the function as `{ tbl1, tbl2 }`. (See below.)

By default this function merges tables with identical keys by taking
the value from the last table in the array with each given key.

The optional `keep_first` allows us to instead prefer values from the
Danielkonge marked this conversation as resolved.
Show resolved Hide resolved
first table in the array where we see the key by passing `true` after the array.
The default behavior is identical to what we get by passing `false`.

```lua
local wezterm = require 'wezterm'
local merge = wezterm.table.merge

local tbl1 = {
a = 1,
b = '2',
}

local tbl2 = {
a = '1',
c = 3,
}

wezterm.log_error(merge { tbl1, tbl2 })
Danielkonge marked this conversation as resolved.
Show resolved Hide resolved
assert(merge { tbl1, tbl2 } == merge({ tbl1, tbl2 }, false))

wezterm.log_error(merge({ tbl1, tbl2 }, true))
```

See also [flatten](flatten.md).
66 changes: 66 additions & 0 deletions docs/config/lua/wezterm.table/to_string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# `wezterm.table.to_string(table [, indent [, skip_outer_bracket]])`

{{since('nightly')}}

This function takes a Lua table and returns a string with the data of
Danielkonge marked this conversation as resolved.
Show resolved Hide resolved
the table. E.g., passing in the table `{ a=1, b=2 }` the function
will return the string:
```
{
a = 1,
b = 2,
}
```

*Note:* This function is not careful about checking for recursive tables, so it can't
be used to print e.g. `_G`. To print a recursive (or in general a very big) table,
it is recommended that you use [to_string_fallback](to_string_fallback.md).

By default this function constructs the string with 2 spaces for indentation.

The optional `indent` allows us to instead prefer other (non-negative) integer values
of spaces for the indentation.

```lua
local wezterm = require 'wezterm'
local tbl_to_string = wezterm.table.to_string

local tbl1 = {
a = 1,
{
b = 2,
},
}
local str1 = [[{
a = 1,
{
b = 2,
},
}]]

assert(str1 == tbl_to_string(tbl1, 4))
```

The optional `skip_outer_bracket` (which can only be used together with `indent`) is
a boolean, which defaults to `false`. If you set it to `true`, the outer brackets are
not included in the string (and thus everything is `indent` fewer spaces indented too).

```lua
local wezterm = require 'wezterm'
local tbl_to_string = wezterm.table.to_string

local tbl1 = {
a = 1,
{
b = 2,
},
}
local str1 = [[a = 1,
{
b = 2,
},]]

assert(str1 == tbl_to_string(tbl1, 0, true))
```

See also [to_string_fallback](to_string_fallback.md).
37 changes: 37 additions & 0 deletions docs/config/lua/wezterm.table/to_string_fallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# `wezterm.table.to_string_fallback(table)`

{{since('nightly')}}

This function takes a Lua table and returns a string with the data of
the table. E.g., passing in the table `{ a=1, b=2 }` the function
will return the string:
```
{
["a"] = 1,
["b"] = 2,
}
```

For nested tables, this function always prints a label (even for arrays).
This can make the string look different than you might expect.
```lua
local wezterm = require 'wezterm'
local tbl_to_string_fb = wezterm.table.to_string_fallback

local tbl1 = {
a = 1,
{
b = 2,
},
}
local str1 = [[{
[1] = {
["b"] = 2,
},
["a"] = 1,
}]]

assert(str1 == tbl_to_string_fb(tbl1))
```

See also [to_string](to_string.md).
1 change: 1 addition & 0 deletions env-bootstrap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ssh-funcs = { path = "../lua-api-crates/ssh-funcs" }
spawn-funcs = { path = "../lua-api-crates/spawn-funcs" }
time-funcs = { path = "../lua-api-crates/time-funcs" }
url-funcs = { path = "../lua-api-crates/url-funcs" }
table-funcs = { path = "../lua-api-crates/table-funcs" }
wezterm-version = { path = "../wezterm-version" }

[target."cfg(windows)".dependencies]
Expand Down
1 change: 1 addition & 0 deletions env-bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ fn register_lua_modules() {
share_data::register,
time_funcs::register,
url_funcs::register,
table_funcs::register,
] {
config::lua::add_context_setup_func(func);
}
Expand Down
11 changes: 11 additions & 0 deletions lua-api-crates/table-funcs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "table-funcs"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0"
config = { path = "../../config" }
luahelper = { path = "../../luahelper" }
Loading
Loading