Apply Web2C TCX tables to native byte streams

This commit is contained in:
gloamxun committed 2026-11-28 09:55:09 +08:00
1 parent 3bb2330676
commit 509f229a52
13 files changed
+393 -38

No files matched your search

+35 -10
View File
@@ -1066,6 +1066,23 @@ impl Files {
text: &str,
start_line: bool,
) -> Result<(), String> {
self.emit_diagnostic_bytes(destination, text.as_bytes(), start_line)
}
fn emit_diagnostic_bytes(
&mut self,
destination: WriteDestination,
bytes: &[u8],
start_line: bool,
) -> Result<(), String> {
let column = |previous| {
let previous = if start_line { 0 } else { previous };
match std::str::from_utf8(bytes) {
Ok(text) => output_column(previous, text),
Err(_) => bytes.iter().rposition(|&b| b == b'\n')
.map_or(previous + bytes.len(), |at| bytes.len() - at - 1),
}
};
if destination != WriteDestination::TerminalOnly {
if self.log.is_none() {
self.log = Some(
@@ -1081,17 +1098,16 @@ impl Files {
if start_line && self.log_column != 0 {
log.write_all(b"\n").map_err(|e| e.to_string())?;
}
log.write_all(text.as_bytes())
.map_err(|e| format!("native log: {e}"))?;
self.log_column = output_column(if start_line { 0 } else { self.log_column }, text);
log.write_all(bytes).map_err(|e| format!("native log: {e}"))?;
self.log_column = column(self.log_column);
}
if self.terminal_sink && self.terminal_enabled && destination != WriteDestination::Log {
let mut terminal = io::stderr().lock();
if start_line && self.terminal_column != 0 {
eprintln!();
terminal.write_all(b"\n").map_err(|e| e.to_string())?;
}
eprint!("{text}");
self.terminal_column =
output_column(if start_line { 0 } else { self.terminal_column }, text);
terminal.write_all(bytes).map_err(|e| e.to_string())?;
self.terminal_column = column(self.terminal_column);
}
Ok(())
}
@@ -1615,6 +1631,9 @@ impl SourceProvider for Files {
fn observe_message(&mut self, text: &str) -> Result<(), String> {
self.emit_diagnostic(WriteDestination::TerminalAndLog, text, false)
}
fn observe_message_bytes(&mut self, bytes: &[u8]) -> Result<(), String> {
self.emit_diagnostic_bytes(WriteDestination::TerminalAndLog, bytes, false)
}
fn observe_diagnostic_pair(&mut self, log: &str, terminal: &str) -> Result<(), String> {
if self.log_column != 0 || self.terminal_column != 0 {
self.emit_diagnostic(WriteDestination::TerminalAndLog, "\n", false)?;
@@ -1871,13 +1890,16 @@ impl SourceProvider for Files {
result
}
fn write_out(&mut self, stream: u8, text: &str) -> Result<(), String> {
self.write_out_bytes(stream, text.as_bytes())
}
fn write_out_bytes(&mut self, stream: u8, bytes: &[u8]) -> Result<(), String> {
let Some(output) = self.outputs.get_mut(&stream) else {
let error = "host output stream is not open".to_owned();
self.stream_io.push(StreamEffect::Write {
stream,
handle: None,
accepted_before: None,
requested: std::sync::Arc::from(text.as_bytes()),
requested: std::sync::Arc::from(bytes),
accepted: 0,
attempts: Vec::new(),
error: Some(error.clone()),
@@ -1885,7 +1907,7 @@ impl SourceProvider for Files {
return Err(error);
};
let before = output.accepted;
let transfer = native_stream::write_all(&mut output.file, text.as_bytes());
let transfer = native_stream::write_all(&mut output.file, bytes);
output.accepted += transfer.accepted as u64;
let error = transfer
.error
@@ -1894,7 +1916,7 @@ impl SourceProvider for Files {
stream,
handle: Some(output.handle),
accepted_before: Some(before),
requested: std::sync::Arc::from(text.as_bytes()),
requested: std::sync::Arc::from(bytes),
accepted: transfer.accepted as u64,
attempts: transfer.attempts,
error: error.clone(),
@@ -1935,6 +1957,9 @@ impl SourceProvider for Files {
fn diagnostic(&mut self, destination: WriteDestination, text: &str) -> Result<(), String> {
self.emit_diagnostic(destination, text, true)
}
fn diagnostic_bytes(&mut self, destination: WriteDestination, bytes: &[u8]) -> Result<(), String> {
self.emit_diagnostic_bytes(destination, bytes, true)
}
}
#[derive(PartialEq, Eq)]