mirror of
https://github.com/tealdeer-rs/tealdeer.git
synced 2026-08-24 00:54:17 +02:00
Rust 2018 (#69)
This commit is contained in:
parent
f3cabddd0d
commit
f5e081ab68
10 changed files with 43 additions and 54 deletions
|
|
@ -2,7 +2,7 @@ version: 2
|
|||
jobs:
|
||||
build:
|
||||
docker:
|
||||
- image: rust:1.30
|
||||
- image: rust:1.31
|
||||
steps:
|
||||
- checkout
|
||||
# Load cargo target from cache if possible.
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
language: rust
|
||||
os: osx
|
||||
rust:
|
||||
- 1.30.0
|
||||
- 1.31.0
|
||||
- stable
|
||||
cache: cargo
|
||||
script: cargo test
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ readme = "README.md"
|
|||
repository = "https://github.com/dbrgn/tealdeer/"
|
||||
version = "1.1.0"
|
||||
include = ["/src/**/*", "/tests/**/*", "/Cargo.toml", "/README.md", "/LICENSE-*", "/screenshot.png", "/bash_tealdeer"]
|
||||
edition = "2018"
|
||||
|
||||
[[bin]]
|
||||
name = "tldr"
|
||||
|
|
|
|||
|
|
@ -117,7 +117,7 @@ tealdeer has been added to a few package managers:
|
|||
|
||||
### From Source (any platform)
|
||||
|
||||
tealdeer requires at least Rust 1.30.
|
||||
tealdeer requires at least Rust 1.31.
|
||||
|
||||
Debug build with logging enabled:
|
||||
|
||||
|
|
|
|||
35
src/cache.rs
35
src/cache.rs
|
|
@ -8,13 +8,14 @@ use std::os::unix::fs::MetadataExt;
|
|||
|
||||
use reqwest::{Client, Proxy};
|
||||
use flate2::read::GzDecoder;
|
||||
use log::debug;
|
||||
use tar::Archive;
|
||||
use time;
|
||||
use walkdir::{DirEntry, WalkDir};
|
||||
use xdg::BaseDirectories;
|
||||
|
||||
use error::TealdeerError::{self, CacheError, UpdateError};
|
||||
use types::OsType;
|
||||
use crate::error::TealdeerError::{self, CacheError, UpdateError};
|
||||
use crate::types::OsType;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Cache {
|
||||
|
|
@ -52,7 +53,7 @@ impl Cache {
|
|||
};
|
||||
|
||||
// Otherwise, fall back to $XDG_CACHE_HOME/tealdeer.
|
||||
let xdg_dirs = match BaseDirectories::with_prefix(::NAME) {
|
||||
let xdg_dirs = match BaseDirectories::with_prefix(crate::NAME) {
|
||||
Ok(dirs) => dirs,
|
||||
Err(_) => return Err(CacheError("Could not determine XDG base directory.".into())),
|
||||
};
|
||||
|
|
@ -88,20 +89,18 @@ impl Cache {
|
|||
/// Update the pages cache.
|
||||
pub fn update(&self) -> Result<(), TealdeerError> {
|
||||
// First, download the compressed data
|
||||
let bytes: Vec<u8> = try!(self.download());
|
||||
let bytes: Vec<u8> = self.download()?;
|
||||
|
||||
// Decompress the response body into an `Archive`
|
||||
let mut archive = self.decompress(&bytes[..]);
|
||||
|
||||
// Determine paths
|
||||
let cache_dir = try!(self.get_cache_dir());
|
||||
let cache_dir = self.get_cache_dir()?;
|
||||
|
||||
// Make sure that cache directory exists
|
||||
debug!("Ensure cache directory {:?} exists", &cache_dir);
|
||||
try!(
|
||||
fs::create_dir_all(&cache_dir)
|
||||
.map_err(|e| UpdateError(format!("Could not create cache directory: {}", e)))
|
||||
);
|
||||
fs::create_dir_all(&cache_dir)
|
||||
.map_err(|e| UpdateError(format!("Could not create cache directory: {}", e)))?;
|
||||
|
||||
// Clear cache directory
|
||||
// Note: This is not the best solution. Ideally we would download the
|
||||
|
|
@ -109,14 +108,12 @@ impl Cache {
|
|||
// But renaming a directory doesn't work across filesystems and Rust
|
||||
// does not yet offer a recursive directory copying function. So for
|
||||
// now, we'll use this approach.
|
||||
try!(self.clear());
|
||||
self.clear()?;
|
||||
|
||||
// Extract archive
|
||||
try!(
|
||||
archive
|
||||
.unpack(&cache_dir)
|
||||
.map_err(|e| UpdateError(format!("Could not unpack compressed data: {}", e)))
|
||||
);
|
||||
archive
|
||||
.unpack(&cache_dir)
|
||||
.map_err(|e| UpdateError(format!("Could not unpack compressed data: {}", e)))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -181,7 +178,7 @@ impl Cache {
|
|||
/// Return the available pages.
|
||||
pub fn list_pages(&self) -> Result<Vec<String>, TealdeerError> {
|
||||
// Determine platforms directory and platform
|
||||
let cache_dir = try!(self.get_cache_dir());
|
||||
let cache_dir = self.get_cache_dir()?;
|
||||
let platforms_dir = cache_dir.join("tldr-master").join("pages");
|
||||
let platform_dir = self.get_platform_dir();
|
||||
|
||||
|
|
@ -230,12 +227,12 @@ impl Cache {
|
|||
|
||||
/// Delete the cache directory.
|
||||
pub fn clear(&self) -> Result<(), TealdeerError> {
|
||||
let path = try!(self.get_cache_dir());
|
||||
let path = self.get_cache_dir()?;
|
||||
if path.exists() && path.is_dir() {
|
||||
try!(fs::remove_dir_all(&path).map_err(|_| CacheError(format!(
|
||||
fs::remove_dir_all(&path).map_err(|_| CacheError(format!(
|
||||
"Could not remove cache directory ({}).",
|
||||
path.display()
|
||||
))));
|
||||
)))?;
|
||||
} else if path.exists() {
|
||||
return Err(CacheError(format!(
|
||||
"Cache path ({}) is not a directory.",
|
||||
|
|
|
|||
|
|
@ -4,10 +4,12 @@ use std::io::{Error as IoError, Read, Write};
|
|||
use std::path::PathBuf;
|
||||
|
||||
use ansi_term::{Color, Style};
|
||||
use log::debug;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use toml;
|
||||
use xdg::BaseDirectories;
|
||||
|
||||
use error::TealdeerError::{self, ConfigError};
|
||||
use crate::error::TealdeerError::{self, ConfigError};
|
||||
|
||||
pub const CONFIG_FILE_NAME: &str = "config.toml";
|
||||
|
||||
|
|
@ -199,7 +201,7 @@ pub fn get_config_dir() -> Result<PathBuf, TealdeerError> {
|
|||
};
|
||||
|
||||
// Otherwise, fall back to $XDG_CONFIG_HOME/tealdeer.
|
||||
let xdg_dirs = match BaseDirectories::with_prefix(::NAME) {
|
||||
let xdg_dirs = match BaseDirectories::with_prefix(crate::NAME) {
|
||||
Ok(dirs) => dirs,
|
||||
Err(_) => {
|
||||
return Err(ConfigError("Could not determine XDG base directory.".into()))
|
||||
|
|
|
|||
|
|
@ -3,10 +3,11 @@
|
|||
use std::io::BufRead;
|
||||
|
||||
use ansi_term::{ANSIString, ANSIStrings};
|
||||
use log::debug;
|
||||
|
||||
use config::Config;
|
||||
use tokenizer::Tokenizer;
|
||||
use types::LineType;
|
||||
use crate::config::Config;
|
||||
use crate::tokenizer::Tokenizer;
|
||||
use crate::types::LineType;
|
||||
|
||||
fn highlight_command<'a>(
|
||||
command: &'a str,
|
||||
|
|
|
|||
35
src/main.rs
35
src/main.rs
|
|
@ -43,21 +43,8 @@
|
|||
)
|
||||
)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate ansi_term;
|
||||
extern crate reqwest;
|
||||
extern crate docopt;
|
||||
#[cfg(feature = "logging")]
|
||||
extern crate env_logger;
|
||||
extern crate flate2;
|
||||
extern crate tar;
|
||||
extern crate time;
|
||||
extern crate toml;
|
||||
extern crate walkdir;
|
||||
extern crate xdg;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
|
|
@ -66,6 +53,7 @@ use std::process;
|
|||
|
||||
use ansi_term::Color;
|
||||
use docopt::Docopt;
|
||||
use serde_derive::Deserialize;
|
||||
|
||||
mod cache;
|
||||
mod config;
|
||||
|
|
@ -74,12 +62,12 @@ mod formatter;
|
|||
mod tokenizer;
|
||||
mod types;
|
||||
|
||||
use cache::Cache;
|
||||
use config::{get_config_path, make_default_config, Config};
|
||||
use error::TealdeerError::{CacheError, ConfigError, UpdateError};
|
||||
use formatter::print_lines;
|
||||
use tokenizer::Tokenizer;
|
||||
use types::OsType;
|
||||
use crate::cache::Cache;
|
||||
use crate::config::{get_config_path, make_default_config, Config};
|
||||
use crate::error::TealdeerError::{CacheError, ConfigError, UpdateError};
|
||||
use crate::formatter::print_lines;
|
||||
use crate::tokenizer::Tokenizer;
|
||||
use crate::types::OsType;
|
||||
|
||||
const NAME: &str = "tealdeer";
|
||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
|
@ -137,7 +125,7 @@ struct Args {
|
|||
/// Print page by path
|
||||
fn print_page(path: &Path) -> Result<(), String> {
|
||||
// Open file
|
||||
let file = try!(File::open(path).map_err(|msg| format!("Could not open file: {}", msg)));
|
||||
let file = File::open(path).map_err(|msg| format!("Could not open file: {}", msg))?;
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
// Look up config file, if none is found fall back to default config.
|
||||
|
|
@ -375,11 +363,8 @@ fn main() {
|
|||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use docopt::Docopt;
|
||||
use docopt::Error;
|
||||
use Args;
|
||||
use OsType;
|
||||
use USAGE;
|
||||
use docopt::{Docopt, Error};
|
||||
use crate::{Args, OsType, USAGE};
|
||||
|
||||
fn test_helper(argv: &[&str]) -> Result<Args, Error> {
|
||||
Docopt::new(USAGE).and_then(|d| d.argv(argv.iter()).deserialize())
|
||||
|
|
|
|||
|
|
@ -2,7 +2,8 @@
|
|||
|
||||
use std::io::BufRead;
|
||||
|
||||
use types::LineType;
|
||||
use log::warn;
|
||||
use crate::types::LineType;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum TldrFormat {
|
||||
|
|
@ -87,7 +88,7 @@ where
|
|||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::Tokenizer;
|
||||
use types::LineType;
|
||||
use crate::types::LineType;
|
||||
|
||||
#[test]
|
||||
fn test_first_line_old_format() {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
use std::fmt;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Copy, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
#[allow(dead_code)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue