fix: stdin handling and exit command (#58)

### Summary of Changes
Fixes **#53** (*Fish shell integration panics with `inappropriate ioctl
for device`*).
#### 1. Fixed Non-TTY Stdin (`panic: inappropriate ioctl for device`) —
Fixes #53
- **Root Cause**: When executing `iris init fish | source` (or sourcing
shell configs), `os.Stdin` (FD 0) is connected to a pipe instead of a
TTY. Calling `term.MakeRaw()` on
a pipe returns `ENOTTY`.
- **Fix**: Guarded `term.MakeRaw()` with `term.IsTerminal()` checks
across `root/wrapper.go` and `root/root.go`. Added fallback to open and
use `/dev/tty` when `os.Stdin` is
  not a terminal device.
- **Fish IPC Hooks**: Added `fish_preexec` and `fish_postexec` event
handlers to `iris init fish` to send `IRIS_CMD_START` / `IRIS_CMD_STOP`
IPC signals over `$IRIS_FD`.
  
#### 2. Fixed Shell Setup Order (Duplicate `fastfetch` / startup
executions) — Fixes #53
- **Root Cause**: `iris setup` appended the init script to the bottom of
shell configs (`config.fish`, `.zshrc`, `.bashrc`), causing pre-existing
interactive startup commands
  to run twice.
- **Fix**: Updated `setupCmd` in `root/init.go` to **prepend** `# Iris
Autocomplete` to the top of shell configuration files.
  
#### 3. Fixed Terminal Exit Loop Handling — Relates to #50
- **Root Cause**: When typing `exit` in the wrapped shell, Linux PTY
returns `syscall.EIO` (`Input/output error`) on `ptmx.Read()` rather
than `io.EOF`. `runWrapper()` was
ignoring non-EOF errors and looping indefinitely without restoring the
terminal or calling `os.Exit(0)`.
- **Fix**: Updated the `ptmx.Read` error handler in `root/wrapper.go` to
handle `syscall.EIO` and exit cleanly.
  
   ---
  
  ### Issues Addressed
   - Closes #53
   - Relates to #50, #55
  
   ---
  
  ### Verification Tested
- [x] Verified `iris setup fish` / `iris init fish | source` initializes
without `ioctl` panic.
- [x] Verified typing `exit` in Fish, Zsh, and Bash terminates IRIS
cleanly and restores standard terminal state.
   - [x] Ran `go test ./...` — all tests pass.

---------

Co-authored-by: verse91 <versedev.store@proton.me>
Co-authored-by: VERSE <sunnyone0901@gmail.com>
This commit is contained in:
Ashvin Singh
2026-07-29 21:28:48 +07:00
committed by GitHub
co-authored by verse91 VERSE
parent d6cfb9c909
commit d73921e419
4 changed files with 83 additions and 30 deletions
+26 -9
View File
@@ -31,7 +31,7 @@ if [ -n "$TMUX" ] && [ -n "$IRIS_PID" ]; then
fi
fi
if [ -z "$IRIS_PID" ]; then
if [ -z "$IRIS_PID" ] && [ -z "$IRIS_RESCUE" ]; then
export IRIS_ACTIVE_SHELL="zsh"
exec iris
fi
@@ -67,7 +67,7 @@ if [ -n "$TMUX" ] && [ -n "$IRIS_PID" ]; then
fi
fi
if [ -z "$IRIS_PID" ]; then
if [ -z "$IRIS_PID" ] && [ -z "$IRIS_RESCUE" ]; then
export IRIS_ACTIVE_SHELL="bash"
exec iris
fi
@@ -83,10 +83,20 @@ if set -q TMUX; and set -q IRIS_PID
end
end
if not set -q IRIS_PID
if status is-interactive; and not set -q IRIS_PID; and not set -q IRIS_RESCUE
set -gx IRIS_ACTIVE_SHELL "fish"
exec iris
end
# Iris Autocomplete Hook
if set -q IRIS_PID; and set -q IRIS_FD
function _iris_fish_postexec --on-event fish_postexec
echo -n -e "IRIS_CMD_STOP\x00" >&$IRIS_FD 2>/dev/null
end
function _iris_fish_preexec --on-event fish_preexec
echo -n -e "IRIS_CMD_START\x00" >&$IRIS_FD 2>/dev/null
end
end
`)
}
},
@@ -149,19 +159,26 @@ var setupCmd = &cobra.Command{
return
}
content, _ := os.ReadFile(configFile)
content, readErr := os.ReadFile(configFile)
if strings.Contains(string(content), "iris init") {
fmt.Printf("Iris is already configured in %s\n", configFile)
} else {
f, err := os.OpenFile(configFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
var newContent string
if readErr == nil && len(content) > 0 {
newContent = "# Iris Autocomplete\n" + evalCmd + "\n\n" + string(content)
} else {
newContent = "# Iris Autocomplete\n" + evalCmd + "\n"
}
if mkdirErr := os.MkdirAll(filepath.Dir(configFile), 0755); mkdirErr != nil {
fmt.Printf("Failed to create directory for %s: %v\n", configFile, mkdirErr)
return
}
err = os.WriteFile(configFile, []byte(newContent), 0644)
if err != nil {
fmt.Printf("Failed to update %s: %v\n", configFile, err)
return
}
defer func() { _ = f.Close() }()
_, _ = f.WriteString("\n# Iris Autocomplete\n" + evalCmd + "\n")
fmt.Printf("✓ Added iris integration to %s\n", configFile)
fmt.Printf("✓ Added iris integration to top of %s\n", configFile)
}
// initialize default config file if it does not exist