Skip to content

Exit on EOF (Ctrl-d) - #6

Merged
hanoglu merged 1 commit into
hanoglu:mainfrom
MaxGyver83:feature/exit-on-EOF
Jun 15, 2023
Merged

Exit on EOF (Ctrl-d)#6
hanoglu merged 1 commit into
hanoglu:mainfrom
MaxGyver83:feature/exit-on-EOF

Conversation

@MaxGyver83

Copy link
Copy Markdown
Contributor

Exit on EOF (Ctrl-d)

The exit status of read is 1 in this case. Thus, || break after the read builtin breaks out of the while loop.

Closes #4.

@MaxGyver83

Copy link
Copy Markdown
Contributor Author

Alternative implementation: Ctrl-d has the same effect as abort inside inline prompts and else the same effect as exit:

diff --git a/TermiC.sh b/TermiC.sh
index 298665b..57f5692 100644
--- a/TermiC.sh
+++ b/TermiC.sh
@@ -32,7 +32,7 @@ echo -e  $initSource > $sourceFile

 while true;do
        [[ $inlineCounter -gt 0 ]] && promptPS1="   " || promptPS1=">> "
-       read -rep "$promptPS1"$(echo $(yes ... | head -n $inlineCounter) | sed 's/ //g') prompt
+       read -rep "$promptPS1"$(echo $(yes ... | head -n $inlineCounter) | sed 's/ //g') prompt || { [[ $inlineCounter -gt 0 ]] && prompt="abort" || break; }
        [[ $prompt == "" ]] && continue
        [[ $prompt == "exit" ]] && break
        [[ $prompt == "clear" ]] && :> $sourceFile && :> $sourceFile.tmp && :> $binaryFile && fullPrompt="" && inlineCounter=0 && echo -e  $initSource > $sourceFile && continue

@hanoglu

hanoglu commented Jun 15, 2023

Copy link
Copy Markdown
Owner

That sounds good. I also moved read command to a subshell so that when Ctrl + C combination detected, prompt is cleared like Python terminal.

...
trap "echo -e '\nKeyboardInterrupt'" SIGINT
while true;do
	[[ $inlineCounter -gt 0 ]] && promptPS1="   " || promptPS1=">> "
  prompt=$(read -rep "$promptPS1"$(echo $(yes ... | head -n $inlineCounter) | sed 's/ //g') prompt || { [[ $inlineCounter -gt 0 ]] || prompt="exit"; } ; echo $prompt) 
...

@hanoglu
hanoglu merged commit 787e7ae into hanoglu:main Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TermiC does not exit after EOF, but exits after receiving a SIGINT

2 participants