Skip to content
Open
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 逐窗格命令输出采集与本机引用

## Status

本地候选;相关测试、正常产品构建及隔离 CMD 静态恢复验证已执行,尚未提交或推送。

## Context

网格快照和有界输出存储已有共享实现,但尚需将已确认的 shell 命令与具体窗格关联。
本阶段提供采集、静态恢复和不可变引用,不接入完整窗口输出保存队列或设置控件。

## Evidence

- `pebrel_app/src/gpui_shell/terminal/view/completion.rs` 的 `commit_line` 从屏幕和提示符边界确认提交,运行中程序内部输入不能作为新命令。
- `pebrel_app/src/recent_output.rs` 与 `pebrel_terminal/src/grid/snapshot.rs` 已提供容量约束和静态导入;视图不重复实现。
- `pebrel_app/src/gpui_shell/workspace/session_persistence/shutdown_tests.rs` 保护系统拆除期间最后完整工作区,独立实例保存还须遵循更新预约通道。
- 设置入口原有 1999 行,直接加入本阶段规则后为 2020 行;按启动与恢复准入职责提取后为 1993 行,不提高预算、不压缩格式或删测试。

## Decision

- 只在 `commit_line` 确认分支开始采集;Runtime 输入沿用回显屏障及提交前保存的提示符。结束与快照读取主屏,即使备用屏正活动,也不采集其中的输入。
- 恢复只导入显示数据,不执行命令;弱引用、退出状态、首次提交和关闭采集状态共同拒绝过期恢复。
- 按窗口、窗格及内容摘要生成本机引用,先全局淘汰最旧记录再生成引用。分享工作区始终跳过本机引用,手动导入与复制标签不加载输出。
- 在原会话保存器中保留 prepare/complete 代次边界,同时沿用最新系统退出候选选择、取消及更新预约隔离。prepare 返回 I/O 结果,不能把独立实例更新预约失败伪装成无待保存任务。
- 启动与恢复准入规则集中在设置层 `startup.rs`;原目录校验方法原样迁入,调用接口不变,不改变配置格式和依赖方向。

## Rejected alternatives

- 从任意 Enter、键击镜像、cwd 或运行徽章推断提交或归属:会记录程序内部输入或串档。
- 恢复旧分支的完整会话保存器:会丢失关机冻结和独立实例更新预约行为。
- 为本阶段复制已有快照、存储模块或接入完整输出保存队列:重复权威或越过阶段边界。

## Consequences

保留最近五条和既有容量合同;历史显示不能恢复活进程。当前阶段仍不提供完整保存开关 UI 和窗口输出持久化接线,不应宣称完整产品恢复已接通。

## Validation

终端 580 项、设置 111 项、应用相关去重 438 项通过;文件预算 2 项通过,格式与架构检查通过。正常 GPUI 产品构建及两个同目录原生 CMD 的隔离实测通过:各自恢复五条记录、中文可见、互不串档、继续输入不重复导入且不重跑旧命令。实测使用合成存档,仅证明本阶段静态恢复。启动补全组默认并行运行仍有失败,未修改上游亦可复现;候选串行运行 45 项通过,不将其描述为默认并行全绿。

## Supersedes

None。

## Revisit when

