Compare commits
2 Commits
b46877edde
...
cdc9e7e266
| Author | SHA1 | Date | |
|---|---|---|---|
| cdc9e7e266 | |||
| 624677b961 |
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
A Linux shell written in Rust. The name is a nod to the original Unix utilities `sh` and `ed`. It's a shell with a heavy emphasis on smooth line editing.
|
A Linux shell written in Rust. The name is a nod to the original Unix utilities `sh` and `ed`. It's a shell with a heavy emphasis on smooth line editing.
|
||||||
|
|
||||||
<img width="506" height="407" alt="shed" src="https://github.com/user-attachments/assets/5333dd47-ae1b-45cd-8729-b623f586b10e" />
|
<img width="506" height="407" alt="shed" src="https://github.com/user-attachments/assets/3945f663-a361-4418-bf20-0c4eaa2a36d2" />
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
@@ -44,8 +44,9 @@ Additionally, `echo` now has a `-p` flag that expands prompt escape sequences, s
|
|||||||
|
|
||||||
`shed` comes with fuzzy completion and history searching out of the box. It has it's own internal fuzzyfinder implementation, so `fzf` is not a dependency.
|
`shed` comes with fuzzy completion and history searching out of the box. It has it's own internal fuzzyfinder implementation, so `fzf` is not a dependency.
|
||||||
|
|
||||||
<img width="773" height="505" alt="shed_comp" src="https://github.com/user-attachments/assets/0078ef5a-ba01-479a-831e-96ae5a25b4e3" />
|
<img width="773" height="505" alt="shed_comp" src="https://github.com/user-attachments/assets/d317387e-4c33-406a-817f-1c183afab749" />
|
||||||
<img width="773" height="536" alt="shed_search" src="https://github.com/user-attachments/assets/7169dacc-a92b-48f7-bf45-0f14a6d38a10" />
|
<img width="773" height="536" alt="shed_search" src="https://github.com/user-attachments/assets/5109eb14-5c33-46bb-ab39-33c60ca039a8" />
|
||||||
|
|
||||||
|
|
||||||
### Keymaps
|
### Keymaps
|
||||||
|
|
||||||
|
|||||||
@@ -1,96 +0,0 @@
|
|||||||
## Prompt example
|
|
||||||
|
|
||||||
This is the `shed` code for the prompt that I currently use. Note that the scripting language for `shed` is essentially identical to bash. This prompt code uses the `\!` escape sequence which lets you use the output of a function as your prompt.
|
|
||||||
|
|
||||||
Also note that in `shed`, the `echo` builtin has a new `-p` flag which expands prompt escape sequences. This allows you to access these escape sequences in any context.
|
|
||||||
|
|
||||||
The end result is the prompt that appears in the README:
|
|
||||||
|
|
||||||
<img width="506" height="407" alt="shed" src="https://github.com/user-attachments/assets/5333dd47-ae1b-45cd-8729-b623f586b10e" />
|
|
||||||
|
|
||||||
```bash
|
|
||||||
prompt_topline() {
|
|
||||||
local user_and_host="\e[0m\e[1m$USER\e[1;36m@\e[1;31m$HOST\e[0m"
|
|
||||||
echo -n "\e[1;34m┏━ $user_and_host\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
prompt_stat_line() {
|
|
||||||
local last_exit_code="$?"
|
|
||||||
local last_cmd_status
|
|
||||||
local last_cmd_runtime
|
|
||||||
if [ "$last_exit_code" -eq "0" ]; then
|
|
||||||
last_cmd_status="\e[1;32m\e[0m"
|
|
||||||
else
|
|
||||||
last_cmd_status="\e[1;31m\e[0m"
|
|
||||||
fi
|
|
||||||
local last_runtime_raw="$(echo -p "\t")"
|
|
||||||
if [ -z "$last_runtime_raw" ]; then
|
|
||||||
return 0
|
|
||||||
else
|
|
||||||
last_cmd_runtime="\e[1;38;2;249;226;175m $(echo -p "\T")\e[0m"
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo -n "\e[1;34m┃ $last_cmd_runtime ($last_cmd_status)\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
prompt_git_line() {
|
|
||||||
git rev-parse --is-inside-work-tree > /dev/null 2>&1 || return
|
|
||||||
|
|
||||||
local gitsigns
|
|
||||||
local status="$(git status --porcelain 2>/dev/null)"
|
|
||||||
local branch="$(git branch --show-current 2>/dev/null)"
|
|
||||||
|
|
||||||
[ -n "$status" ] && echo "$status" | command grep -q '^ [MADR]' && gitsigns="$gitsigns!"
|
|
||||||
[ -n "$status" ] && echo "$status" | command grep -q '^??' && gitsigns="$gitsigns?"
|
|
||||||
[ -n "$status" ] && echo "$status" | command grep -q '^[MADR]' && gitsigns="$gitsigns+"
|
|
||||||
|
|
||||||
local ahead="$(git rev-list --count @{upstream}..HEAD 2>/dev/null)"
|
|
||||||
local behind="$(git rev-list --count HEAD..@{upstream} 2>/dev/null)"
|
|
||||||
[ $ahead -gt 0 ] && gitsigns="$gitsigns↑"
|
|
||||||
[ $behind -gt 0 ] && gitsigns="$gitsigns↓"
|
|
||||||
|
|
||||||
if [ -n "$gitsigns" ] || [ -n "$branch" ]; then
|
|
||||||
if [ -n "$gitsigns" ]; then
|
|
||||||
gitsigns="\e[1;31m[$gitsigns]"
|
|
||||||
fi
|
|
||||||
echo -n "\e[1;34m┃ \e[1;35m ${branch}$gitsigns\e[0m\n"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
prompt_jobs_line() {
|
|
||||||
local job_count="$(echo -p '\j')"
|
|
||||||
if [ "$job_count" -gt 0 ]; then
|
|
||||||
echo -n "\e[1;34m┃ \e[1;33m $job_count job(s) running\e[0m\n"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
prompt_ssh_line() {
|
|
||||||
local ssh_server="$(echo $SSH_CONNECTION | cut -f3 -d' ')"
|
|
||||||
[ -n "$ssh_server" ] && echo -n "\e[1;34m┃ \e[1;39m🌐 $ssh_server\e[0m\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
prompt_pwd_line() {
|
|
||||||
echo -p "\e[1;34m┣━━ \e[1;36m\W\e[1;32m/"
|
|
||||||
}
|
|
||||||
|
|
||||||
prompt_dollar_line() {
|
|
||||||
local dollar="$(echo -p "\$ ")"
|
|
||||||
local dollar="$(echo -e "\e[1;32m$dollar\e[0m")"
|
|
||||||
echo -n "\e[1;34m┗━ $dollar "
|
|
||||||
}
|
|
||||||
|
|
||||||
prompt() {
|
|
||||||
local statline="$(prompt_stat_line)"
|
|
||||||
local topline="$(prompt_topline)"
|
|
||||||
local gitline="$(prompt_git_line)"
|
|
||||||
local jobsline="$(prompt_jobs_line)"
|
|
||||||
local sshline="$(prompt_ssh_line)"
|
|
||||||
local pwdline="$(prompt_pwd_line)"
|
|
||||||
local dollarline="$(prompt_dollar_line)"
|
|
||||||
local prompt="$topline$statline$gitline$jobsline$sshline$pwdline\n$dollarline"
|
|
||||||
|
|
||||||
echo -en "$prompt"
|
|
||||||
}
|
|
||||||
|
|
||||||
export PS1="\!prompt "
|
|
||||||
```
|
|
||||||
200
examples/cool_prompt.sh
Normal file
200
examples/cool_prompt.sh
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
# This is the code for the prompt I currently use
|
||||||
|
# It makes use of the '\@funcname' function expansion escape sequence
|
||||||
|
# and the '-p' flag for echo which expands prompt escape sequences
|
||||||
|
#
|
||||||
|
# The final product looks like this:
|
||||||
|
# ┏━ user@hostname INSERT
|
||||||
|
# ┃ 1ms
|
||||||
|
# ┃ main[!?] ~1 +1 -1
|
||||||
|
# ┃ 1 job(s) running
|
||||||
|
# ┣━━ ~/path/to/pwd/
|
||||||
|
# ┗━ $ $shed 0.5.0 (x86_64 linux)
|
||||||
|
# (The vi mode indicator is styled to match the color of the separators)
|
||||||
|
|
||||||
|
prompt() {
|
||||||
|
local statline="$(prompt_stat_line)"
|
||||||
|
local topline="$(prompt_topline)"
|
||||||
|
local jobsline="$(prompt_jobs_line)"
|
||||||
|
local sshline="$(prompt_ssh_line)"
|
||||||
|
local pwdline="$(prompt_pwd_line)"
|
||||||
|
local dollarline="$(prompt_dollar_line)"
|
||||||
|
local prompt="$topline$statline$PROMPT_GIT_LINE$jobsline$sshline$pwdline\n$dollarline"
|
||||||
|
|
||||||
|
echo -en "$prompt"
|
||||||
|
|
||||||
|
}
|
||||||
|
prompt_dollar_line() {
|
||||||
|
local dollar="$(echo -p "\$ ")"
|
||||||
|
local dollar="$(echo -e "\e[1;32m$dollar\e[0m")"
|
||||||
|
echo -n "\e[1;34m┗━ $dollar "
|
||||||
|
|
||||||
|
}
|
||||||
|
prompt_git_line() {
|
||||||
|
# git is really expensive so we've gotta make these calls count
|
||||||
|
|
||||||
|
# get the status
|
||||||
|
local status="$(git status --porcelain -b 2>/dev/null)" || return
|
||||||
|
|
||||||
|
local branch="" gitsigns="" ahead=0 behind=0
|
||||||
|
# split at the first linebreak
|
||||||
|
local header="${status%%$'\n'*}"
|
||||||
|
|
||||||
|
# cut the '## ' prefix
|
||||||
|
branch="${header#\#\# }"
|
||||||
|
# cut the '..' suffix
|
||||||
|
branch="${branch%%...*}"
|
||||||
|
|
||||||
|
# parse ahead/behind counts
|
||||||
|
case "$header" in
|
||||||
|
*ahead*) ahead="${header#*ahead }"; ahead="${ahead%%[],]*}"; gitsigns="${gitsigns}↑" ;;
|
||||||
|
esac
|
||||||
|
case "$header" in
|
||||||
|
*behind*) behind="${header#*behind }"; behind="${behind%%[],]*}"; gitsigns="${gitsigns}↓" ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# grab gitsigns
|
||||||
|
case "$status" in
|
||||||
|
# unstaged changes
|
||||||
|
*$'\n'" "[MAR]*) gitsigns="${gitsigns}!" ;;
|
||||||
|
esac
|
||||||
|
case "$status" in
|
||||||
|
# untracked files
|
||||||
|
*$'\n'"??"*) gitsigns="${gitsigns}?" ;;
|
||||||
|
esac
|
||||||
|
case "$status" in
|
||||||
|
# deleted files
|
||||||
|
*$'\n'" "[D]*) gitsigns="${gitsigns}" ;;
|
||||||
|
esac
|
||||||
|
case "$status" in
|
||||||
|
# staged changes
|
||||||
|
*$'\n'[MADR]*) gitsigns="${gitsigns}+" ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# unfortunately we need one more git fork
|
||||||
|
local diff="$(git diff --shortstat 2>/dev/null)"
|
||||||
|
|
||||||
|
local changed="" add="" del=""
|
||||||
|
if [ -n "$diff" ]; then
|
||||||
|
changed="${diff%% file*}"; changed="${changed##* }"
|
||||||
|
case "$diff" in
|
||||||
|
*insertion*) add="${diff#*, }"; add="${add%% *}" ;;
|
||||||
|
esac
|
||||||
|
case "$diff" in
|
||||||
|
*deletion*) del="${diff% deletion*}"; del="${del##* }" ;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$gitsigns" ] || [ -n "$branch" ]; then
|
||||||
|
# style gitsigns if not empty
|
||||||
|
[ -n "$gitsigns" ] && gitsigns="\e[1;31m[$gitsigns]"
|
||||||
|
# style changed/deleted/added text
|
||||||
|
[ -n "$changed" ] && [ "$changed" -gt 0 ] && changed="\e[1;34m~$changed \e[0m"
|
||||||
|
[ -n "$add" ] && [ "$add" -gt 0 ] && add="\e[1;32m+$add \e[0m"
|
||||||
|
[ -n "$del" ] && [ "$del" -gt 0 ] && del="\e[1;31m-$del\e[0m"
|
||||||
|
|
||||||
|
# echo the final product
|
||||||
|
echo -n "\e[1;34m┃ \e[1;35m $branch$gitsigns\e[0m $changed$add$del\n"
|
||||||
|
fi
|
||||||
|
|
||||||
|
}
|
||||||
|
prompt_jobs_line() {
|
||||||
|
local job_count="$(echo -p '\j')"
|
||||||
|
if [ "$job_count" -gt 0 ]; then
|
||||||
|
echo -n "\e[1;34m┃ \e[1;33m $job_count job(s) running\e[0m\n"
|
||||||
|
fi
|
||||||
|
|
||||||
|
}
|
||||||
|
prompt_mode() {
|
||||||
|
local mode=""
|
||||||
|
local normal_fg='\e[0m\e[30m\e[1;43m'
|
||||||
|
local normal_bg='\e[0m\e[33m'
|
||||||
|
local insert_fg='\e[0m\e[30m\e[1;46m'
|
||||||
|
local insert_bg='\e[0m\e[36m'
|
||||||
|
local command_fg='\e[0m\e[30m\e[1;42m'
|
||||||
|
local command_bg='\e[0m\e[32m'
|
||||||
|
local visual_fg='\e[0m\e[30m\e[1;45m'
|
||||||
|
local visual_bg='\e[0m\e[35m'
|
||||||
|
local replace_fg='\e[0m\e[30m\e[1;41m'
|
||||||
|
local replace_bg='\e[0m\e[31m'
|
||||||
|
local search_fg='\e[0m\e[30m\e[1;47m'
|
||||||
|
local search_bg='\e[0m\e[39m'
|
||||||
|
local complete_fg='\e[0m\e[30m\e[1;47m'
|
||||||
|
local complete_bg='\e[0m\e[39m'
|
||||||
|
|
||||||
|
# shed exposes it's current vi mode as a variable
|
||||||
|
case "$SHED_VI_MODE" in
|
||||||
|
"NORMAL")
|
||||||
|
mode="$normal_bg${normal_fg}NORMAL$normal_bg\e[0m"
|
||||||
|
;;
|
||||||
|
"INSERT")
|
||||||
|
mode="$insert_bg${insert_fg}INSERT$insert_bg\e[0m"
|
||||||
|
;;
|
||||||
|
"COMMAND")
|
||||||
|
mode="$command_bg${command_fg}COMMAND$command_bg\e[0m"
|
||||||
|
;;
|
||||||
|
"VISUAL")
|
||||||
|
mode="$visual_bg${visual_fg}VISUAL$visual_bg\e[0m"
|
||||||
|
;;
|
||||||
|
"REPLACE")
|
||||||
|
mode="$replace_bg${replace_fg}REPLACE$replace_bg\e[0m"
|
||||||
|
;;
|
||||||
|
"VERBATIM")
|
||||||
|
mode="$replace_bg${replace_fg}VERBATIM$replace_bg\e[0m"
|
||||||
|
;;
|
||||||
|
"COMPLETE")
|
||||||
|
mode="$complete_bg${complete_fg}COMPLETE$complete_bg\e[0m"
|
||||||
|
;;
|
||||||
|
"SEARCH")
|
||||||
|
mode="$search_bg${search_fg}SEARCH$search_bg\e[0m"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
mode=""
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
echo -en "$mode\n"
|
||||||
|
|
||||||
|
}
|
||||||
|
prompt_pwd_line() {
|
||||||
|
# the -p flag exposes prompt escape sequences like '\W'
|
||||||
|
echo -p "\e[1;34m┣━━ \e[1;36m\W\e[1;32m/"
|
||||||
|
|
||||||
|
}
|
||||||
|
prompt_ssh_line() {
|
||||||
|
local ssh_server="$(echo $SSH_CONNECTION | cut -f3 -d' ')"
|
||||||
|
[ -n "$ssh_server" ] && echo -n "\e[1;34m┃ \e[1;39m🌐 $ssh_server\e[0m\n"
|
||||||
|
|
||||||
|
}
|
||||||
|
prompt_stat_line() {
|
||||||
|
local last_exit_code="$?"
|
||||||
|
local last_cmd_status
|
||||||
|
local last_cmd_runtime
|
||||||
|
if [ "$last_exit_code" -eq "0" ]; then
|
||||||
|
last_cmd_status="\e[1;32m"
|
||||||
|
else
|
||||||
|
last_cmd_status="\e[1;31m"
|
||||||
|
fi
|
||||||
|
local last_runtime_raw="$(echo -p "\t")"
|
||||||
|
if [ -z "$last_runtime_raw" ]; then
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
last_cmd_runtime="\e[1;38;2;249;226;175m ${last_cmd_status}$(echo -p "\T")\e[0m"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -n "\e[1;34m┃ $last_cmd_runtime\e[0m\n"
|
||||||
|
|
||||||
|
}
|
||||||
|
prompt_topline() {
|
||||||
|
local user_and_host="\e[0m\e[1m$USER\e[1;36m@\e[1;31m$HOST\e[0m"
|
||||||
|
local mode_text="$(prompt_mode)"
|
||||||
|
echo -n "\e[1;34m┏━ $user_and_host $mode_text\n"
|
||||||
|
|
||||||
|
}
|
||||||
|
shed_ver() {
|
||||||
|
shed --version
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export PS1="\@prompt "
|
||||||
|
# PSR is the text that expands on the right side of the prompt
|
||||||
|
export PSR='\e[36;1m$\@shed_ver\e[0m'
|
||||||
@@ -126,7 +126,7 @@ in
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
default = {};
|
default = [];
|
||||||
description = "Custom keymaps to set when shed starts";
|
description = "Custom keymaps to set when shed starts";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -216,7 +216,7 @@ fn shed_interactive(args: ShedArgs) -> ShResult<()> {
|
|||||||
if let Err(e) = check_signals() {
|
if let Err(e) = check_signals() {
|
||||||
match e.kind() {
|
match e.kind() {
|
||||||
ShErrKind::ClearReadline => {
|
ShErrKind::ClearReadline => {
|
||||||
// Ctrl+C - clear current input and redraw
|
// We got Ctrl+C - clear current input and redraw
|
||||||
readline.reset_active_widget(false)?;
|
readline.reset_active_widget(false)?;
|
||||||
}
|
}
|
||||||
ShErrKind::CleanExit(code) => {
|
ShErrKind::CleanExit(code) => {
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ use crate::{
|
|||||||
utils::RedirVecUtils,
|
utils::RedirVecUtils,
|
||||||
},
|
},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
procio::{IoMode, IoStack},
|
procio::{IoMode, IoStack, PipeGenerator},
|
||||||
state::{
|
state::{
|
||||||
self, ShFunc, VarFlags, VarKind, read_logic, read_shopts, write_jobs, write_logic, write_vars,
|
self, ShFunc, VarFlags, VarKind, read_logic, read_shopts, write_jobs, write_logic, write_vars,
|
||||||
},
|
},
|
||||||
@@ -319,22 +319,17 @@ impl Dispatcher {
|
|||||||
};
|
};
|
||||||
let name = self.source_name.clone();
|
let name = self.source_name.clone();
|
||||||
|
|
||||||
|
self.io_stack.append_to_frame(subsh.redirs);
|
||||||
|
let _guard = self.io_stack.pop_frame().redirect()?;
|
||||||
|
|
||||||
self.run_fork("anonymous_subshell", |s| {
|
self.run_fork("anonymous_subshell", |s| {
|
||||||
if let Err(e) = s.set_assignments(assignments, AssignBehavior::Export) {
|
if let Err(e) = s.set_assignments(assignments, AssignBehavior::Export) {
|
||||||
e.print_error();
|
e.print_error();
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
s.io_stack.append_to_frame(subsh.redirs);
|
|
||||||
let mut argv = match prepare_argv(argv) {
|
|
||||||
Ok(argv) => argv,
|
|
||||||
Err(e) => {
|
|
||||||
e.try_blame(blame).print_error();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let subsh = argv.remove(0);
|
let subsh_raw = argv[0].span.as_str();
|
||||||
let subsh_body = subsh.0.to_string();
|
let subsh_body = subsh_raw[1..subsh_raw.len() - 1].to_string(); // Remove surrounding parentheses
|
||||||
|
|
||||||
if let Err(e) = exec_input(subsh_body, None, s.interactive, Some(name)) {
|
if let Err(e) = exec_input(subsh_body, None, s.interactive, Some(name)) {
|
||||||
e.print_error();
|
e.print_error();
|
||||||
@@ -385,7 +380,7 @@ impl Dispatcher {
|
|||||||
func_body.body_mut().propagate_context(func_ctx);
|
func_body.body_mut().propagate_context(func_ctx);
|
||||||
func_body.body_mut().flags = func.flags;
|
func_body.body_mut().flags = func.flags;
|
||||||
|
|
||||||
if let Err(e) = self.exec_brc_grp(func_body.body().clone()) {
|
if let Err(e) = self.exec_pipeline(func_body.body().clone()) {
|
||||||
match e.kind() {
|
match e.kind() {
|
||||||
ShErrKind::FuncReturn(code) => {
|
ShErrKind::FuncReturn(code) => {
|
||||||
state::set_status(*code);
|
state::set_status(*code);
|
||||||
@@ -409,11 +404,15 @@ impl Dispatcher {
|
|||||||
}
|
}
|
||||||
fn exec_brc_grp(&mut self, brc_grp: Node) -> ShResult<()> {
|
fn exec_brc_grp(&mut self, brc_grp: Node) -> ShResult<()> {
|
||||||
let NdRule::BraceGrp { body } = brc_grp.class else {
|
let NdRule::BraceGrp { body } = brc_grp.class else {
|
||||||
unreachable!()
|
unreachable!("expected BraceGrp node, got {:?}", brc_grp.class)
|
||||||
};
|
};
|
||||||
|
if self.interactive {
|
||||||
|
log::debug!("Executing brace group, body: {:?}", body);
|
||||||
|
}
|
||||||
let fork_builtins = brc_grp.flags.contains(NdFlags::FORK_BUILTINS);
|
let fork_builtins = brc_grp.flags.contains(NdFlags::FORK_BUILTINS);
|
||||||
|
|
||||||
self.io_stack.append_to_frame(brc_grp.redirs);
|
self.io_stack.append_to_frame(brc_grp.redirs);
|
||||||
|
if self.interactive {}
|
||||||
let guard = self.io_stack.pop_frame().redirect()?;
|
let guard = self.io_stack.pop_frame().redirect()?;
|
||||||
let brc_grp_logic = |s: &mut Self| -> ShResult<()> {
|
let brc_grp_logic = |s: &mut Self| -> ShResult<()> {
|
||||||
for node in body {
|
for node in body {
|
||||||
@@ -705,40 +704,57 @@ impl Dispatcher {
|
|||||||
let NdRule::Pipeline { cmds } = pipeline.class else {
|
let NdRule::Pipeline { cmds } = pipeline.class else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
};
|
};
|
||||||
|
if self.interactive {
|
||||||
|
log::debug!("Executing pipeline, cmds: {:#?}", cmds);
|
||||||
|
}
|
||||||
|
let is_bg = pipeline.flags.contains(NdFlags::BACKGROUND);
|
||||||
self.job_stack.new_job();
|
self.job_stack.new_job();
|
||||||
let fork_builtin = cmds.len() > 1; // If there's more than one command, we need to fork builtins
|
if cmds.len() == 1 {
|
||||||
|
self.fg_job = !is_bg && self.interactive;
|
||||||
|
let cmd = cmds.into_iter().next().unwrap();
|
||||||
|
self.dispatch_node(cmd)?;
|
||||||
|
|
||||||
|
// Give the pipeline terminal control as soon as the first child
|
||||||
|
// establishes the PGID, so later children (e.g. nvim) don't get
|
||||||
|
// SIGTTOU when they try to modify terminal attributes.
|
||||||
|
// Only for interactive (top-level) pipelines — command substitution
|
||||||
|
// and other non-interactive contexts must not steal the terminal.
|
||||||
|
if !is_bg
|
||||||
|
&& self.interactive
|
||||||
|
&& let Some(pgid) = self.job_stack.curr_job_mut().unwrap().pgid()
|
||||||
|
{
|
||||||
|
attach_tty(pgid).ok();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
let (mut in_redirs, mut out_redirs) = self.io_stack.pop_frame().redirs.split_by_channel();
|
let (mut in_redirs, mut out_redirs) = self.io_stack.pop_frame().redirs.split_by_channel();
|
||||||
|
|
||||||
// Zip the commands and their respective pipes into an iterator
|
let mut pipes = PipeGenerator::new(cmds.len()).as_io_frames();
|
||||||
let pipes_and_cmds = get_pipe_stack(cmds.len()).into_iter().zip(cmds);
|
|
||||||
|
|
||||||
let is_bg = pipeline.flags.contains(NdFlags::BACKGROUND);
|
|
||||||
self.fg_job = !is_bg && self.interactive;
|
self.fg_job = !is_bg && self.interactive;
|
||||||
let mut tty_attached = false;
|
let mut tty_attached = false;
|
||||||
|
|
||||||
for ((rpipe, wpipe), mut cmd) in pipes_and_cmds {
|
let last_cmd = cmds.len() - 1;
|
||||||
if let Some(pipe) = rpipe {
|
for (i, mut cmd) in cmds.into_iter().enumerate() {
|
||||||
self.io_stack.push_to_frame(pipe);
|
let mut frame = pipes.next().ok_or_else(|| {
|
||||||
} else {
|
ShErr::at(
|
||||||
|
ShErrKind::InternalErr,
|
||||||
|
cmd.get_span(),
|
||||||
|
"failed to set up pipeline redirections".to_string(),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
if i == 0 {
|
||||||
for redir in std::mem::take(&mut in_redirs) {
|
for redir in std::mem::take(&mut in_redirs) {
|
||||||
self.io_stack.push_to_frame(redir);
|
frame.push(redir);
|
||||||
}
|
}
|
||||||
}
|
} else if i == last_cmd {
|
||||||
if let Some(pipe) = wpipe {
|
|
||||||
self.io_stack.push_to_frame(pipe);
|
|
||||||
if cmd.flags.contains(NdFlags::PIPE_ERR) {
|
|
||||||
let err_redir = Redir::new(IoMode::Fd { tgt_fd: STDERR_FILENO, src_fd: STDOUT_FILENO }, RedirType::Output);
|
|
||||||
self.io_stack.push_to_frame(err_redir);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for redir in std::mem::take(&mut out_redirs) {
|
for redir in std::mem::take(&mut out_redirs) {
|
||||||
self.io_stack.push_to_frame(redir);
|
frame.push(redir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if fork_builtin {
|
let _guard = frame.redirect()?;
|
||||||
cmd.flags |= NdFlags::FORK_BUILTINS;
|
|
||||||
}
|
cmd.flags |= NdFlags::FORK_BUILTINS; // multiple cmds means builtins must fork
|
||||||
self.dispatch_node(cmd)?;
|
self.dispatch_node(cmd)?;
|
||||||
|
|
||||||
// Give the pipeline terminal control as soon as the first child
|
// Give the pipeline terminal control as soon as the first child
|
||||||
@@ -755,6 +771,7 @@ impl Dispatcher {
|
|||||||
tty_attached = true;
|
tty_attached = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
let job = self.job_stack.finalize_job().unwrap();
|
let job = self.job_stack.finalize_job().unwrap();
|
||||||
dispatch_job(job, is_bg, self.interactive)?;
|
dispatch_job(job, is_bg, self.interactive)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -818,7 +835,15 @@ impl Dispatcher {
|
|||||||
|
|
||||||
// Set up redirections here so we can attach the guard to propagated errors.
|
// Set up redirections here so we can attach the guard to propagated errors.
|
||||||
self.io_stack.append_to_frame(mem::take(&mut cmd.redirs));
|
self.io_stack.append_to_frame(mem::take(&mut cmd.redirs));
|
||||||
let redir_guard = self.io_stack.pop_frame().redirect()?;
|
let frame = self.io_stack.pop_frame();
|
||||||
|
if self.interactive {
|
||||||
|
log::debug!(
|
||||||
|
"popped frame for builtin '{}', frame: {:#?}",
|
||||||
|
cmd_raw,
|
||||||
|
frame
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let redir_guard = frame.redirect()?;
|
||||||
|
|
||||||
// Register ChildProc in current job
|
// Register ChildProc in current job
|
||||||
let job = self.job_stack.curr_job_mut().unwrap();
|
let job = self.job_stack.curr_job_mut().unwrap();
|
||||||
|
|||||||
@@ -87,7 +87,6 @@ impl ParsedSrc {
|
|||||||
Err(error) => return Err(vec![error]),
|
Err(error) => return Err(vec![error]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log::debug!("Tokens: {:#?}", tokens);
|
|
||||||
|
|
||||||
let mut errors = vec![];
|
let mut errors = vec![];
|
||||||
let mut nodes = vec![];
|
let mut nodes = vec![];
|
||||||
@@ -214,9 +213,7 @@ impl Node {
|
|||||||
assign_node.walk_tree(f);
|
assign_node.walk_tree(f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NdRule::Pipeline {
|
NdRule::Pipeline { ref mut cmds } => {
|
||||||
ref mut cmds,
|
|
||||||
} => {
|
|
||||||
for cmd_node in cmds {
|
for cmd_node in cmds {
|
||||||
cmd_node.walk_tree(f);
|
cmd_node.walk_tree(f);
|
||||||
}
|
}
|
||||||
@@ -642,7 +639,7 @@ pub enum NdRule {
|
|||||||
argv: Vec<Tk>,
|
argv: Vec<Tk>,
|
||||||
},
|
},
|
||||||
Pipeline {
|
Pipeline {
|
||||||
cmds: Vec<Node>
|
cmds: Vec<Node>,
|
||||||
},
|
},
|
||||||
Conjunction {
|
Conjunction {
|
||||||
elements: Vec<ConjunctNode>,
|
elements: Vec<ConjunctNode>,
|
||||||
@@ -778,6 +775,9 @@ impl ParseStream {
|
|||||||
/// appearing at the bottom The check_pipelines parameter is used to prevent
|
/// appearing at the bottom The check_pipelines parameter is used to prevent
|
||||||
/// left-recursion issues in self.parse_pipeln()
|
/// left-recursion issues in self.parse_pipeln()
|
||||||
fn parse_block(&mut self, check_pipelines: bool) -> ShResult<Option<Node>> {
|
fn parse_block(&mut self, check_pipelines: bool) -> ShResult<Option<Node>> {
|
||||||
|
if check_pipelines {
|
||||||
|
try_match!(self.parse_pipeln()?);
|
||||||
|
} else {
|
||||||
try_match!(self.parse_func_def()?);
|
try_match!(self.parse_func_def()?);
|
||||||
try_match!(self.parse_brc_grp(false /* from_func_def */)?);
|
try_match!(self.parse_brc_grp(false /* from_func_def */)?);
|
||||||
try_match!(self.parse_case()?);
|
try_match!(self.parse_case()?);
|
||||||
@@ -785,9 +785,6 @@ impl ParseStream {
|
|||||||
try_match!(self.parse_for()?);
|
try_match!(self.parse_for()?);
|
||||||
try_match!(self.parse_if()?);
|
try_match!(self.parse_if()?);
|
||||||
try_match!(self.parse_test()?);
|
try_match!(self.parse_test()?);
|
||||||
if check_pipelines {
|
|
||||||
try_match!(self.parse_pipeln()?);
|
|
||||||
} else {
|
|
||||||
try_match!(self.parse_cmd()?);
|
try_match!(self.parse_cmd()?);
|
||||||
}
|
}
|
||||||
Ok(None)
|
Ok(None)
|
||||||
@@ -1457,7 +1454,8 @@ impl ParseStream {
|
|||||||
node_tks.push(tk.clone());
|
node_tks.push(tk.clone());
|
||||||
flags |= NdFlags::BACKGROUND;
|
flags |= NdFlags::BACKGROUND;
|
||||||
break;
|
break;
|
||||||
} else if (!matches!(*self.next_tk_class(),TkRule::Pipe | TkRule::ErrPipe)) || is_punctuated {
|
} else if (!matches!(*self.next_tk_class(), TkRule::Pipe | TkRule::ErrPipe)) || is_punctuated
|
||||||
|
{
|
||||||
break;
|
break;
|
||||||
} else if let Some(pipe) = self.next_tk() {
|
} else if let Some(pipe) = self.next_tk() {
|
||||||
node_tks.push(pipe)
|
node_tks.push(pipe)
|
||||||
@@ -1470,9 +1468,7 @@ impl ParseStream {
|
|||||||
} else {
|
} else {
|
||||||
Ok(Some(Node {
|
Ok(Some(Node {
|
||||||
// TODO: implement pipe_err support
|
// TODO: implement pipe_err support
|
||||||
class: NdRule::Pipeline {
|
class: NdRule::Pipeline { cmds },
|
||||||
cmds,
|
|
||||||
},
|
|
||||||
flags,
|
flags,
|
||||||
redirs: vec![],
|
redirs: vec![],
|
||||||
context: self.context.clone(),
|
context: self.context.clone(),
|
||||||
@@ -1867,9 +1863,7 @@ where
|
|||||||
check_node(assign_node, filter, operation);
|
check_node(assign_node, filter, operation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NdRule::Pipeline {
|
NdRule::Pipeline { ref mut cmds } => {
|
||||||
ref mut cmds,
|
|
||||||
} => {
|
|
||||||
for cmd_node in cmds {
|
for cmd_node in cmds {
|
||||||
check_node(cmd_node, filter, operation);
|
check_node(cmd_node, filter, operation);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
|
iter::Map,
|
||||||
ops::{Deref, DerefMut},
|
ops::{Deref, DerefMut},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -220,12 +221,6 @@ impl<'e> IoFrame {
|
|||||||
let tgt_fd = io_mode.tgt_fd();
|
let tgt_fd = io_mode.tgt_fd();
|
||||||
let src_fd = io_mode.src_fd();
|
let src_fd = io_mode.src_fd();
|
||||||
dup2(src_fd, tgt_fd)?;
|
dup2(src_fd, tgt_fd)?;
|
||||||
// Close the original pipe fd after dup2 — it's been duplicated to
|
|
||||||
// tgt_fd and keeping it open prevents SIGPIPE delivery in pipelines.
|
|
||||||
// We replace the IoMode to drop the Arc<OwnedFd>, which closes the fd.
|
|
||||||
if matches!(io_mode, IoMode::Pipe { .. }) {
|
|
||||||
*io_mode = IoMode::Close { tgt_fd };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(RedirGuard::new(self))
|
Ok(RedirGuard::new(self))
|
||||||
}
|
}
|
||||||
@@ -337,3 +332,56 @@ impl From<Vec<IoFrame>> for IoStack {
|
|||||||
pub fn borrow_fd<'f>(fd: i32) -> BorrowedFd<'f> {
|
pub fn borrow_fd<'f>(fd: i32) -> BorrowedFd<'f> {
|
||||||
unsafe { BorrowedFd::borrow_raw(fd) }
|
unsafe { BorrowedFd::borrow_raw(fd) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct PipeGenerator {
|
||||||
|
num_cmds: usize,
|
||||||
|
cursor: usize,
|
||||||
|
last_rpipe: Option<Redir>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PipeGenerator {
|
||||||
|
pub fn new(num_cmds: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
num_cmds,
|
||||||
|
cursor: 0,
|
||||||
|
last_rpipe: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn as_io_frames(self) -> Map<Self, fn((Option<Redir>, Option<Redir>)) -> IoFrame> {
|
||||||
|
self.map(|(r, w)| {
|
||||||
|
let mut frame = IoFrame::new();
|
||||||
|
if let Some(r) = r {
|
||||||
|
frame.push(r);
|
||||||
|
}
|
||||||
|
if let Some(w) = w {
|
||||||
|
frame.push(w);
|
||||||
|
}
|
||||||
|
frame
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for PipeGenerator {
|
||||||
|
type Item = (Option<Redir>, Option<Redir>);
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
if self.cursor == self.num_cmds {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
if self.cursor + 1 == self.num_cmds {
|
||||||
|
if self.num_cmds == 1 {
|
||||||
|
return None;
|
||||||
|
} else {
|
||||||
|
self.cursor += 1;
|
||||||
|
return Some((self.last_rpipe.take(), None));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let (r, w) = IoMode::get_pipes();
|
||||||
|
let mut rpipe = Some(Redir::new(r, RedirType::Input));
|
||||||
|
std::mem::swap(&mut self.last_rpipe, &mut rpipe);
|
||||||
|
|
||||||
|
let wpipe = Redir::new(w, RedirType::Output);
|
||||||
|
|
||||||
|
self.cursor += 1;
|
||||||
|
Some((rpipe, Some(wpipe)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -639,6 +639,10 @@ impl ShedVi {
|
|||||||
.update_pending_cmd((self.editor.as_str(), self.editor.cursor.get()));
|
.update_pending_cmd((self.editor.as_str(), self.editor.cursor.get()));
|
||||||
let hint = self.history.get_hint();
|
let hint = self.history.get_hint();
|
||||||
self.editor.set_hint(hint);
|
self.editor.set_hint(hint);
|
||||||
|
|
||||||
|
// If we are here, we hit a case where pressing tab returned a single candidate
|
||||||
|
// So we can just go ahead and reset the completer after this
|
||||||
|
self.completer.reset();
|
||||||
}
|
}
|
||||||
Ok(None) => {
|
Ok(None) => {
|
||||||
let post_cmds = read_logic(|l| l.get_autocmds(AutoCmdKind::OnCompletionStart));
|
let post_cmds = read_logic(|l| l.get_autocmds(AutoCmdKind::OnCompletionStart));
|
||||||
|
|||||||
Reference in New Issue
Block a user