Skip to content

Commit

Permalink
Add flag to print detected terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
swsnr committed Aug 25, 2018
1 parent 0d620ec commit 4f94575
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,31 @@ fn read_input<T: AsRef<str>>(filename: T) -> std::io::Result<(PathBuf, String)>
}

fn process_arguments(size: TerminalSize, args: Arguments) -> Result<(), Box<Error>> {
let (base_dir, input) = read_input(args.filename)?;
let parser = Parser::new(&input);

if args.dump_events {
mdcat::dump_events(&mut std::io::stdout(), parser)?;
if args.detect_only {
println!("Terminal: {}", args.terminal.name());
Ok(())
} else {
let syntax_set = SyntaxSet::load_defaults_newlines();
mdcat::push_tty(
args.terminal,
TerminalSize {
width: args.columns,
..size
},
parser,
&base_dir,
args.resource_access,
syntax_set,
)?;
Ok(())
let (base_dir, input) = read_input(args.filename)?;
let parser = Parser::new(&input);

if args.dump_events {
mdcat::dump_events(&mut std::io::stdout(), parser)?;
Ok(())
} else {
let syntax_set = SyntaxSet::load_defaults_newlines();
mdcat::push_tty(
args.terminal,
TerminalSize {
width: args.columns,
..size
},
parser,
&base_dir,
args.resource_access,
syntax_set,
)?;
Ok(())
}
}
}

Expand All @@ -118,6 +123,7 @@ struct Arguments {
resource_access: ResourceAccess,
columns: usize,
dump_events: bool,
detect_only: bool,
}

impl Arguments {
Expand All @@ -136,6 +142,7 @@ impl Arguments {
};
let filename = value_t!(matches, "filename", String)?;
let dump_events = matches.is_present("dump_events");
let detect_only = matches.is_present("detect_only");
let columns = value_t!(matches, "columns", usize)?;
let resource_access = if matches.is_present("local_only") {
ResourceAccess::LocalOnly
Expand All @@ -148,6 +155,7 @@ impl Arguments {
columns,
resource_access,
dump_events,
detect_only,
terminal,
})
}
Expand Down Expand Up @@ -208,6 +216,12 @@ Report issues to <https://github.com/lunaryorn/mdcat>.",
.long("dump-events")
.help("Dump Markdown parser events and exit")
.hidden(true)
)
.arg(
Arg::with_name("detect_only")
.long("detect-only")
.help("Only detect the terminal type and exit")
.hidden(true)
);

let matches = app.get_matches();
Expand Down
4 changes: 4 additions & 0 deletions src/terminal/ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ impl<W: Write> AnsiTerminal<W> {
impl<W: Write> Terminal for AnsiTerminal<W> {
type TerminalWrite = W;

fn name(&self) -> &'static str {
"ANSI"
}

fn write(&mut self) -> &mut W {
&mut self.writer
}
Expand Down
4 changes: 4 additions & 0 deletions src/terminal/dumb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ impl<W: Write> DumbTerminal<W> {
impl<W: Write> Terminal for DumbTerminal<W> {
type TerminalWrite = W;

fn name(&self) -> &'static str {
"dumb"
}

fn write(&mut self) -> &mut W {
&mut self.writer
}
Expand Down
4 changes: 4 additions & 0 deletions src/terminal/iterm2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ impl<W: Write> ITerm2<W> {
impl<W: Write> Terminal for ITerm2<W> {
type TerminalWrite = W;

fn name(&self) -> &'static str {
"iTerm2"
}

fn write(&mut self) -> &mut W {
self.ansi.write()
}
Expand Down
4 changes: 4 additions & 0 deletions src/terminal/terminology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ impl<W: Write> Terminology<W> {
impl<W: Write> Terminal for Terminology<W> {
type TerminalWrite = W;

fn name(&self) -> &'static str {
"Terminology"
}

fn write(&mut self) -> &mut W {
self.ansi.write()
}
Expand Down
4 changes: 4 additions & 0 deletions src/terminal/vte50.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ impl<W: Write> VTE50Terminal<W> {
impl<W: Write> Terminal for VTE50Terminal<W> {
type TerminalWrite = W;

fn name(&self) -> &'static str {
"VTE 50"
}

fn write(&mut self) -> &mut W {
self.ansi.write()
}
Expand Down
3 changes: 3 additions & 0 deletions src/terminal/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub trait Terminal {
/// The associated writer of this terminal.
type TerminalWrite: Write;

/// Get a descriptive name for this terminal.
fn name(&self) -> &str;

/// Get a writer for this terminal.
fn write(&mut self) -> &mut Self::TerminalWrite;

Expand Down

0 comments on commit 4f94575

Please sign in to comment.