-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
term.rs
155 lines (141 loc) · 3.61 KB
/
term.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).
use std::fs::File;
use std::io::{Read, Write};
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::sync::Arc;
use parking_lot::Mutex;
use crate::{Console, Destination};
///
/// An implementation of Read and Write that reads from stdin and writes to stderr.
///
/// Used to implement `console::Term` for use with the `indicatif` library.
///
#[derive(Debug)]
pub(crate) struct TermDestination {
// Optional so that it can be restored to the destination on Drop.
console: Mutex<Option<Console>>,
// The destination that the Console was taken from, and will be restored to.
destination: Arc<Destination>,
}
impl TermDestination {
pub(crate) fn new(
console: Console,
destination: Arc<Destination>,
) -> (
TermReadDestination,
TermWriteDestination,
TermWriteDestination,
) {
let stderr_use_color = console.stderr_use_color;
let term_destination = Arc::new(TermDestination {
console: Mutex::new(Some(console)),
destination,
});
(
TermReadDestination(term_destination.clone()),
TermWriteDestination {
destination: term_destination.clone(),
use_color: false,
is_stderr: false,
},
TermWriteDestination {
destination: term_destination,
use_color: stderr_use_color,
is_stderr: true,
},
)
}
}
#[derive(Debug)]
pub struct TermReadDestination(Arc<TermDestination>);
#[derive(Debug)]
pub struct TermWriteDestination {
destination: Arc<TermDestination>,
pub use_color: bool,
is_stderr: bool,
}
impl Read for TermReadDestination {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.0.console.lock().as_mut().unwrap().read_stdin(buf)
}
}
impl AsRawFd for TermReadDestination {
fn as_raw_fd(&self) -> RawFd {
self.0.console.lock().as_ref().unwrap().stdin_as_raw_fd()
}
}
impl Write for TermWriteDestination {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
if self.is_stderr {
self
.destination
.console
.lock()
.as_mut()
.unwrap()
.write_stderr(buf)?;
} else {
self
.destination
.console
.lock()
.as_mut()
.unwrap()
.write_stdout(buf)?;
}
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
impl AsRawFd for TermWriteDestination {
fn as_raw_fd(&self) -> RawFd {
if self.is_stderr {
self
.destination
.console
.lock()
.as_ref()
.unwrap()
.stderr_as_raw_fd()
} else {
self
.destination
.console
.lock()
.as_ref()
.unwrap()
.stdout_as_raw_fd()
}
}
}
impl Drop for TermDestination {
fn drop(&mut self) {
self
.destination
.exclusive_clear(self.console.lock().take().unwrap())
}
}
///
/// Attempt to clone the file handle behind this destination to turn it into an owned File
/// reference which can be closed independently.
///
/// Roughly equivalent to `File::try_clone`.
///
pub trait TryCloneAsFile {
fn try_clone_as_file(&self) -> std::io::Result<File>;
}
impl<T: AsRawFd> TryCloneAsFile for T {
fn try_clone_as_file(&self) -> std::io::Result<File> {
let raw_fd = self.as_raw_fd();
unsafe {
let underlying_file = File::from_raw_fd(raw_fd);
let cloned = underlying_file.try_clone()?;
// Drop the temporarily materialized file now that we've duped it.
let _ = underlying_file.into_raw_fd();
Ok(cloned)
}
}
}