完整窗口输出保存接线落地,或命令确认、逐 pane 启动描述及系统退出保存合同改变时。
2 changes: 2 additions & 0 deletions pebrel_app/src/gpui_shell/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub struct Settings {
pub cursor_motion: pebrel_settings::CursorMotion,
/// 选区完成即复制(旧壳 `copy_on_select` 设置)。
pub copy_on_select: bool,
pub(crate) command_output_enabled: bool,
pub scrollback_lines: usize,
pub scroll_speed: f32,
pub scrollbar_visibility: pebrel_settings::ScrollbarVisibility,
Expand Down Expand Up @@ -296,6 +297,7 @@ impl Settings {
cursor_blink: runtime.cursor_blink,
cursor_motion: runtime.cursor_motion,
copy_on_select: runtime.copy_on_select,
command_output_enabled: runtime.command_output_enabled(),
scrollback_lines: runtime.scrollback_lines,
scroll_speed: runtime.scroll_speed,
scrollbar_visibility: runtime.scrollbar_visibility,
Expand Down
16 changes: 15 additions & 1 deletion pebrel_app/src/gpui_shell/terminal/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ mod path_drop;
mod pointer;
mod program_status;
mod reading;
mod recent_output;
#[cfg(all(test, feature = "gpui-test-support"))]
mod recent_output_tests;
mod remote_activity;
mod runtime;
mod session_failure;
Expand Down Expand Up @@ -352,6 +355,11 @@ pub struct TerminalView {
/// `NEBULA|` 标题报 `running_program`,装了 OSC133 集成的 bash/zsh/nu 只
/// 发这一对语义标记——两条路都要能点亮侧栏的「运行中」。
command_running: bool,
recent_output: crate::recent_output::RecentOutput,
recent_output_restore_closed: bool,
recent_output_enabled: bool,
reveal_recent_output_on_restore: bool,
restored_viewport_offset: Option<usize>,
/// 进程树给出的反证:`command_running` 为真、但 shell 树下只剩交互式
/// shell 与 console plumbing 时置位,转圈据此熄灭(issue #42)。判据与
/// 节流都在 `runtime::reconcile_shell_activity`,裁定理由见那里。
Expand Down Expand Up @@ -645,6 +653,7 @@ impl TerminalView {
}
match event {
TermEvent::Wakeup => {
self.follow_restored_viewport();
self.restore_shell_pointer_selection();
self.observe_reading_output(cx);
self.flush_pending_runtime_submit(cx);
Expand Down Expand Up @@ -973,11 +982,14 @@ impl TerminalView {
if !preserves_prompt {
term.nebula_end_prompt();
}
term.scroll_display(Scroll::Bottom);
if self.restored_viewport_offset.is_none() {
term.scroll_display(Scroll::Bottom);
}
term.selection = None;
}
session.notifier.notify(bytes);
}
self.follow_restored_viewport();
if status_acknowledged {
self.refresh_program_status(cx);
}
Expand Down Expand Up @@ -1177,6 +1189,7 @@ impl TerminalView {
let font_size = px(settings.font_size_px);
let palette = Arc::new(settings.palette.clone());
let copy_on_select = settings.copy_on_select;
let command_output_enabled = settings.command_output_enabled;
let default_cursor_style = settings.term_config().default_cursor_style;
let cursor_style_changed = self.default_cursor_style != default_cursor_style;
self.ghost_enabled = settings.ghost;
Expand Down Expand Up @@ -1230,6 +1243,7 @@ impl TerminalView {
term.reset_cursor_style_override();
}
}
self.set_recent_output_enabled(command_output_enabled);
self.restart_cursor_blink(cx);
cx.notify();
}
Expand Down
1 change: 1 addition & 0 deletions pebrel_app/src/gpui_shell/terminal/view/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ impl TerminalView {
&& !self.suggest.screen_line.trim().is_empty();
suggest::commit_line(&mut self.suggest);
if confirmed_submission {
self.begin_recent_output();
self.mark_submitted_command(native_submission);
self.classify_submitted_command();
cx.notify();
Expand Down
6 changes: 5 additions & 1 deletion pebrel_app/src/gpui_shell/terminal/view/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ impl TerminalView {
}

/// 把回滚位置落到 `target`(滚动条拖拽的落点提交)。
pub(super) fn scroll_to_offset(&self, target: usize, current: usize) {
pub(super) fn scroll_to_offset(&mut self, target: usize, current: usize) {
self.reveal_recent_output_on_restore = false;
self.restored_viewport_offset = None;
if let Some(session) = &self.session
&& self.native_results.scroll_to(target, &mut session.term.lock())
{
Expand Down Expand Up @@ -1124,6 +1126,8 @@ impl TerminalView {
}
session.notifier.notify(bytes);
} else if !application_only {
self.reveal_recent_output_on_restore = false;
self.restored_viewport_offset = None;
let (offset, history) = self
.native_results
.projection
Expand Down
138 changes: 138 additions & 0 deletions pebrel_app/src/gpui_shell/terminal/view/recent_output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//! 命令恢复只消费已确认的 shell 提交,不从运行徽章推断提交。

use super::TerminalView;
use pebrel_terminal::term::TermMode;

impl TerminalView {
pub(crate) fn restore_output_batch(
path: std::path::PathBuf,
targets: Vec<(String, gpui::WeakEntity<Self>)>,
cx: &mut gpui::App,
) {
if targets.is_empty() {
return;
}
let read = cx
.background_executor()
.spawn(async move { crate::recent_output::storage::Archive::read_from(&path) });
cx.spawn(async move |cx| {
let mut archive = match read.await {
Ok(archive) => archive,
Err(error) => {
if error.kind() != std::io::ErrorKind::NotFound {
log::warn!("Could not restore command output: {error}");
}
return;
},
};
for (id, target) in targets {
let Some(records) = archive.take_records(&id) else { continue };
let _ = target.update(cx, |view, cx| {
if view.exited.is_some() {
return;
}
if let Err(error) = view.restore_recent_output(records, cx) {
log::warn!("Invalid saved command output: {error}");
}
});
}
})
.detach();
}

pub(crate) fn set_recent_output_enabled(&mut self, enabled: bool) {
self.recent_output_enabled = enabled;
if !enabled {
self.clear_recent_output();
}
}

pub(crate) fn clear_recent_output(&mut self) {
self.recent_output = crate::recent_output::RecentOutput::default();
self.recent_output_restore_closed = true;
}

pub(crate) fn restore_recent_output(
&mut self,
records: Vec<crate::recent_output::CommandRecord>,
cx: &mut gpui::Context<Self>,
) -> Result<bool, &'static str> {
if !self.recent_output_enabled || self.recent_output_restore_closed {
return Ok(false);
}
let Some(session) = &self.session else { return Ok(false) };
let recent = crate::recent_output::RecentOutput::try_from_records(records)?;
let mut term = session.term.lock();
if term.mode().contains(TermMode::ALT_SCREEN) {
return Ok(false);
}
let reveal = self.reveal_recent_output_on_restore
&& term.grid().display_offset() == 0
&& !term.mode().contains(TermMode::VI);
recent.restore(term.grid_mut())?;
// 恢复内容位于主屏之前;只移动视口,不改 ConPTY 的绝对光标坐标。
// 新输出逐步占用主屏下方空行,输入不能提前隐藏恢复内容。
if reveal {
let grid = term.grid();
use pebrel_terminal::grid::Dimensions;
let room = grid.screen_lines().saturating_sub(grid.cursor.point.line.0 as usize + 1);
let offset = room.min(grid.history_size());
term.scroll_display(pebrel_terminal::grid::Scroll::Delta(offset as i32));
self.restored_viewport_offset = (offset > 0).then_some(offset);
}
self.recent_output = recent;
self.recent_output_restore_closed = true;
drop(term);
cx.notify();
Ok(true)
}

pub(super) fn follow_restored_viewport(&mut self) {
use pebrel_terminal::grid::{Dimensions, Scroll};
let Some(_) = self.restored_viewport_offset else { return };
let Some(session) = &self.session else { return };
let mut term = session.term.lock();
if term.mode().intersects(TermMode::ALT_SCREEN | TermMode::VI) {
self.restored_viewport_offset = None;
return;
}
let grid = term.grid();
let room = grid.screen_lines().saturating_sub(grid.cursor.point.line.0 as usize + 1);
// 恢复跟随期间用空白容量容纳历史;宽度重排可能增加历史行数,
// 不能沿用恢复时的旧 offset,否则会截掉长命令的开头。
let current = grid.display_offset();
let offset = room.min(grid.history_size());
if current != offset {
term.scroll_display(Scroll::Delta(offset as i32 - current as i32));
}
self.restored_viewport_offset = (offset > 0).then_some(offset);
}

pub(super) fn begin_recent_output(&mut self) {
if !self.recent_output_enabled {
return;
}
let Some(session) = &self.session else { return };
let term = session.term.lock();
if !term.mode().intersects(TermMode::ALT_SCREEN | TermMode::VI) {
self.recent_output_restore_closed = true;
self.recent_output.finish(term.grid());
self.recent_output.begin(term.grid());
}
}

pub(super) fn finish_recent_output(&mut self) {
let Some(session) = &self.session else { return };
let term = session.term.lock();
self.recent_output.finish(term.primary_grid());
}

pub(crate) fn recent_output_snapshot(&self) -> Vec<crate::recent_output::CommandRecord> {
if !self.recent_output_enabled {
return Vec::new();
}
let Some(session) = &self.session else { return Vec::new() };
let term = session.term.lock();
self.recent_output.snapshot(term.primary_grid())
}
}
Loading
Loading