Skip to content

Commit

Permalink
fix and slightly change save type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
letterlock committed Dec 27, 2023
1 parent ca581bc commit 0dfdab4
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 153 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "brr-editor"
authors = ["maxwell letterlock"]
version = "1.0.1"
version = "1.0.2"
edition = "2021"
readme = "README.md"
license = "GPL-3.0-or-later"
Expand All @@ -21,3 +21,6 @@ simple-logging = "2.0.2"

[build-dependencies]
winres = "0.1"

[profile.release]
strip = true
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ if you want to take a break from writing and look over what you've written, you

## configuration
brr uses a simple plaintext config file that should be fairly straightforward to use, just open it in your favourite (actually functional) text editor and change the values after the equals symbols! the 'brr.conf.default' file contains all the default values and syntax, as well as some explanations for the various options.
on opening, brr will check the directory containing its executable for a 'brr.conf' file. if you're on linux, brr will first check "$XDG_CONFIG_HOME/brr" (if this is unset, it will also just check ~/.config/brr), before checking its own directory.
on opening, brr will check the directory containing its executable for a 'brr.conf' file. if you're on linux, brr will first check "`$XDG_CONFIG_HOME/brr`" (if this is unset, it will also just check `~/.config/brr`), before checking its own directory.

## logging and errors
if you're on windows, brr reports all errors through a log file placed in the same directory as the executable. if you're on linux, brr will first try to find (and create, if absent) "$XDG_STATE_HOME/brr". if $XDG_STATE_HOME is unset, it will default to "~/.local/state/brr". if it can't create that, it will just try to put the log in the directory containing its executable.
if you're on windows, brr reports all errors through a log file placed in the same directory as the executable. if you're on linux, brr will first try to find (and create, if absent) "`$XDG_STATE_HOME/brr`". if `$XDG_STATE_HOME` is unset, it will default to "`~/.local/state/brr`". if it can't create that, it will just try to put the log in the directory containing its executable.
if brr encounters an unrecoverable error, it will panic and die, leaving you with the error that killed it. if you opened the executable from outside of a terminal (e.g. you just ran the executable), the terminal will close as the process exits, so you'll have to check the log for what happened.
if you do encounter an error that kills brr, please report it as an issue, include your log file, and tell me what you were doing when brr was die.

Expand Down
9 changes: 5 additions & 4 deletions src/document.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Terminal, Metadata, DisplayRow, AppendBuffer, Position, die};
use crate::{Terminal, Metadata, DisplayRow, AppendBuffer, Position, die, SaveType};
use {
unicode_segmentation::UnicodeSegmentation,
words_count::WordsCount,
Expand Down Expand Up @@ -76,7 +76,7 @@ impl Document {
}
}

