1use std::convert::TryFrom;
2use std::ffi::{OsStr, OsString};
4use std::io::{self, BufRead, BufReader, Read};
7use content_inspector::{self, ContentType};
11/// A description of an Input source.
12/// This tells bat how to refer to the input.
14pub struct InputDescription {
15 pub(crate) name: String,
18 /// This replaces the name if provided.
19 title: Option<String>,
24 /// A summary description of the input.
25 /// Defaults to "{kind} '{name}'"
26 summary: Option<String>,
29impl InputDescription {
30 /// Creates a description for an input.
31 pub fn new(name: impl Into<String>) -> Self {
40 pub fn set_kind(&mut self, kind: Option<String>) {
44 pub fn set_summary(&mut self, summary: Option<String>) {
45 self.summary = summary;
48 pub fn set_title(&mut self, title: Option<String>) {
52 pub fn title(&self) -> &String {
53 match self.title.as_ref() {
54 Some(ref title) => title,
59 pub fn kind(&self) -> Option<&String> {
63 pub fn summary(&self) -> String {
64 self.summary.clone().unwrap_or_else(|| match &self.kind {
65 None => self.name.clone(),
66 Some(kind) => format!("{} '{}'", kind.to_lowercase(), self.name),
71pub(crate) enum InputKind<'a> {
72 OrdinaryFile(OsString),
74 CustomReader(Box<dyn Read + 'a>),
77impl<'a> InputKind<'a> {
78 pub fn description(&self) -> InputDescription {
80 InputKind::OrdinaryFile(ref path) => InputDescription::new(path.to_string_lossy()),
81 InputKind::StdIn => InputDescription::new("STDIN"),
82 InputKind::CustomReader(_) => InputDescription::new("READER"),
87#[derive(Clone, Default)]
88pub(crate) struct InputMetadata {
89 pub(crate) user_provided_name: Option<OsString>,
93 pub(crate) kind: InputKind<'a>,
94 pub(crate) metadata: InputMetadata,
95 pub(crate) description: InputDescription,
98pub(crate) enum OpenedInputKind {
99 OrdinaryFile(OsString),
104pub(crate) struct OpenedInput<'a> {
105 pub(crate) kind: OpenedInputKind,
106 pub(crate) metadata: InputMetadata,
107 pub(crate) reader: InputReader<'a>,
108 pub(crate) description: InputDescription,
112 pub fn ordinary_file(path: &OsStr) -> Self {
113 let kind = InputKind::OrdinaryFile(path.to_os_string());
115 description: kind.description(),
116 metadata: InputMetadata::default(),
121 pub fn stdin() -> Self {
122 let kind = InputKind::StdIn;
124 description: kind.description(),
125 metadata: InputMetadata::default(),
130 pub fn from_reader(reader: Box<dyn Read + 'a>) -> Self {
131 let kind = InputKind::CustomReader(reader);
133 description: kind.description(),
134 metadata: InputMetadata::default(),
139 pub fn is_stdin(&self) -> bool {
140 if let InputKind::StdIn = self.kind {
147 pub fn with_name(mut self, provided_name: Option<&OsStr>) -> Self {
148 if let Some(name) = provided_name {
149 self.description.name = name.to_string_lossy().to_string()
152 self.metadata.user_provided_name = provided_name.map(|n| n.to_owned());
156 pub fn description(&self) -> &InputDescription {
160 pub fn description_mut(&mut self) -> &mut InputDescription {
161 &mut self.description
164 pub(crate) fn open<R: BufRead + 'a>(self, stdin: R) -> Result<OpenedInput<'a>> {
165 let description = self.description().clone();
167 InputKind::StdIn => Ok(OpenedInput {
168 kind: OpenedInputKind::StdIn,
170 metadata: self.metadata,
171 reader: InputReader::new(stdin),
173 InputKind::OrdinaryFile(path) => Ok(OpenedInput {
174 kind: OpenedInputKind::OrdinaryFile(path.clone()),
176 metadata: self.metadata,
178 let file = File::open(&path)
179 .map_err(|e| format!("'{}': {}", path.to_string_lossy(), e))?;
180 if file.metadata()?.is_dir() {
181 return Err(format!("'{}' is a directory.", path.to_string_lossy()).into());
183 InputReader::new(BufReader::new(file))
186 InputKind::CustomReader(reader) => Ok(OpenedInput {
188 kind: OpenedInputKind::CustomReader,
189 metadata: self.metadata,
190 reader: InputReader::new(BufReader::new(reader)),
196impl TryFrom<&'_ Input<'_>> for clircle::Identifier {
199 fn try_from(input: &Input) -> std::result::Result<clircle::Identifier, ()> {
201 InputKind::OrdinaryFile(ref path) => Self::try_from(Path::new(path)).map_err(|_| ()),
202 InputKind::StdIn => Self::try_from(clircle::Stdio::Stdin).map_err(|_| ()),
203 InputKind::CustomReader(_) => Err(()),
208pub(crate) struct InputReader<'a> {
209 inner: Box<dyn BufRead + 'a>,
210 pub(crate) first_line: Vec<u8>,
211 pub(crate) content_type: Option<ContentType>,
214impl<'a> InputReader<'a> {
215 fn new<R: BufRead + 'a>(mut reader: R) -> InputReader<'a> {
216 let mut first_line = vec![];
217 reader.read_until(b'\n', &mut first_line).ok();
219 let content_type = if first_line.is_empty() {
222 Some(content_inspector::inspect(&first_line[..]))
225 if content_type == Some(ContentType::UTF_16LE) {
226 reader.read_until(0x00, &mut first_line).ok();
230 inner: Box::new(reader),
236 pub(crate) fn read_line(&mut self, buf: &mut Vec<u8>) -> io::Result<bool> {
237 if self.first_line.is_empty() {
238 let res = self.inner.read_until(b'\n', buf).map(|size| size > 0)?;
240 if self.content_type == Some(ContentType::UTF_16LE) {
241 self.inner.read_until(0x00, buf).ok();
246 buf.append(&mut self.first_line);
254 let content = b"#!/bin/bash\necho hello";
255 let mut reader = InputReader::new(&content[..]);
257 assert_eq!(b"#!/bin/bash\n", &reader.first_line[..]);
259 let mut buffer = vec![];
261 let res = reader.read_line(&mut buffer);
262 assert!(res.is_ok());
263 assert_eq!(true, res.unwrap());
264 assert_eq!(b"#!/bin/bash\n", &buffer[..]);
268 let res = reader.read_line(&mut buffer);
269 assert!(res.is_ok());
270 assert_eq!(true, res.unwrap());
271 assert_eq!(b"echo hello", &buffer[..]);
275 let res = reader.read_line(&mut buffer);
276 assert!(res.is_ok());
277 assert_eq!(false, res.unwrap());
278 assert!(buffer.is_empty());
283 let content = b"\xFF\xFE\x73\x00\x0A\x00\x64\x00";
284 let mut reader = InputReader::new(&content[..]);
286 assert_eq!(b"\xFF\xFE\x73\x00\x0A\x00", &reader.first_line[..]);
288 let mut buffer = vec![];
290 let res = reader.read_line(&mut buffer);
291 assert!(res.is_ok());
292 assert_eq!(true, res.unwrap());
293 assert_eq!(b"\xFF\xFE\x73\x00\x0A\x00", &buffer[..]);
297 let res = reader.read_line(&mut buffer);
298 assert!(res.is_ok());
299 assert_eq!(true, res.unwrap());
300 assert_eq!(b"\x64\x00", &buffer[..]);
304 let res = reader.read_line(&mut buffer);
305 assert!(res.is_ok());
306 assert_eq!(false, res.unwrap());
307 assert!(buffer.is_empty());
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list