Replies: 3 comments
-
Looked into this quite a bit more recently. I managed to get really close to using print -s or history -s using syscalls in Golang. The only issue is: But now I know that it may be possible. We would just need the equivalent translation of print or history in Golang, or some system binary that can do this maybe? func printCommandOutput(command string) error {
// Look for the "print" command in the PATH
printBinary, lookErr := exec.LookPath("print")
if lookErr != nil {
return fmt.Errorf("print command not found in PATH: %w", lookErr)
}
// Create the command with arguments
args := []string{"-s", command}
// Get the environment variables
env := os.Environ()
// Execute the command without forking
err := syscall.Exec(printBinary, args, env)
if err != nil {
return fmt.Errorf("failed to execute print command: %w", err)
}
return nil
}
func execute(cmd *cobra.Command, args []string) (err error) {
flag := config.Flag
var options []string
if flag.Query != "" {
options = append(options, fmt.Sprintf("--query %s", shellescape.Quote(flag.Query)))
}
commands, err := filter(options, flag.FilterTag)
if err != nil {
return err
}
command := strings.Join(commands, "; ")
if config.Flag.Command {
fmt.Printf("> %s\n", command)
}
err = printCommandOutput(command)
if err != nil {
fmt.Println(color.YellowString("Cannot save the command to history, 'print' command not found"))
if config.Flag.Debug {
fmt.Println(color.RedString(err.Error()))
}
}
return run(command, os.Stdin, os.Stdout)
} |
Beta Was this translation helpful? Give feedback.
-
Or not, given that the history list is maintained in memory https://github.com/zsh-users/zsh/blob/791aaf88cca53036d19ad2e247c783744ccf3837/Src/hist.c#L1387 and we'd somehow have to know where it is and inject into this memory space from an external binary 😬 |
Beta Was this translation helpful? Give feedback.
-
I have one last idea:
It just won't work if you're executing pet as Took me a while to think along the lines of modifying zsh config, still not an expert at developing pet 😄 What do you think @knqyf263? You definitely know more about what's possible here! |
Beta Was this translation helpful? Give feedback.
-
Looking into this a little:
Two kinds of bash histories: Session history (in RAM) and stored history (in file zsh_history, bash_history, etc.) Good reference
Managed to inject the command into shell history file (unix only for now), but the problem is that it still doesn't show up in the session history. To do that we need to copy the file into session history with
history -r;
which is a no-go.A way to inject into session history directly is
print -s *command*
(zsh) orhistory -s *command*
(bash). But this doesn't work when done from inside pet. Perhaps it's a subshell issue where the history is different from the parent shell that ran pet.Need a way to manipulate parent shell from subshell.
After spending an hour on this, seems pretty hard to do @sgohl
Beta Was this translation helpful? Give feedback.
All reactions