pub fn save(&mut self, words: u8) -> Result<(), Error> {
pub fn save(&mut self, words: u8, save_type: SaveType) -> Result<(), Error> {
let mut tmp_path = self.metadata.path.clone();
tmp_path.set_extension("tmp");
info!(
Expand All @@ -88,7 +88,8 @@ impl Document {
// words here is set in the config file and
// refers to how many words brr should save
// as they're written
if words > 1 {
if save_type == SaveType::Words
&& words > 1 {
if let Some((split_at_index, ..)) = self.append_buffer.buffer
.unicode_word_indices()
.nth(words.saturating_sub(1) as usize) {
Expand All @@ -101,7 +102,7 @@ impl Document {
} else if !self.append_buffer.buffer.is_empty() {
self.content.push_str(&self.append_buffer.buffer);
self.append_buffer.buffer.clear();
};
}

self.count = words_count::count(&self.content);

Expand Down
89 changes: 48 additions & 41 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ enum Mode {
Prompt,
}

#[derive(PartialEq, Clone, Copy)]
pub enum SaveType {
Words,
Time,
Manual,
}

struct Message {
text: String,
time: Instant,
Expand Down Expand Up @@ -223,7 +230,7 @@ impl Editor {
};
self.should_quit = true;
},
(KeyModifiers::CONTROL, KeyCode::Char('s')) => self.save(0),
(KeyModifiers::CONTROL, KeyCode::Char('s')) => self.save(0, SaveType::Manual),
(KeyModifiers::CONTROL, KeyCode::Char('o')) => self.open(),
(KeyModifiers::CONTROL, KeyCode::Char('h')) => {
self.message =
Expand All @@ -238,11 +245,13 @@ impl Editor {
},
(_, KeyCode::Char(pressed_char)) if self.mode == Mode::Edit => {
self.document.append_buffer.count_words();
if self.document.append_buffer.word_count == self.config.save_words as usize {
self.save(self.config.save_words);
if self.document.append_buffer.word_count == self.config.save_words as usize
&& self.config.save_words > 1 {
self.save(self.config.save_words, SaveType::Words);
} else if self.document.last_edit.elapsed() > Duration::new(self.config.save_time as u64, 0)
&& !self.document.append_buffer.buffer.is_empty() {
self.save(0);
&& !self.document.append_buffer.buffer.is_empty()
&& self.config.save_time > 0 {
self.save(0, SaveType::Time);
}
self.document.insert(pressed_char);
self.snap_view();
Expand All @@ -251,9 +260,9 @@ impl Editor {
(_, KeyCode::Backspace) if self.mode == Mode::Edit => {
// skip checking word count if backspacing
if self.document.last_edit.elapsed() > Duration::new(self.config.save_time as u64, 0)
&& !self.document.append_buffer.buffer.is_empty() {
self.save(0);
self.message = Message::from("sorry, five seconds passed! file saved.".to_string());
&& !self.document.append_buffer.buffer.is_empty()
&& self.config.save_time > 0 {
self.save(0, SaveType::Time);
}
if (self.view_pos.x > 0
|| self.view_pos.y > 0)
Expand All @@ -265,11 +274,13 @@ impl Editor {
},
(_, KeyCode::Enter) if self.mode == Mode::Edit => {
self.document.append_buffer.count_words();
if self.document.append_buffer.word_count == self.config.save_words as usize {
self.save(self.config.save_words);
if self.document.append_buffer.word_count == self.config.save_words as usize
&& self.config.save_words > 1 {
self.save(self.config.save_words, SaveType::Words);
} else if self.document.last_edit.elapsed() > Duration::new(self.config.save_time as u64, 0)
&& !self.document.append_buffer.buffer.is_empty() {
self.save(0);
&& !self.document.append_buffer.buffer.is_empty()
&& self.config.save_time > 0 {
self.save(0, SaveType::Time);
}
self.document.insert('\n');
self.snap_view();
Expand Down Expand Up @@ -326,7 +337,7 @@ impl Editor {
};
self.should_quit = true;
},
(KeyEventKind::Press, KeyModifiers::CONTROL, KeyCode::Char('s')) => self.save(0),
(KeyEventKind::Press, KeyModifiers::CONTROL, KeyCode::Char('s')) => self.save(0, SaveType::Manual),
(KeyEventKind::Press, KeyModifiers::CONTROL, KeyCode::Char('o')) => self.open(),
(KeyEventKind::Press, KeyModifiers::CONTROL, KeyCode::Char('h')) => {
self.message =
Expand All @@ -341,11 +352,13 @@ impl Editor {
},
(KeyEventKind::Press, _, KeyCode::Char(pressed_char)) if self.mode == Mode::Edit => {
self.document.append_buffer.count_words();
if self.document.append_buffer.word_count == self.config.save_words as usize {
self.save(self.config.save_words);
if self.document.append_buffer.word_count == self.config.save_words as usize
&& self.config.save_words > 1 {
self.save(self.config.save_words, SaveType::Words);
} else if self.document.last_edit.elapsed() > Duration::new(self.config.save_time as u64, 0)
&& !self.document.append_buffer.buffer.is_empty() {
self.save(0);
&& !self.document.append_buffer.buffer.is_empty()
&& self.config.save_time > 0 {
self.save(0, SaveType::Time);
}
self.document.insert(pressed_char);
self.snap_view();
Expand All @@ -354,8 +367,9 @@ impl Editor {
(KeyEventKind::Press, _, KeyCode::Backspace) if self.mode == Mode::Edit => {
// skip checking word count if backspacing
if self.document.last_edit.elapsed() > Duration::new(self.config.save_time as u64, 0)
&& !self.document.append_buffer.buffer.is_empty() {
self.save(0);
&& !self.document.append_buffer.buffer.is_empty()
&& self.config.save_time > 0 {
self.save(0, SaveType::Time);
self.message = Message::from("sorry, five seconds passed! file saved.".to_string());
}
if (self.view_pos.x > 0
Expand All @@ -368,11 +382,13 @@ impl Editor {
},
(KeyEventKind::Press, _, KeyCode::Enter) if self.mode == Mode::Edit => {
self.document.append_buffer.count_words();
if self.document.append_buffer.word_count == self.config.save_words as usize {
self.save(self.config.save_words);
if self.document.append_buffer.word_count == self.config.save_words as usize
&& self.config.save_words > 1 {
self.save(self.config.save_words, SaveType::Words);
} else if self.document.last_edit.elapsed() > Duration::new(self.config.save_time as u64, 0)
&& !self.document.append_buffer.buffer.is_empty() {
self.save(0);
&& !self.document.append_buffer.buffer.is_empty()
&& self.config.save_time > 0{
self.save(0, SaveType::Time);
}
self.document.insert('\n');
self.snap_view();
Expand Down Expand Up @@ -554,24 +570,15 @@ impl Editor {
Ok(())
}

pub fn save(&mut self, words: u8) {
if words > 1 {
match self.document.save(words) {
Ok(()) => (),
Err(error_msg) => {
self.message = Message::from("error writing file. see log for details.".to_string());
error!("[document.rs -> editor.rs]: {error_msg} - could not save file.");
},
};
} else {
match self.document.save(words) {
Ok(()) => self.message = Message::from("file saved successfully.".to_string()),
Err(error_msg) => {
self.message = Message::from("error writing file. see log for details.".to_string());
error!("[document.rs -> editor.rs]: {error_msg} - could not save file.");
},
};
};
pub fn save(&mut self, words: u8, save_type: SaveType) {
match self.document.save(words, save_type) {
Ok(()) if save_type == SaveType::Manual => self.message = Message::from("file saved successfully.".to_string()),
Ok(()) => (),
Err(error_msg) => {
self.message = Message::from("error writing file. see log for details.".to_string());
error!("[document.rs -> editor.rs]: {error_msg} - could not save file.");
},
}
}

pub fn open(&mut self) {
Expand Down
Loading

0 comments on commit 0dfdab4

Please sign in to comment.