Skip to content

Commit 7650fa7

Browse files
committed
CLI: Fixed broken pipe error (by using less for example)
1 parent a6ec3a8 commit 7650fa7

1 file changed

Lines changed: 63 additions & 55 deletions

File tree

portablemc-cli/src/output.rs

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl Output {
161161
OutputMode::Human(mode) => {
162162

163163
if !mode.log_newline {
164-
println!();
164+
let _ = writeln!(io::stdout());
165165
mode.log_newline = true;
166166
mode.log_line.clear();
167167
mode.log_background.clear();
@@ -224,7 +224,7 @@ impl<const BG: bool> Log<'_, BG> {
224224

225225
/// Internal function to flush the line and background buffers, should only be
226226
/// called in human readable mode.
227-
fn flush_human_line(&mut self, newline: bool) {
227+
fn flush_human_line(&mut self, newline: bool) -> io::Result<()> {
228228

229229
let OutputMode::Human(mode) = &mut self.output.mode else { panic!() };
230230

@@ -235,34 +235,34 @@ impl<const BG: bool> Log<'_, BG> {
235235
// we use cursor save/restore position in order to easily support wrapping.
236236
if mode.log_newline {
237237
// If the line is currently empty, save the cursor position!
238-
lock.write_all(b"\x1b[s").unwrap();
238+
lock.write_all(b"\x1b[s")?;
239239
} else {
240240
// If the line is not empty, restore saved cursor position and clear line.
241-
lock.write_all(b"\x1b[u\x1b[K").unwrap();
241+
lock.write_all(b"\x1b[u\x1b[K")?;
242242
}
243243
} else {
244-
lock.write_all(b"\r").unwrap();
244+
lock.write_all(b"\r")?;
245245
}
246246

247-
lock.write_all(mode.log_line.as_bytes()).unwrap();
247+
lock.write_all(mode.log_line.as_bytes())?;
248248
if !mode.log_line.is_empty() && !mode.log_background.is_empty() {
249-
lock.write_all(b" -- ").unwrap();
249+
lock.write_all(b" -- ")?;
250250
}
251-
lock.write_all(mode.log_background.as_bytes()).unwrap();
251+
lock.write_all(mode.log_background.as_bytes())?;
252252

253253
if newline {
254254

255255
mode.log_line.clear();
256256
mode.log_background.clear();
257257
mode.log_newline = true;
258258

259-
lock.write_all(b"\n").unwrap();
259+
lock.write_all(b"\n")?;
260260

261261
} else {
262262
mode.log_newline = false;
263263
}
264264

265-
lock.flush().unwrap();
265+
lock.flush()
266266

267267
}
268268

@@ -275,7 +275,7 @@ impl Log<'_, false> {
275275
pub fn newline(&mut self) -> &mut Self {
276276
if let OutputMode::Human(mode) = &mut self.output.mode {
277277
if !mode.log_newline {
278-
println!();
278+
let _ = writeln!(io::stdout());
279279
mode.log_line.clear();
280280
mode.log_background.clear();
281281
mode.log_newline = true;
@@ -331,7 +331,7 @@ impl Log<'_, false> {
331331
}
332332
}
333333

334-
self.flush_human_line(level != LogLevel::Pending);
334+
let _ = self.flush_human_line(level != LogLevel::Pending);
335335

336336
}
337337
}
@@ -395,7 +395,7 @@ impl Log<'_, true> {
395395
mode.log_background.clear();
396396
write!(mode.log_background, "{message}").unwrap();
397397

398-
self.flush_human_line(false);
398+
let _ = self.flush_human_line(false);
399399

400400
}
401401
self
@@ -499,7 +499,7 @@ impl<'a> TableOutput<'a> {
499499
}
500500
OutputMode::TabSep(mode) => {
501501
debug_assert!(mode.buffer.is_empty());
502-
println!("sep");
502+
let _ = writeln!(io::stdout(), "sep");
503503
}
504504
}
505505

@@ -540,59 +540,67 @@ impl Drop for TableOutput<'_> {
540540
};
541541

