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

lua: print -> log_info

Make print equivalent to log_info.
Improve the various log functions so that all of their arguments
are considered.

refs: https://github.com/wez/wezterm/issues/641
This commit is contained in:
Wez Furlong 2021-05-03 09:16:30 -07:00
parent 09f0421b48
commit 0e9d86f9f9
4 changed files with 59 additions and 11 deletions

View File

@ -2,8 +2,7 @@ use crate::{FontAttributes, FontStretch, FontWeight, TextStyle};
use anyhow::anyhow;
use bstr::BString;
pub use luahelper::*;
use mlua::{FromLua, ToLua, ToLuaMulti};
use mlua::{Lua, Table, Value};
use mlua::{FromLua, Lua, Table, ToLua, ToLuaMulti, Value, Variadic};
use serde::*;
use smol::prelude::*;
use std::path::Path;
@ -94,24 +93,59 @@ pub fn make_lua_context(config_file: &Path) -> anyhow::Result<Lua> {
lua.create_function(|_, ()| Ok(crate::running_under_wsl()))?,
)?;
fn print_helper(args: Variadic<Value>) -> String {
let mut output = String::new();
for (idx, item) in args.into_iter().enumerate() {
if idx > 0 {
output.push(' ');
}
match item {
Value::String(s) => match s.to_str() {
Ok(s) => output.push_str(s),
Err(_) => {
let item = String::from_utf8_lossy(s.as_bytes());
output.push_str(&item);
}
},
item @ _ => {
let item = format!("{:?}", ValueWrapper(item));
output.push_str(&item);
}
}
}
output
}
wezterm_mod.set(
"log_error",
lua.create_function(|_, msg: String| {
log::error!("lua: {}", msg);
lua.create_function(|_, args: Variadic<Value>| {
let output = print_helper(args);
log::error!("lua: {}", output);
Ok(())
})?,
)?;
wezterm_mod.set(
"log_info",
lua.create_function(|_, msg: String| {
log::info!("lua: {}", msg);
lua.create_function(|_, args: Variadic<Value>| {
let output = print_helper(args);
log::info!("lua: {}", output);
Ok(())
})?,
)?;
wezterm_mod.set(
"log_warn",
lua.create_function(|_, msg: String| {
log::warn!("lua: {}", msg);
lua.create_function(|_, args: Variadic<Value>| {
let output = print_helper(args);
log::warn!("lua: {}", output);
Ok(())
})?,
)?;
globals.set(
"print",
lua.create_function(|_, args: Variadic<Value>| {
let output = print_helper(args);
log::info!("lua: {}", output);
Ok(())
})?,
)?;

View File

@ -1,4 +1,4 @@
# `wezterm.log_error(msg)`
# `wezterm.log_error(arg, ..)`
This function logs the provided message string through wezterm's logging layer
at 'ERROR' level. If you started wezterm from a terminal that text will print
@ -10,4 +10,8 @@ local wezterm = require 'wezterm';
wezterm.log_error("Hello!");
```
*Since: nightly builds only*
Now accepts multiple arguments, and those arguments can be of any type.
See also [log_info](log_info.md) and [log_warn](log_warn.md).

View File

@ -1,4 +1,4 @@
# `wezterm.log_info(msg)`
# `wezterm.log_info(arg, ..)`
*Since: 20210314-114017-04b7cedd*
@ -12,5 +12,10 @@ local wezterm = require 'wezterm';
wezterm.log_info("Hello!");
```
*Since: nightly builds only*
Now accepts multiple arguments, and those arguments can be of any type.
See also [log_error](log_error.md) and [log_warn](log_warn.md).

View File

@ -1,4 +1,4 @@
# `wezterm.log_warn(msg)`
# `wezterm.log_warn(arg, ..)`
*Since: 20210314-114017-04b7cedd*
@ -12,5 +12,10 @@ local wezterm = require 'wezterm';
wezterm.log_warn("Hello!");
```
*Since: nightly builds only*
Now accepts multiple arguments, and those arguments can be of any type.
See also [log_info](log_info.md) and [log_error](log_error.md).