Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying the preferred image protocol #136

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use crate::{
processing::builder::{PresentationBuilderOptions, Themes},
render::{
highlighting::{CodeHighlighter, HighlightThemeSet},
media::MediaRender,
media::{GraphicsMode, MediaRender},
},
resource::Resources,
theme::{LoadThemeError, PresentationTheme, PresentationThemeSet},
Expand Down
55 changes: 49 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use clap::{error::ErrorKind, CommandFactory, Parser};
use clap::{error::ErrorKind, CommandFactory, Parser, ValueEnum};
use comrak::Arena;
use presenterm::{
CommandSource, Config, Exporter, HighlightThemeSet, LoadThemeError, MarkdownParser, MediaRender, PresentMode,
PresentationBuilderOptions, PresentationTheme, PresentationThemeSet, Presenter, PresenterOptions, Resources,
Themes, TypstRender,
CommandSource, Config, Exporter, GraphicsMode, HighlightThemeSet, LoadThemeError, MarkdownParser, MediaRender,
PresentMode, PresentationBuilderOptions, PresentationTheme, PresentationThemeSet, Presenter, PresenterOptions,
Resources, Themes, TypstRender,
};
use std::{
env,
Expand Down Expand Up @@ -44,8 +44,39 @@ struct Cli {
/// Display acknowledgements.
#[clap(long, group = "target")]
acknowledgements: bool,

/// The preferred image protocol.
#[clap(long)]
image_protocol: Option<ImageProtocol>,
}

#[derive(Clone, Debug, ValueEnum)]
enum ImageProtocol {
Iterm2,
Kitty,
Sixel,
AsciiBlocks,
}

impl TryFrom<ImageProtocol> for GraphicsMode {
type Error = SixelUnsupported;

fn try_from(protocol: ImageProtocol) -> Result<Self, Self::Error> {
let mode = match protocol {
ImageProtocol::Iterm2 => GraphicsMode::Iterm2,
ImageProtocol::Kitty => GraphicsMode::Kitty,
ImageProtocol::AsciiBlocks => GraphicsMode::AsciiBlocks,
#[cfg(feature = "sixel")]
ImageProtocol::Sixel => GraphicsMode::Sixel,
#[cfg(not(feature = "sixel"))]
ImageProtocol::Sixel => return Err(SixelUnsupported),
};
Ok(mode)
}
}

struct SixelUnsupported;

fn create_splash() -> String {
let crate_version = env!("CARGO_PKG_VERSION");

Expand Down Expand Up @@ -153,8 +184,20 @@ fn run(cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
}
} else {
let commands = CommandSource::new(&path, config.bindings)?;
let options =
PresenterOptions { builder_options: options, mode, font_size_fallback: config.defaults.terminal_font_size };
let graphics_mode = match cli.image_protocol.map(GraphicsMode::try_from) {
Some(Ok(mode)) => mode,
Some(Err(_)) => {
let mut cmd = Cli::command();
cmd.error(ErrorKind::InvalidValue, "sixel support was not enabled during compilation").exit();
}
None => GraphicsMode::default(),
};
let options = PresenterOptions {
builder_options: options,
mode,
graphics_mode,
font_size_fallback: config.defaults.terminal_font_size,
};
let presenter = Presenter::new(&default_theme, commands, parser, resources, typst, themes, options);
presenter.present(&path)?;
}
Expand Down
3 changes: 2 additions & 1 deletion src/presenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::{

pub struct PresenterOptions {
pub mode: PresentMode,
pub graphics_mode: GraphicsMode,
pub builder_options: PresentationBuilderOptions,
pub font_size_fallback: Option<u8>,
}
Expand Down Expand Up @@ -75,7 +76,7 @@ impl<'a> Presenter<'a> {

let graphics_mode = match self.options.mode {
PresentMode::Export => GraphicsMode::AsciiBlocks,
_ => GraphicsMode::default(),
_ => self.options.graphics_mode.clone(),
};
let mut drawer = TerminalDrawer::new(io::stdout(), graphics_mode, self.options.font_size_fallback)?;
loop {
Expand Down
3 changes: 1 addition & 2 deletions src/render/media.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use super::properties::CursorPosition;
use crate::render::properties::WindowSize;
use image::{DynamicImage, ImageError};
use std::{fmt::Debug, io, ops::Deref, path::PathBuf, rc::Rc};
use viuer::{get_kitty_support, is_iterm_supported, KittySupport, ViuError};

use super::properties::CursorPosition;

/// An image.
///
/// This stores the image in an [std::rc::Rc] so it's cheap to clone.
Expand Down
Loading