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

Save edited decision table #9

Open
DariuszDepta opened this issue Oct 4, 2024 · 1 comment
Open

Save edited decision table #9

DariuszDepta opened this issue Oct 4, 2024 · 1 comment
Assignees
Labels
editor Decision table editor good first issue Good for newcomers help wanted Extra attention is needed
Milestone

Comments

@DariuszDepta
Copy link
Member

Add saving option.

Under the ctrl+s key combination there should be an action of saving the edited decision table to the file.

@DariuszDepta DariuszDepta added good first issue Good for newcomers help wanted Extra attention is needed editor Decision table editor labels Oct 4, 2024
@DariuszDepta DariuszDepta added this to the 0.0.5 milestone Oct 4, 2024
@DariuszDepta
Copy link
Member Author

Use case

  • As a user I am opening a file containing a decision table in Unicode format.
  • Then I edit the content and press ctrl+s to save the changes.
  • All changes I made, should be saved in the originally opened file, because when I work with decision tables, in one terminal window I do edit the decision table and in the second terminal window I test/evaluate it using dsntk.
  • A backup file containing the original content should be created during the editing process. Since I am uncertain about the stability of the editor before its official production release, I want to ensure my original file is not lost.

Implementation proposal

  1. Before opening the original file, make a backup copy named original_name + .bak extension.
  2. Load the content of the file and allow editing. Original file must not be blocked during editing. It is done here:

    dte/editor/src/main.rs

    Lines 28 to 29 in cbd1fd4

    // read the file content as Unicode string
    if let Ok(content) = std::fs::read_to_string(file_name) {
  3. Add a new trigger for ctrl+s combination:
    #[derive(Debug, Copy, Clone, PartialEq, Eq)]
    pub enum Trigger {
    AltLeft,
    Backspace,
  4. Add a new trigger handler:
    pub fn read_trigger() -> Trigger {
    loop {
    if let Ok(event) = event::read() {
    match event {
    Event::Key(KeyEvent { code, modifiers, kind, state }) => match (code, modifiers, kind, state) {
    (KeyCode::Enter, MODIFIER_NONE, KIND_PRESS, STATUS_NONE) => return Trigger::Enter,
    (KeyCode::Left, MODIFIER_NONE, KIND_PRESS, STATUS_NONE) => return Trigger::Left,
  5. Create a new action for saving the file:

    dte/editor/src/editor.rs

    Lines 63 to 67 in cbd1fd4

    /// Processes a trigger when the screen is unlocked (normal state).
    fn process_trigger_when_unlocked_screen(&mut self, trigger: Trigger) -> Result<()> {
    match trigger {
    Trigger::Backspace => self.action_delete(true)?,
    Trigger::Char(ch) => self.action_insert_char(ch)?,
  6. In this action save the edited content to the original file. Edited content is available in the Controller:
    pub fn content(&self) -> &[Row] {
    self.plane.content()
    }
  7. The Row has function text() that returns a line of text. All rows may be joined with a new line, make sure the last new line is preserved in the text file and save the whole text in one operation to the original file.

Some Rust tips from ChatGPT 😀

Saving files

use std::fs::File;
use std::io::Write; // Import the Write trait

fn main() -> std::io::Result<()> {
    // Create or open the file
    let mut file = File::create("output.txt")?;

    // Write the text to the file
    file.write_all(b"Hello, Rust!")?;

    // Optional: Add a newline
    file.write_all(b"\n")?;

    // Return Ok if everything is fine
    Ok(())
}

Joining text slices

fn main() {
    let lines = vec!["Line 1", "Line 2", "Line 3"];
    let joined = lines.join("\n");
    println!("{}", joined);
}

@DariuszDepta DariuszDepta modified the milestones: 0.0.5, 0.0.6 Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editor Decision table editor good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants