Skip to content

Commit

Permalink
Make most things pub(crate)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfontanini committed Oct 21, 2023
1 parent c33b588 commit 50a8548
Show file tree
Hide file tree
Showing 24 changed files with 274 additions and 286 deletions.
8 changes: 4 additions & 4 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static DEFAULT_BOTTOM_SLIDE_MARGIN: u16 = 3;
///
/// This type transforms [MarkdownElement]s and turns them into a presentation, which is made up of
/// render operations.
pub struct PresentationBuilder<'a> {
pub(crate) struct PresentationBuilder<'a> {
slide_operations: Vec<RenderOperation>,
slides: Vec<Slide>,
highlighter: CodeHighlighter,
Expand All @@ -45,7 +45,7 @@ pub struct PresentationBuilder<'a> {

impl<'a> PresentationBuilder<'a> {
/// Construct a new builder.
pub fn new(
pub(crate) fn new(
default_highlighter: CodeHighlighter,
default_theme: &'a PresentationTheme,
resources: &'a mut Resources,
Expand All @@ -65,7 +65,7 @@ impl<'a> PresentationBuilder<'a> {
}

/// Build a presentation.
pub fn build(mut self, elements: Vec<MarkdownElement>) -> Result<Presentation, BuildError> {
pub(crate) fn build(mut self, elements: Vec<MarkdownElement>) -> Result<Presentation, BuildError> {
if let Some(MarkdownElement::FrontMatter(contents)) = elements.first() {
self.process_front_matter(contents)?;
}
Expand Down Expand Up @@ -724,7 +724,7 @@ struct RunCodeOperationInner {
}

#[derive(Debug)]
pub struct RunCodeOperation {
pub(crate) struct RunCodeOperation {
code: Code,
default_colors: Colors,
block_colors: Colors,
Expand Down
4 changes: 2 additions & 2 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use crate::presentation::{Presentation, RenderOperation, Slide};
use std::{cmp::Ordering, mem};

/// Allow diffing presentations.
pub struct PresentationDiffer;
pub(crate) struct PresentationDiffer;

impl PresentationDiffer {
/// Find the first modified slide between original and updated.
///
/// This tries to take into account both content and style changes such that changing
pub fn first_modified_slide(original: &Presentation, updated: &Presentation) -> Option<usize> {
pub(crate) fn first_modified_slide(original: &Presentation, updated: &Presentation) -> Option<usize> {
let original_slides = original.iter_slides();
let updated_slides = updated.iter_slides();
for (index, (original, updated)) in original_slides.zip(updated_slides).enumerate() {
Expand Down
29 changes: 11 additions & 18 deletions src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use std::{
use tempfile::NamedTempFile;

/// Allows executing code.
pub struct CodeExecuter;
pub(crate) struct CodeExecuter;

impl CodeExecuter {
/// Execute a piece of code.
pub fn execute(code: &Code) -> Result<ExecutionHandle, CodeExecuteError> {
pub(crate) fn execute(code: &Code) -> Result<ExecutionHandle, CodeExecuteError> {
if !code.language.supports_execution() {
return Err(CodeExecuteError::UnsupportedExecution);
}
Expand Down Expand Up @@ -49,7 +49,7 @@ impl CodeExecuter {

/// An error during the execution of some code.
#[derive(thiserror::Error, Debug)]
pub enum CodeExecuteError {
pub(crate) enum CodeExecuteError {
#[error("code language doesn't support execution")]
UnsupportedExecution,

Expand All @@ -65,15 +65,15 @@ pub enum CodeExecuteError {

/// A handle for the execution of a piece of code.
#[derive(Debug)]
pub struct ExecutionHandle {
pub(crate) struct ExecutionHandle {
state: Arc<Mutex<ExecutionState>>,
#[allow(dead_code)]
reader_handle: thread::JoinHandle<()>,
}

impl ExecutionHandle {
/// Get the current state of the process.
pub fn state(&self) -> ExecutionState {
pub(crate) fn state(&self) -> ExecutionState {
self.state.lock().unwrap().clone()
}
}
Expand Down Expand Up @@ -123,21 +123,14 @@ impl ProcessReader {

/// The state of the execution of a process.
#[derive(Clone, Default, Debug)]
pub struct ExecutionState {
pub output: Vec<String>,
pub status: ProcessStatus,
}

impl ExecutionState {
/// Extract the lines printed so far.
pub fn into_lines(self) -> Vec<String> {
self.output
}
pub(crate) struct ExecutionState {
pub(crate) output: Vec<String>,
pub(crate) status: ProcessStatus,
}

/// The status of a process.
#[derive(Clone, Debug, Default)]
pub enum ProcessStatus {
pub(crate) enum ProcessStatus {
#[default]
Running,
Success,
Expand All @@ -146,7 +139,7 @@ pub enum ProcessStatus {

impl ProcessStatus {
/// Check whether the underlying process is finished.
pub fn is_finished(&self) -> bool {
pub(crate) fn is_finished(&self) -> bool {
matches!(self, ProcessStatus::Success | ProcessStatus::Failure)
}
}
Expand All @@ -172,7 +165,7 @@ echo 'bye'"
};

let expected_lines = vec!["hello world", "bye"];
assert_eq!(state.into_lines(), expected_lines);
assert_eq!(state.output, expected_lines);
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/input/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ use std::{fs, io, path::PathBuf, time::SystemTime};
/// This uses polling rather than something fancier like `inotify`. The latter turned out to make
/// code too complex for little added gain. This instead keeps the last modified time for the given
/// path and uses that to determine if it's changed.
pub struct PresentationFileWatcher {
pub(crate) struct PresentationFileWatcher {
path: PathBuf,
last_modification: SystemTime,
}

impl PresentationFileWatcher {
/// Create a watcher over the given file path.
pub fn new<P: Into<PathBuf>>(path: P) -> Self {
pub(crate) fn new<P: Into<PathBuf>>(path: P) -> Self {
let path = path.into();
let last_modification = fs::metadata(&path).and_then(|m| m.modified()).unwrap_or(SystemTime::UNIX_EPOCH);
Self { path, last_modification }
}

/// Checker whether this file has modifications.
pub fn has_modifications(&mut self) -> io::Result<bool> {
pub(crate) fn has_modifications(&mut self) -> io::Result<bool> {
let metadata = fs::metadata(&self.path)?;
let modified_time = metadata.modified()?;
if modified_time > self.last_modification {
Expand Down
6 changes: 3 additions & 3 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod fs;
pub mod source;
pub mod user;
pub(crate) mod fs;
pub(crate) mod source;
pub(crate) mod user;
13 changes: 2 additions & 11 deletions src/input/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,10 @@ impl CommandSource {
Self { watcher, user_input: UserInput::default() }
}

/// Block until the next command arrives.
pub fn next_command(&mut self) -> io::Result<Command> {
loop {
if let Some(command) = self.try_next_command()? {
return Ok(command);
}
}
}

/// Try to get the next command.
///
/// This attempts to get a command and returns `Ok(None)` on timeout.
pub fn try_next_command(&mut self) -> io::Result<Option<Command>> {
pub(crate) fn try_next_command(&mut self) -> io::Result<Option<Command>> {
match self.user_input.poll_next_command(Duration::from_millis(250)) {
Ok(Some(command)) => {
return Ok(Some(Command::User(command)));
Expand All @@ -48,7 +39,7 @@ impl CommandSource {

/// A command.
#[derive(Clone, Debug)]
pub enum Command {
pub(crate) enum Command {
/// A user input command.
User(UserCommand),

Expand Down
8 changes: 4 additions & 4 deletions src/input/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ use std::{io, mem, time::Duration};

/// A user input handler.
#[derive(Default)]
pub struct UserInput {
pub(crate) struct UserInput {
state: InputState,
}

impl UserInput {
/// Polls for the next input command coming from the keyboard.
pub fn poll_next_command(&mut self, timeout: Duration) -> io::Result<Option<UserCommand>> {
pub(crate) fn poll_next_command(&mut self, timeout: Duration) -> io::Result<Option<UserCommand>> {
if poll(timeout)? { self.next_command() } else { Ok(None) }
}

/// Blocks waiting for the next command.
pub fn next_command(&mut self) -> io::Result<Option<UserCommand>> {
pub(crate) fn next_command(&mut self) -> io::Result<Option<UserCommand>> {
let current_state = mem::take(&mut self.state);
let (command, next_state) = match read()? {
Event::Key(event) => Self::apply_key_event(event, current_state),
Expand Down Expand Up @@ -87,7 +87,7 @@ impl UserInput {

/// A command from the user.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum UserCommand {
pub(crate) enum UserCommand {
/// Redraw the presentation.
///
/// This can happen on terminal resize.
Expand Down
32 changes: 20 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@
//!
//! This is not meant to be used as a crate!
pub mod builder;
pub mod diff;
pub mod execute;
pub mod input;
pub mod markdown;
pub mod presentation;
pub mod presenter;
pub mod render;
pub mod resource;
pub mod splash;
pub mod style;
pub mod theme;
pub(crate) mod builder;
pub(crate) mod diff;
pub(crate) mod execute;
pub(crate) mod input;
pub(crate) mod markdown;
pub(crate) mod presentation;
pub(crate) mod presenter;
pub(crate) mod render;
pub(crate) mod resource;
pub(crate) mod style;
pub(crate) mod theme;

pub use crate::{
input::source::CommandSource,
markdown::parse::MarkdownParser,
presenter::{PresentMode, Presenter},
render::highlighting::CodeHighlighter,
resource::Resources,
theme::PresentationTheme,
};
27 changes: 20 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use clap::{error::ErrorKind, CommandFactory, Parser};
use colored::Colorize;
use comrak::Arena;
use presenterm::{
input::source::CommandSource,
markdown::parse::MarkdownParser,
presenter::{PresentMode, Presenter},
render::highlighting::CodeHighlighter,
resource::Resources,
splash::show_splashes,
theme::PresentationTheme,
CodeHighlighter, CommandSource, MarkdownParser, PresentMode, PresentationTheme, Presenter, Resources,
};
use std::path::{Path, PathBuf};

Expand All @@ -28,6 +23,24 @@ struct Cli {
theme: String,
}

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

let logo = format!(
r#"
┌─┐┬─┐┌─┐┌─┐┌─┐┌┐┌┌┬┐┌─┐┬─┐┌┬┐
├─┘├┬┘├┤ └─┐├┤ │││ │ ├┤ ├┬┘│││
┴ ┴└─└─┘└─┘└─┘┘└┘ ┴ └─┘┴└─┴ ┴ v{}
A terminal slideshow tool
@mfontanini/presenterm
"#,
crate_version,
)
.bold()
.purple();
format!("{logo}")
}

fn run(cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
let Some(default_theme) = PresentationTheme::from_name(&cli.theme) else {
let mut cmd = Cli::command();
Expand Down
Loading

0 comments on commit 50a8548

Please sign in to comment.