From e7efc8b64397c8eae897482649fa6619db7330fd Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Wed, 6 Sep 2023 06:37:12 +0800 Subject: [PATCH] wezterm: add binding for viewing last output in pager --- wezterm/config/events.lua | 33 +++++++++++++++++++++++++++++++++ wezterm/config/keys.lua | 5 ++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/wezterm/config/events.lua b/wezterm/config/events.lua index 560e32b..451a514 100644 --- a/wezterm/config/events.lua +++ b/wezterm/config/events.lua @@ -1,5 +1,7 @@ local module = {} local wezterm = require("wezterm") +local io = require("io") +local os = require("os") wezterm.on("update-right-status", function(window, pane) local key = window:active_key_table() @@ -9,4 +11,35 @@ wezterm.on("update-right-status", function(window, pane) window:set_right_status(key or "") end) +wezterm.on("view-last-output-in-new-pane", function(_, pane) + local zones = pane:get_semantic_zones("Output") + local last_zone = zones[#zones - 1] + + if not last_zone then + return nil + end + + local output = pane:get_text_from_semantic_zone(last_zone) + + local tmpname = os.tmpname() + local f = io.open(tmpname, "w+") + if f ~= nil then + f:write(output) + f:flush() + f:close() + end + + pane:split { + args = { os.getenv("PAGER") or "less", tmpname }, + direction = "Bottom", + domain = { DomainName = "local" }, + } + + -- Without this, it would quickly close all of the process immediately. + wezterm.sleep_ms(1000) + + -- While it isn't required, it is nice to clean it up. + os.remove(tmpname) +end) + return module diff --git a/wezterm/config/keys.lua b/wezterm/config/keys.lua index d02dd7c..fff2026 100644 --- a/wezterm/config/keys.lua +++ b/wezterm/config/keys.lua @@ -1,8 +1,10 @@ local wezterm = require("wezterm") local base = require("config/base") -local events = require("config/events") local act = wezterm.action +-- Take note this is required to import the events in this module. +local _ = require("config/events") + local keymod = base.keymod local alt_keymod = base.alt_keymod local module = {} @@ -115,6 +117,7 @@ function module.apply_to_config(config) { key = "r", mods = keymod, action = act.ReloadConfiguration }, { key = "t", mods = keymod, action = act.ShowDebugOverlay }, { key = "p", mods = keymod, action = act.ActivateCommandPalette }, + { key = "e", mods = keymod, action = act.EmitEvent "view-last-output-in-new-pane" }, -- Selection { key = "Space", mods = "LEADER", action = act.QuickSelect },