Skip to content

Commit

Permalink
allow to edit commands, including alias
Browse files Browse the repository at this point in the history
  • Loading branch information
lasantosr committed May 16, 2023
1 parent a58bcc3 commit dcc2039
Show file tree
Hide file tree
Showing 17 changed files with 333 additions and 201 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.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "intelli-shell"
description = "Like IntelliSense, but for shells"
version = "0.2.3"
version = "0.2.4"
edition = "2021"
license = "Apache-2.0"
readme = "README.md"
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ It currently works on Bash, Zsh and Fish and should be compatible with most Linu
- Autocomplete currently typed command
- Full Text Search in both command and description with hashtag support on descriptions
- Find & replace labels of currently typed command
- Edit bookmarked commands and provide aliases
- Non-intrusive (inline) and full-screen interfaces
- Fetch command to parse and store [tldr](https://github.com/tldr-pages/tldr) pages (Thanks to them!)
- Portability. You can use bookmarked commands in any supported shell, as well as exporting and importing elsewhere.
Expand Down Expand Up @@ -133,14 +134,15 @@ You can view supported actions by running `intelli-shell -h`. Most used standalo
- `ctrl + l` replace labels of currently typed command
- `esc` clean current line, this binding can be skipped if `INTELLI_SKIP_ESC_BIND=1`

**Note:** When navigating items, selected suggestion can be deleted with `ctrl + d`
**Note:** When navigating items, selected suggestion can be deleted with `ctrl + d` or edited with any of: `ctrl + e`,
`ctrl + u` or `F2`

You can customize key bindings using environment variables: `INTELLI_BOOKMARK_HOTKEY`, `INTELLI_SEARCH_HOTKEY` and `INTELLI_LABEL_HOTKEY`

## Wishlist

- [x] Labels support to store most used labels and select them using a dedicated UI
- [ ] Usability improvements to manage stored commands (including aliases)
- [x] Usability improvements to manage stored commands (including aliases)
- [x] Support for more terminals
- [x] [Fish](https://fishshell.com/)
- [x] PowerShell
Expand Down
2 changes: 1 addition & 1 deletion intelli-shell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Set-PSReadLineKeyHandler -Chord $IntelliBookmarkChord -BriefDescription "Intelli

$TempFile = New-TemporaryFile
$line = $line -replace '"','""""""""""""'
$Command = 'intelli-shell.exe --file-output=""""' + $TempFile.FullName + '"""" save """"' + $line + '""""'
$Command = 'intelli-shell.exe --file-output=""""' + $TempFile.FullName + '"""" new -c """"' + $line + '""""'
Start-Process powershell.exe -Wait -NoNewWindow -ArgumentList "-command ""$Command""" -RedirectStandardError "NUL"
$IntelliOutput = Get-Content -Raw $TempFile
Remove-Item $TempFile
Expand Down
4 changes: 2 additions & 2 deletions intelli-shell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if [[ -n "$ZSH_VERSION" ]]; then
}

function _intelli_save {
_intelli_exec save "$BUFFER"
_intelli_exec new -c "$BUFFER"
}

function _intelli_label {
Expand Down Expand Up @@ -78,7 +78,7 @@ elif [[ -n "$BASH" ]]; then
}

function _intelli_save {
_intelli_exec save "$READLINE_LINE"
_intelli_exec new -c "$READLINE_LINE"
}

function _intelli_label {
Expand Down
12 changes: 5 additions & 7 deletions src/common/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,8 @@ pub trait InteractiveProcess: Process {
// `ctrl + d` - Delete
KeyCode::Char(c) if has_ctrl && c == 'd' => self.delete_current()?,
// `ctrl + u` | `ctrl + e` | F2 - Edit / Update
KeyCode::F(f) if f == 2 => {
// TODO edit - delegate to process?
}
KeyCode::Char(c) if has_ctrl && (c == 'e' || c == 'u') => {
// TODO edit
}
KeyCode::F(f) if f == 2 => self.edit_current()?,
KeyCode::Char(c) if has_ctrl && (c == 'e' || c == 'u') => self.edit_current()?,
// Selection
KeyCode::Home => self.home(),
KeyCode::End => self.end(),
Expand Down Expand Up @@ -171,8 +167,10 @@ pub trait InteractiveProcess: Process {
/// Removes a character from the currently selected input, if any
fn delete_char(&mut self, backspace: bool) -> Result<()>;

/// Deleted the currently selected item, if any
/// Deletes the currently selected item, if any
fn delete_current(&mut self) -> Result<()>;
/// Edits the currently selected item, if any
fn edit_current(&mut self) -> Result<()>;
/// Accepts the currently selected item, if any
fn accept_current(&mut self) -> Result<Option<ProcessOutput>>;
/// Exits with the current state
Expand Down
13 changes: 8 additions & 5 deletions src/common/widget/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ use crate::{model::Command, theme::Theme};

impl<'a> IntoWidget<ListItem<'a>> for &'a Command {
fn into_widget(self, theme: Theme) -> ListItem<'a> {
let content = Spans::from(vec![
let mut content = vec![
Span::raw(&self.cmd),
Span::styled(" # ", Style::default().fg(theme.secondary)),
Span::styled(&self.description, Style::default().fg(theme.secondary)),
]);
ListItem::new(content)
Span::styled(" # ", Style::default().fg(theme.description)),
Span::styled(&self.description, Style::default().fg(theme.description)),
];
if let Some(alias) = &self.alias {
content.insert(0, Span::styled(format!("[{alias}] "), Style::default().fg(theme.alias)))
}
ListItem::new(Spans::from(content))
}
}
4 changes: 2 additions & 2 deletions src/common/widget/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ impl<'a> IntoCursorWidget<Text<'a>> for &'a LabeledCommand {
.map(|p| {
let span = match p {
CommandPart::Text(t) | CommandPart::LabelValue(t) => {
Span::styled(t, Style::default().fg(theme.disabled))
Span::styled(t, Style::default().fg(theme.secondary))
}
CommandPart::Label(l) => {
let style = if !first_label_found {
first_label_found = true;
first_label_width = l.len_chars() as u16 + 4;
Style::default().fg(theme.main).add_modifier(Modifier::BOLD)
} else {
Style::default().fg(theme.disabled)
Style::default().fg(theme.secondary)
};
Span::styled(format!("{{{{{l}}}}}"), style)
}
Expand Down
9 changes: 8 additions & 1 deletion src/common/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ where
self
}

pub fn set_focus(&mut self, focus: bool) -> &mut Self {
self.focus = focus;
self
}

pub fn inner(&self) -> &T {
&self.text
}
Expand Down Expand Up @@ -136,7 +141,9 @@ where
paragraph = paragraph.block(block);
// Remove borders from max width & height
max_width -= 2;
max_height -= 2;
if max_height > 2 {
max_height -= 2;
}
// Shift offset because of borders
if let (Some(cursor), Some(end_offset)) = (cursor.as_mut(), end_offset.as_mut()) {
cursor.x += 1;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! - Autocomplete currently typed command
//! - Full Text Search in both command and description with hashtag support on descriptions
//! - Find & replace labels of currently typed command
//! - Edit bookmarked commands and provide aliases
//! - Non-intrusive (inline) and full-screen interfaces
//! - Fetch command to parse and store [tldr](https://github.com/tldr-pages/tldr) pages (Thanks to them!)
//! - Portability. You can use bookmarked commands in any supported shell, as well as exporting and importing elsewhere.
Expand Down
26 changes: 16 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crossterm::{
QueueableCommand,
};
use intelli_shell::{
model::AsLabeledCommand,
process::{LabelProcess, SaveCommandProcess, SearchProcess},
model::{AsLabeledCommand, Command},
process::{EditCommandProcess, LabelProcess, SearchProcess},
remove_newlines,
storage::{SqliteStorage, USER_CATEGORY},
theme, ExecutionContext, Process, ProcessOutput,
Expand Down Expand Up @@ -49,10 +49,11 @@ struct Args {
#[derive(Subcommand)]
#[cfg_attr(debug_assertions, derive(Debug))]
enum Actions {
/// Saves a new user command
Save {
/// Stores a new user command
New {
/// Command to be stored
command: String,
#[arg(short, long)]
command: Option<String>,

#[arg(short, long)]
/// Description of the command
Expand Down Expand Up @@ -123,11 +124,16 @@ fn run(cli: Args) -> Result<()> {

// Execute command
let res = match cli.action {
Actions::Save { command, description } => exec(
cli.inline,
cli.inline_extra_line,
SaveCommandProcess::new(&storage, remove_newlines(command), description, context),
),
Actions::New { command, description } => {
let cmd = command.map(remove_newlines);
let description = description.map(remove_newlines);
let command = Command::new(USER_CATEGORY, cmd.unwrap_or_default(), description.unwrap_or_default());
exec(
cli.inline,
cli.inline_extra_line,
EditCommandProcess::new(&storage, command, context)?,
)
}
Actions::Search { filter } => exec(
cli.inline,
cli.inline_extra_line,
Expand Down
Loading

0 comments on commit dcc2039

Please sign in to comment.