1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-23 05:12:40 +03:00

lua: add wezterm.procinfo module

This commit is contained in:
Wez Furlong 2022-07-16 08:49:58 -07:00
parent ec65b64f30
commit 5e60743519
13 changed files with 149 additions and 5 deletions

1
.gitignore vendored
View File

@ -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

13
Cargo.lock generated
View File

@ -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"

View File

@ -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",

View File

@ -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)

View File

@ -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"
```

View File

@ -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"
```

View File

@ -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",
}
```

View File

@ -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

View File

@ -0,0 +1,6 @@
# `wezterm.procinfo.pid()`
*Since: nightly builds only*
Returns the process id for the current process.

View File

@ -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" }

View File

@ -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,

View File

@ -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" }

View File

@ -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(())
}