542542
let mut separators: _ = mode.table_separators.iter().copied().peekable();
543-
let mut writer = io::stdout().lock();
543+
544+
// Ignore any IO error. Replace with try block when they will be stable.
545+
let _ = (|| -> io::Result<()> {
544546

545-
// Write top segment.
546-
write!(writer, "┌─").unwrap();
547-
write_separator(&mut writer, "─┬─");
548-
write!(writer, "─┐\n").unwrap();
547+
let mut writer = io::stdout().lock();
549548

550-
// Reset and restart again to print.
551-
let mut row = 0usize;
552-
let mut column = 0usize;
553-
let mut last_idx = 0usize;
554-
for idx in mode.table_cells.iter().copied() {
549+
// Write top segment.
550+
write!(writer, "┌─")?;
551+
write_separator(&mut writer, "─┬─");
552+
write!(writer, "─┐\n")?;
555553

556-
if column != 0 {
557-
write!(writer, " │ ").unwrap();
558-
} else {
554+
// Reset and restart again to print.
555+
let mut row = 0usize;
556+
let mut column = 0usize;
557+
let mut last_idx = 0usize;
558+
for idx in mode.table_cells.iter().copied() {
559559

560-
if separators.next_if_eq(&row).is_some() {
561-
write!(writer, "├─").unwrap();
562-
write_separator(&mut writer, "─┼─");
563-
write!(writer, "─┤\n").unwrap();
564-
}
560+
if column != 0 {
561+
write!(writer, " │ ")?;
562+
} else {
565563

566-
write!(writer, "│ ").unwrap();
564+
if separators.next_if_eq(&row).is_some() {
565+
write!(writer, "├─")?;
566+
write_separator(&mut writer, "─┼─");
567+
write!(writer, "─┤\n")?;
568+
}
567569

568-
}
570+
write!(writer, "│ ")?;
569571

570-
let content = &mode.table_buffer[last_idx..idx];
571-
last_idx = idx;
572+
}
572573

573-
let width = columns_width[column];
574-
write!(writer, "{content:width$}").unwrap();
574+
let content = &mode.table_buffer[last_idx..idx];
575+
last_idx = idx;
576+
577+
let width = columns_width[column];
578+
write!(writer, "{content:width$}")?;
579+
580+
column += 1;
581+
if column == self.columns {
582+
row += 1;
583+
column = 0;
584+
write!(writer, " │\n")?;
585+
}
575586

576-
column += 1;
577-
if column == self.columns {
578-
row += 1;
579-
column = 0;
580-
write!(writer, " │\n").unwrap();
581587
}
582588

583-
}
589+
// It's a really special case that will never happen, add last separator.
590+
if separators.next_if_eq(&row).is_some() {
591+
write!(writer, "├─")?;
592+
write_separator(&mut writer, "─┼─");
593+
write!(writer, "─┤\n")?;
594+
}
584595

585-
// It's a really special case that will never happen, add last separator.
586-
if separators.next_if_eq(&row).is_some() {
587-
write!(writer, "├─").unwrap();
588-
write_separator(&mut writer, "─┼─");
589-
write!(writer, "─┤\n").unwrap();
590-
}
596+
// Write bottom segment.
597+
write!(writer, "└─")?;
598+
write_separator(&mut writer, "─┴─");
599+
write!(writer, "─\n")?;
600+
601+
Ok(())
591602

592-
// Write bottom segment.
593-
write!(writer, "└─").unwrap();
594-
write_separator(&mut writer, "─┴─");
595-
write!(writer, "─┘\n").unwrap();
603+
})();
596604

597605
mode.table_buffer.clear();
598606
mode.table_cells.clear();
@@ -654,7 +662,7 @@ impl Drop for Row<'_> {
654662
for _ in 0..self.column_remaining {
655663
mode.buffer.push('\t');
656664
}
657-
println!("{}", mode.buffer);
665+
let _ = writeln!(io::stdout(), "{}", mode.buffer);
658666
mode.buffer.clear();
659667
}
660668
}

0 commit comments

Comments
 (0)