-
-
Notifications
You must be signed in to change notification settings - Fork 805
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
base: main
Are you sure you want to change the base?
Conversation
I think this is ready for feedback now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this!
Sorry that it's taken a while to get around to reviewing it!
For https://github.com/kikito/inspect.lua |
Co-authored-by: Wez Furlong <[email protected]>
I guess it depends what people use |
Since wezterm has custom embedded types that have their own string representation, it is important that what we provide for this string representation is:
I think that means that we should stick to the |
For the last commit, it is only meant to be temporary for now, if you want to try out |
This commit changes how lua Strings are rendered when printing them through ValuePrinter. Previously, we'd try to convert to a utf8 string and print the bare string to the output. If it failed, we'd get a less than useful output; for this example when inspecting the the `utf8` global module from the debug overlay: ``` > utf8.charpattern (error converting Lua string to &str (invalid utf-8 sequence of 1 bytes from index 4)) ``` Now we handle the failure case and show it as a binary string using a somewhat invented syntax; the `b"string"` syntax isn't valid in lua, but it helps to communicate that this is a binary string: ``` > utf8.charpattern b"[\x00-\x7f\xc2-\xfd][\x80-\xbf]*" ``` in addition, we now quote and escape unicode strings. Previously; ``` > wezterm.target_triple x86_64-unknown-linux-gnu ``` now: ``` > wezterm.target_triple "x86_64-unknown-linux-gnu" ``` refs: #4336
re: |
Another thing to consider for the We could just keep the |
Perhaps it would even be possible to return the value at the key path if it is set? I don't know how well rust interacts with multiple return types (can you use Result with a tuple?). local has, color = has_key(config, 'copy_mode_active_highlight_bg', 'Color')
if has then
-- do something with color
else
-- color is nil, do something else
end Since the rust code already has to traverse the entire table path, this would only add one |
We could add a functionality like that too, but wouldn't this make more sense to put into its own function called something like |
Also removed the to_string docs and renamed length to count.
Note: I haven't tested this code yet.
I added optional extra keys to |
The docs are mostly fixed now, and I have cleaned up a few other things. @wez: In |
// we don't need a clone in this case | ||
for pair in table.pairs::<LuaValue, LuaValue>() { | ||
let (_, tbl_value) = pair?; | ||
if tbl_value == value { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these equality operations use whatever ==
maps to in rust, or should these be calling lua_value_eq
?
Probably not always. See my comment below about closures. I don't really want to go down a rabbit hole on that in this PR, so my suggestion would be: just keep this with the shallow level search for this PR and then you can revisit in a follow up if you're feeling motivated!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will just remove the behavior parameter for now then, and only support a shallow search in this PR.
As to ==
, I think this should be equivalent to mlua's .eq()
, but I can change it to that to make it clearer what is going on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or do you mean .equals()
? I think that is a custom method whereas .eq()
is just derived.
} | ||
} | ||
} | ||
DepthMode::Deep => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH, I'm not sure about this Deep value search operation. I can't think of a single time I've needed something like this in 25+ years. The closest I've come is a DFS on a graph, but in that situation I've needed to apply a closure rather than look for a full value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can just remove it to simplify the code for now. I thought it might be useful to see if certain values were precent in one's config, but in that case you would usually know what key it should be set for anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually looking at the updated code, I don't really think it adds much extra. Though I do agree that it will probably only be rarely used. Do you prefer to just remove it?
I can't quite read the source for "impl_lua_conversion_dynamic!" well enough to know if it already is, but is it possible to make the behavior enums case insensitive? if not then don't worry about it, but since we're using behavior strings and these won't be accessible as enums from lua, i'd prefer (personally) to use lowercase strings. case insensitive would provide the best of both worlds, as well as ensuring that users don't have to remember whether each method needs capitalized or lowercase strings. |
Note: This can probably be written a bit nicer later.
I think I have addressed most of the feedback now, but I still need to edit the following:
|
There are probably a few minor things left to edit in the code, but I think it is close enough that I can start to write some tests. @wez is there any other place in the code that does testing of similar Lua function implementations that I can base my tests on? Otherwise I will have to spend a little time and read the mlua docs more carefully. Update: I have added a couple of example tests now. Please let me know if they look okay or if you would want a different kind of test setup? Then I can continue writing more tests. Also, looking back at the code I made an observation: |
@wez I kind of forgot about this. Could you give some feedback on the last two comments? Then I can go through and clean this up during this weekend (and maybe some of next week). |
I will add documentation later, but here is a first draft of some functions for working with tables in lua. This gives some of what was asked for in #4328.