/// Invoke a command in the prompt
Invoke a command in the prompt
Invoke a command in the prompt
pub fn invoke<'a>(&mut self, cmd: PromptCommand<'a>) {
        use self::PromptCommand::*;
        match cmd {
            Set { option } => {
                self.status_bar.msg = match self.options.set(option) {
                    Ok(()) => format!("Option set: {}", option),
                    Err(()) => format!("Option does not exist: {}", option),
                }
            }
            Unset { option } => {
                self.status_bar.msg = match self.options.unset(option) {
                    Ok(()) => format!("Option unset: {}", option),
                    Err(()) => format!("Option does not exist: {}", option),
                }
            }
            Toggle { option } => {
                self.status_bar.msg = match self.options.toggle(option) {
                    Ok(()) => format!("Option toggled: {}", option),
                    Err(()) => format!("Option does not exist: {}", option),
                }
            }
            Get { option } => {
                self.status_bar.msg = match self.options.get(option) {
                    Some(true) => format!("Option set: {}", option),
                    Some(false) => format!("Option unset: {}", option),
                    None => format!("Option does not exist: {}", option),
                }
            }
            Open { path } => {
                let line = self.buffers.current_buffer().get_line(0).unwrap().len();
                let empty = self.buffers.current_buffer().len() == 1 && line == 0;
                let ix = self.buffers.current_buffer_index();
                self.status_bar.msg = match self.open(path) {
                    FileStatus::NotFound => format!("File {} could not be opened", path),
                    FileStatus::Ok => format!("File {} opened", path),
                    _ => unreachable!(),
                };
                if empty {
                    self.buffers.delete_buffer(ix);
                }
            }
            Write { path } => {
                if self.options.get("readonly") == Some(true) {
                    // TODO: add override (w!)
                    self.status_bar.msg = format!("File {} is opened in readonly mode", path)
                } else {
                    self.status_bar.msg = match self.write(path) {
                        FileStatus::NotFound => format!("File {} could not be opened", path),
                        FileStatus::Ok => format!("File {} written", path),
                        FileStatus::Other => format!("Couldn't write {}", path),
                    }
                }
            }
            ListBuffers => {
                let description = get_buffers_description(&self.buffers);
                let mut new_buffer: Buffer = SplitBuffer::from_str(&description).into();
                new_buffer.title = Some("<Buffers>".into());
                new_buffer.is_transient = true; // delete the buffer when the user switches away
                let new_buffer_index = self.buffers.new_buffer(new_buffer);
                self.buffers.switch_to(new_buffer_index);
                self.redraw_task = RedrawTask::Full;
            }
            SwitchToBuffer { buffer_index: ix } => if !self.buffers.is_buffer_index_valid(ix) {
                self.status_bar.msg = format!("Invalid buffer #{}", ix);
            } else if self.buffers.current_buffer_index() == ix{
                self.status_bar.msg = format!("Already in buffer #{}", ix);
            } else {
                self.buffers.switch_to(ix);
                self.redraw_task = RedrawTask::Full;
                self.status_bar.msg = format!("Switched to buffer #{}", ix);
            },
            CreateBuffer => {
                let new = self.buffers.new_buffer(Buffer::new());
                self.buffers.switch_to(new);
                self.redraw_task = RedrawTask::Full;
                self.status_bar.msg = format!("Switched to buffer#{}", new);
            }
            DeleteBuffer => {
                let ix = self.buffers.current_buffer_index();
                self.buffers.delete_buffer(ix);
                self.redraw_task = RedrawTask::Full;
            }
            Help => {
                self.open("/apps/sodium/help.txt");
            }
            Quit => {
                exit(0);
            }
        }
        self.hint();
    }
pub fn invoke < ' a > ( & mut self , cmd : PromptCommand < ' a > ) { use self :: PromptCommand :: * ; match cmd { Set { option } => { self . status_bar . msg = match self . options . set ( option ) { Ok ( ( ) ) => format ! ( " " option ) , Err ( ( ) ) => format ! ( " " option ) , } } Unset { option } => { self . status_bar . msg = match self . options . unset ( option ) { Ok ( ( ) ) => format ! ( " " option ) , Err ( ( ) ) => format ! ( " " option ) , } } Toggle { option } => { self . status_bar . msg = match self . options . toggle ( option ) { Ok ( ( ) ) => format ! ( " " option ) , Err ( ( ) ) => format ! ( " " option ) , } } Get { option } => { self . status_bar . msg = match self . options . get ( option ) { Some ( true ) => format ! ( " " option ) , Some ( false ) => format ! ( " " option ) , None => format ! ( " " option ) , } } Open { path } => { let line = self . buffers . current_buffer ( ) . get_line ( 0 ) . unwrap ( ) . len ( ) ; let empty = self . buffers . current_buffer ( ) . len ( ) == 1 && line == 0 ; let ix = self . buffers . current_buffer_index ( ) ; self . status_bar . msg = match self . open ( path ) { FileStatus :: NotFound => format ! ( " " path ) , FileStatus :: Ok => format ! ( " " path ) , _ => unreachable ! ( ) , } ; if empty { self . buffers . delete_buffer ( ix ) ; } } Write { path } => { if self . options . get ( " " ) == Some ( true ) { // TODO: add override (w!) self . status_bar . msg = format ! ( " " path ) } else { self . status_bar . msg = match self . write ( path ) { FileStatus :: NotFound => format ! ( " " path ) , FileStatus :: Ok => format ! ( " " path ) , FileStatus :: Other => format ! ( " " path ) , } } } ListBuffers => { let description = get_buffers_description ( & self . buffers ) ; let mut new_buffer : Buffer = SplitBuffer :: from_str ( & description ) . into ( ) ; new_buffer . title = Some ( " " . into ( ) ) ; new_buffer . is_transient = true ; // delete the buffer when the user switches away let new_buffer_index = self . buffers . new_buffer ( new_buffer ) ; self . buffers . switch_to ( new_buffer_index ) ; self . redraw_task = RedrawTask :: Full ; } SwitchToBuffer { buffer_index : ix } => if ! self . buffers . is_buffer_index_valid ( ix ) { self . status_bar . msg = format ! ( " " ix ) ; } else if self . buffers . current_buffer_index ( ) == ix { self . status_bar . msg = format ! ( " " ix ) ; } else { self . buffers . switch_to ( ix ) ; self . redraw_task = RedrawTask :: Full ; self . status_bar . msg = format ! ( " " ix ) ; } , CreateBuffer => { let new = self . buffers . new_buffer ( Buffer :: new ( ) ) ; self . buffers . switch_to ( new ) ; self . redraw_task = RedrawTask :: Full ; self . status_bar . msg = format ! ( " " new ) ; } DeleteBuffer => { let ix = self . buffers . current_buffer_index ( ) ; self . buffers . delete_buffer ( ix ) ; self . redraw_task = RedrawTask :: Full ; } Help => { self . open ( " " ) ; } Quit => { exit ( 0 ) ; } } self . hint ( ) ; }
Invoke a command in the prompt
Invoke a command in the prompt
&mut self
cmd
PromptCommand<'a>