From 5e6074351987a4cdf76d0fbee69996d6da7949d7 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 16 Jul 2022 08:49:58 -0700 Subject: [PATCH] lua: add wezterm.procinfo module --- .gitignore | 1 + Cargo.lock | 13 +++++++ ci/generate-docs.py | 4 ++ docs/changelog.md | 11 +++--- .../current_working_dir_for_pid.md | 13 +++++++ .../executable_path_for_pid.md | 13 +++++++ .../lua/wezterm.procinfo/get_info_for_pid.md | 39 +++++++++++++++++++ .../lua/wezterm.procinfo/index.markdown | 8 ++++ docs/config/lua/wezterm.procinfo/pid.md | 6 +++ env-bootstrap/Cargo.toml | 1 + env-bootstrap/src/lib.rs | 1 + lua-api-crates/procinfo-funcs/Cargo.toml | 14 +++++++ lua-api-crates/procinfo-funcs/src/lib.rs | 30 ++++++++++++++ 13 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 docs/config/lua/wezterm.procinfo/current_working_dir_for_pid.md create mode 100644 docs/config/lua/wezterm.procinfo/executable_path_for_pid.md create mode 100644 docs/config/lua/wezterm.procinfo/get_info_for_pid.md create mode 100644 docs/config/lua/wezterm.procinfo/index.markdown create mode 100644 docs/config/lua/wezterm.procinfo/pid.md create mode 100644 lua-api-crates/procinfo-funcs/Cargo.toml create mode 100644 lua-api-crates/procinfo-funcs/src/lib.rs diff --git a/.gitignore b/.gitignore index d8c6bf504..3d43e969d 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ /docs/config/lua/wezterm.color/index.md /docs/config/lua/wezterm.gui/index.md /docs/config/lua/wezterm.mux/index.md +/docs/config/lua/wezterm.procinfo/index.md /docs/config/lua/wezterm/index.md /docs/config/lua/window-events/index.md /docs/config/lua/window/index.md diff --git a/Cargo.lock b/Cargo.lock index d922e70f1..3a68b763a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1138,6 +1138,7 @@ dependencies = [ "logging", "mux-lua", "objc", + "procinfo-funcs", "share-data", "spawn-funcs", "ssh-funcs", @@ -3305,6 +3306,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "procinfo-funcs" +version = "0.1.0" +dependencies = [ + "anyhow", + "config", + "libc", + "luahelper", + "procinfo", + "wezterm-dynamic", +] + [[package]] name = "promise" version = "0.2.0" diff --git a/ci/generate-docs.py b/ci/generate-docs.py index 5b4f18ac0..0360d1eb8 100644 --- a/ci/generate-docs.py +++ b/ci/generate-docs.py @@ -338,6 +338,10 @@ TOC = [ "module: wezterm.mux", "config/lua/wezterm.mux", ), + Gen( + "module: wezterm.procinfo", + "config/lua/wezterm.procinfo", + ), Gen( "struct: Config", "config/lua/config", diff --git a/docs/changelog.md b/docs/changelog.md index d50269384..919432123 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -18,14 +18,15 @@ As features stabilize some brief notes about them will accumulate here. * [window:set_position](config/lua/window/set_position.md) method for controlling window position. * [window:maximize](config/lua/window/maximize.md) and [window:restore](config/lua/window/restore.md) methods for controlling window maximization state. * [window:get_selection_escapes_for_pane](config/lua/window/get_selection_escapes_for_pane.md) method for getting the current selection including escape sequences. [#2223](https://github.com/wez/wezterm/issues/2223) -* New [wezterm.color](config/lua/wezterm.color/index.md) module for working with colors and importing color schemes. -* New [wezterm.gui](config/lua/wezterm.gui/index.md) module and [mux_window:gui_window](config/lua/mux-window/gui_window.md) method. -* New [wezterm.gui.screens()](config/lua/wezterm.gui/screens.md) function for getting information about the available screens/monitors/displays -* New [wezterm.gui.get_appearance()](config/lua/wezterm.gui/get_appearance.md) function for a simpler way to get system dark mode state +* [wezterm.color](config/lua/wezterm.color/index.md) module for working with colors and importing color schemes. +* [wezterm.gui](config/lua/wezterm.gui/index.md) module and [mux_window:gui_window](config/lua/mux-window/gui_window.md) method. +* [wezterm.gui.screens()](config/lua/wezterm.gui/screens.md) function for getting information about the available screens/monitors/displays +* [wezterm.gui.get_appearance()](config/lua/wezterm.gui/get_appearance.md) function for a simpler way to get system dark mode state +* [wezterm.procinfo](config/lua/wezterm.procinfo/index.md) module for querying local process information. * You may now use [wezterm.format](config/lua/wezterm/format.md) (or otherwise use strings with escape sequences) in the labels of the [Launcher Menu](config/launch.md#the-launcher-menu). * You may now specify `assume_emoji_presentation = true` (or `false`) in [wezterm.font()](config/lua/wezterm/font.md) and [wezterm.font_with_fallback()](config/lua/wezterm/font_with_fallback.md) * Wayland: `zwp_text_input_v3` is now supported, which enables IME to work in wezterm if your compositor also implements this protocol. -* New [wezterm.json_parse()](config/lua/wezterm/json_parse.md) and [wezterm.json_encode()](config/lua/wezterm/json_encode.md) functions for working with JSON. +* [wezterm.json_parse()](config/lua/wezterm/json_parse.md) and [wezterm.json_encode()](config/lua/wezterm/json_encode.md) functions for working with JSON. * Hundreds of new color schemes have been imported from [base16](https://github.com/chriskempson/base16-schemes-source), [Gogh](https://gogh-co.github.io/Gogh/) and [terminal.sexy](https://terminal.sexy/). [Browse the schemes](colorschemes/index.md) and look for themes with `(base16)`, `(Gogh)` and `(terminal.sexy)` in the name to discover them! * [pane:is_alt_screen_active()](config/lua/pane/is_alt_screen_active.md) for testing whether the alt screen is active. Thanks to [@Funami580](https://github.com/Funami580)! [#2234](https://github.com/wez/wezterm/issues/2234) * X11/Wayland: [XDG desktop portal](https://flatpak.github.io/xdg-desktop-portal/) is now used to determine whether dark mode is in use [#2258](https://github.com/wez/wezterm/issues/2258) diff --git a/docs/config/lua/wezterm.procinfo/current_working_dir_for_pid.md b/docs/config/lua/wezterm.procinfo/current_working_dir_for_pid.md new file mode 100644 index 000000000..768c6be80 --- /dev/null +++ b/docs/config/lua/wezterm.procinfo/current_working_dir_for_pid.md @@ -0,0 +1,13 @@ +# `wezterm.procinfo.current_working_dir_for_pid(pid)` + +*Since: nightly builds only* + +Returns the current working directory for the specified process id. + +This function may return `nil` if it was unable to return the info. + +```lua +> wezterm.procinfo.current_working_dir_for_pid(wezterm.procinfo.pid()) +"/home/wez/wez-personal/wezterm" +``` + diff --git a/docs/config/lua/wezterm.procinfo/executable_path_for_pid.md b/docs/config/lua/wezterm.procinfo/executable_path_for_pid.md new file mode 100644 index 000000000..bc3557c8e --- /dev/null +++ b/docs/config/lua/wezterm.procinfo/executable_path_for_pid.md @@ -0,0 +1,13 @@ +# `wezterm.procinfo.executable_path_for_pid(pid)` + +*Since: nightly builds only* + +Returns the path to the executable image for the specified process id. + +This function may return `nil` if it was unable to return the info. + +```lua +> wezterm.procinfo.executable_path_for_pid(wezterm.procinfo.pid()) +"/home/wez/wez-personal/wezterm/target/debug/wezterm-gui" +``` + diff --git a/docs/config/lua/wezterm.procinfo/get_info_for_pid.md b/docs/config/lua/wezterm.procinfo/get_info_for_pid.md new file mode 100644 index 000000000..88414f1f1 --- /dev/null +++ b/docs/config/lua/wezterm.procinfo/get_info_for_pid.md @@ -0,0 +1,39 @@ +# `wezterm.procinfo.get_info_for_pid(pid)` + +*Since: nightly builds only* + +Returns a [LocalProcessInfo](../LocalProcessInfo.md) object for the specified +process id. + +This function may return `nil` if it was unable to return the info. + +```lua +> wezterm.procinfo.get_info_for_pid(wezterm.procinfo.pid()) +{ + "argv": [ + "/home/wez/wez-personal/wezterm/target/debug/wezterm-gui", + ], + "children": { + 540513: { + "argv": [ + "-zsh", + ], + "children": {}, + "cwd": "/home/wez", + "executable": "/usr/bin/zsh", + "name": "zsh", + "pid": 540513, + "ppid": 540450, + "start_time": 232656896, + "status": "Sleep", + }, + }, + "cwd": "/home/wez/wez-personal/wezterm", + "executable": "/home/wez/wez-personal/wezterm/target/debug/wezterm-gui", + "name": "wezterm-gui", + "pid": 540450, + "ppid": 425276, + "start_time": 8671498240, + "status": "Run", +} +``` diff --git a/docs/config/lua/wezterm.procinfo/index.markdown b/docs/config/lua/wezterm.procinfo/index.markdown new file mode 100644 index 000000000..6bb846177 --- /dev/null +++ b/docs/config/lua/wezterm.procinfo/index.markdown @@ -0,0 +1,8 @@ +*Since: nightly builds only* + +The `wezterm.procinfo` module exposes functions that allow querying information +about processes that are running on the local system. + +## Available functions + + diff --git a/docs/config/lua/wezterm.procinfo/pid.md b/docs/config/lua/wezterm.procinfo/pid.md new file mode 100644 index 000000000..65ef324ec --- /dev/null +++ b/docs/config/lua/wezterm.procinfo/pid.md @@ -0,0 +1,6 @@ +# `wezterm.procinfo.pid()` + +*Since: nightly builds only* + +Returns the process id for the current process. + diff --git a/env-bootstrap/Cargo.toml b/env-bootstrap/Cargo.toml index 3a125de1c..4363fb15e 100644 --- a/env-bootstrap/Cargo.toml +++ b/env-bootstrap/Cargo.toml @@ -21,6 +21,7 @@ color-funcs = { path = "../lua-api-crates/color-funcs" } termwiz-funcs = { path = "../lua-api-crates/termwiz-funcs" } logging = { path = "../lua-api-crates/logging" } mux-lua = { path = "../lua-api-crates/mux" } +procinfo-funcs = { path = "../lua-api-crates/procinfo-funcs" } filesystem = { path = "../lua-api-crates/filesystem" } json = { path = "../lua-api-crates/json" } share-data = { path = "../lua-api-crates/share-data" } diff --git a/env-bootstrap/src/lib.rs b/env-bootstrap/src/lib.rs index d32bcb2f0..0cf62cbba 100644 --- a/env-bootstrap/src/lib.rs +++ b/env-bootstrap/src/lib.rs @@ -165,6 +165,7 @@ fn register_lua_modules() { termwiz_funcs::register, logging::register, mux_lua::register, + procinfo_funcs::register, filesystem::register, json::register, ssh_funcs::register, diff --git a/lua-api-crates/procinfo-funcs/Cargo.toml b/lua-api-crates/procinfo-funcs/Cargo.toml new file mode 100644 index 000000000..acd7a6535 --- /dev/null +++ b/lua-api-crates/procinfo-funcs/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "procinfo-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" } +libc = "0.2" +luahelper = { path = "../../luahelper" } +procinfo = { path = "../../procinfo" } +wezterm-dynamic = { path = "../../wezterm-dynamic" } diff --git a/lua-api-crates/procinfo-funcs/src/lib.rs b/lua-api-crates/procinfo-funcs/src/lib.rs new file mode 100644 index 000000000..ddd0fe80a --- /dev/null +++ b/lua-api-crates/procinfo-funcs/src/lib.rs @@ -0,0 +1,30 @@ +use config::lua::get_or_create_sub_module; +use config::lua::mlua::Lua; +use procinfo::LocalProcessInfo; + +pub fn register(lua: &Lua) -> anyhow::Result<()> { + let proc_mod = get_or_create_sub_module(lua, "procinfo")?; + proc_mod.set( + "pid", + lua.create_function(|_, _: ()| Ok(unsafe { libc::getpid() }))?, + )?; + proc_mod.set( + "get_info_for_pid", + lua.create_function(|_, pid: u32| Ok(LocalProcessInfo::with_root_pid(pid)))?, + )?; + proc_mod.set( + "current_working_dir_for_pid", + lua.create_function(|_, pid: u32| { + Ok(LocalProcessInfo::current_working_dir(pid) + .and_then(|p| p.to_str().map(|s| s.to_string()))) + })?, + )?; + proc_mod.set( + "executable_path_for_pid", + lua.create_function(|_, pid: u32| { + Ok(LocalProcessInfo::executable_path(pid) + .and_then(|p| p.to_str().map(|s| s.to_string()))) + })?, + )?; + Ok(()) +}