Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor code
  • Loading branch information
baoyachi committed Feb 18, 2022
commit 122da4aa99323ed53e46b72c79c3c5c83a60f53f
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ strip-ansi-escapes = "0.1.1"
strum = "0.23"
strum_macros = "0.23"
fd-lock = "3.0.3"
time = {version="0.3.5",features=["parsing","formatting"]}
time = { version = "0.3.5", features = ["parsing", "formatting"] }
anyhow = "1.0.52"

[dev-dependencies]
tempfile = "3.2.0"
Expand Down
4 changes: 2 additions & 2 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl Reedline {
}

/// Output the complete [`History`] chronologically with numbering to the terminal
pub fn print_history(&mut self) -> Result<()> {
pub fn print_history(&mut self) -> anyhow::Result<()> {
let history: Vec<_> = self
.history
.iter_chronologic()
Expand All @@ -318,7 +318,7 @@ impl Reedline {

for (i, entry) in history {
let format_time_type = self.history.format_time_type();
self.print_line(&entry.format(i, format_time_type))?;
self.print_line(&entry.format(i, format_time_type)?)?;
}
Ok(())
}
Expand Down
20 changes: 10 additions & 10 deletions src/history/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,15 @@ impl InnerEntry {
}
}

pub fn format(&self, i: usize, f: Option<FormatTimeType>) -> String {
pub fn format(&self, i: usize, f: Option<FormatTimeType>) -> anyhow::Result<String> {
if let Some(f) = f {
let format_str = match f {
FormatTimeType::Time(_) => self
.time
.time()
.format(&f.validate_format().unwrap())
.unwrap(),
FormatTimeType::Date(_) => self.time.format(&f.validate_format().unwrap()).unwrap(),
FormatTimeType::Time(_) => self.time.time().format(&f.format_item()?)?,
FormatTimeType::Date(_) => self.time.format(&f.format_item()?)?,
};
return format!("{}\t{}", format_str, self.entry);
return Ok(format!("{}\t{}", format_str, self.entry));
}
format!("{}\t{}", i + 1, self.entry)
Ok(format!("{}\t{}", i + 1, self.entry))
}
}

Expand All @@ -54,7 +50,11 @@ pub enum FormatTimeType {
}

impl FormatTimeType {
pub(crate) fn validate_format(&self) -> Result<Vec<FormatItem<'_>>, InvalidFormatDescription> {
pub(crate) fn validate_format(&self) -> Result<(), InvalidFormatDescription> {
self.format_item().and_then(|_| Ok(()))
}

pub(crate) fn format_item(&self) -> Result<Vec<FormatItem<'_>>, InvalidFormatDescription> {
match self {
FormatTimeType::Time(f) | FormatTimeType::Date(f) => {
let vec = format_description::parse(f)?;
Expand Down
10 changes: 5 additions & 5 deletions src/history/file_backed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use std::{
path::PathBuf,
};


use time::{OffsetDateTime};
use time::error::InvalidFormatDescription;
use time::OffsetDateTime;

/// Default size of the [`FileBackedHistory`] used when calling [`FileBackedHistory::default()`]
pub const HISTORY_SIZE: usize = 1000;
Expand Down Expand Up @@ -194,11 +194,11 @@ impl FileBackedHistory {
Ok(hist)
}

pub fn with_time(self, f: FormatTimeType) -> Self {
pub fn with_time(self, f: FormatTimeType) -> Result<Self, InvalidFormatDescription> {
let mut hist = self;
f.validate_format().unwrap();
f.validate_format()?;
hist.format_time_type = Some(f);
hist
Ok(hist)
}

fn back_with_criteria(&mut self, criteria: &dyn Fn(&str) -> bool) {
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ use {
get_reedline_default_keybindings, get_reedline_edit_commands,
get_reedline_keybinding_modifiers, get_reedline_keycodes, get_reedline_prompt_edit_modes,
get_reedline_reedline_events, CompletionMenu, DefaultCompleter, DefaultHinter,
DefaultPrompt, EditMode, Emacs, ExampleHighlighter, FileBackedHistory,
HistoryMenu, Keybindings, Reedline, ReedlineEvent, Signal, Vi,
DefaultPrompt, EditMode, Emacs, ExampleHighlighter, FileBackedHistory, HistoryMenu,
Keybindings, Reedline, ReedlineEvent, Signal, Vi,
},
std::{
io::{stdout, Write},
time::Duration,
},
};

fn main() -> Result<()> {
fn main() -> anyhow::Result<()> {
// quick command like parameter handling
let vi_mode = matches!(std::env::args().nth(1), Some(x) if x == "--vi");
let debug_mode = matches!(std::env::args().nth(2), Some(x) if x == "--debug");
Expand Down