Initial non-unit tested version of fish support

This commit is contained in:
David Dworken 2022-10-18 19:55:41 -07:00
parent b31d35685c
commit abe231f5b0
4 changed files with 83 additions and 1 deletions

View File

@ -1961,3 +1961,5 @@ func FuzzTestMultipleUsers(f *testing.F) {
fuzzTest(t, zshTester{}, input)
})
}
// TODO: somehow test/confirm that hishtory works even if only bash/only zsh is installed

23
client/lib/config.fish Normal file
View File

@ -0,0 +1,23 @@
function _hishtory_post_exec --on-event fish_postexec
# Runs after <ENTER>, but before the command is executed
set --global _hishtory_command $argv
set --global _hishtory_start_time (date +%s)
end
set --global _hishtory_first_prompt 1
function __hishtory_on_prompt --on-event fish_prompt
# Runs after the command is executed in order to render the prompt
# $? contains the exit code
set _hishtory_exit_code $status
if [ -n "$_hishtory_first_prompt" ]
set --global -e _hishtory_first_prompt
else if [ -n "$_hishtory_command" ]
hishtory saveHistoryEntry fish $_hishtory_exit_code "$_hishtory_command" $_hishtory_start_time &
end
end
# TODO: support background commands, see https://github.com/fish-shell/fish-shell/issues/3894
# TODO: control-r search is currently unsupported for fish
[ "$(hishtory config-get enable-control-r)" = true ] && echo "Sorry, hishtory does not currently support overriding control-r for fish"

View File

@ -49,6 +49,9 @@ var ConfigZshContents string
//go:embed test_config.zsh
var TestConfigZshContents string
//go:embed config.fish
var ConfigFishContents string
var Version string = "Unknown"
// 256KB ought to be enough for any reasonable cmd
@ -129,7 +132,7 @@ func BuildHistoryEntry(ctx *context.Context, args []string) (*data.HistoryEntry,
return nil, err
}
entry.Command = cmd
} else if shell == "zsh" {
} else if shell == "zsh" || shell == "fish" {
cmd := strings.TrimSuffix(strings.TrimSuffix(args[4], "\n"), " ")
if strings.HasPrefix(cmd, " ") {
// Don't save commands that start with a space
@ -533,6 +536,10 @@ func Install() error {
if err != nil {
return err
}
err = configureFish(homedir, path)
if err != nil {
return err
}
_, err = hctx.GetConfig()
if err != nil {
// No config, so set up a new installation
@ -541,6 +548,54 @@ func Install() error {
return nil
}
// TODO: deduplicate shell config code
func configureFish(homedir, binaryPath string) error {
// Check if fish is installed
_, err := exec.LookPath("fish")
if err != nil {
return nil
}
// Create the file we're going to source. Do this no matter what in case there are updates to it.
fishConfigPath := path.Join(homedir, shared.HISHTORY_PATH, "config.fish")
configContents := ConfigFishContents
err = ioutil.WriteFile(fishConfigPath, []byte(configContents), 0o644)
if err != nil {
return fmt.Errorf("failed to write config.zsh file: %v", err)
}
// Check if we need to configure the fishrc
fishIsConfigured, err := isFishConfigured(homedir)
if err != nil {
return fmt.Errorf("failed to check ~/.config/fish/config.fish: %v", err)
}
if fishIsConfigured {
return nil
}
// Add to fishrc
f, err := os.OpenFile(path.Join(homedir, ".config/fish/config.fish"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644)
if err != nil {
return fmt.Errorf("failed to append to ~/.config/fish/config.fish: %v", err)
}
defer f.Close()
_, err = f.WriteString("\n# Hishtory Config:\nexport PATH=\"$PATH:" + path.Join(homedir, shared.HISHTORY_PATH) + "\"\nsource " + fishConfigPath + "\n")
if err != nil {
return fmt.Errorf("failed to append to zshrc: %v", err)
}
return nil
}
func isFishConfigured(homedir string) (bool, error) {
_, err := os.Stat(path.Join(homedir, ".config/fish/config.fish"))
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
bashrc, err := ioutil.ReadFile(path.Join(homedir, ".config/fish/config.fish"))
if err != nil {
return false, fmt.Errorf("failed to read ~/.config/fish/config.fish: %v", err)
}
return strings.Contains(string(bashrc), "# Hishtory Config:"), nil
}
func configureZshrc(homedir, binaryPath string) error {
// Create the file we're going to source in our zshrc. Do this no matter what in case there are updates to it.
zshConfigPath := path.Join(homedir, shared.HISHTORY_PATH, "config.zsh")

View File

@ -34,6 +34,7 @@ func ResetLocalState(t *testing.T) {
_ = os.Remove(path.Join(homedir, HISHTORY_PATH, "hishtory"))
_ = os.Remove(path.Join(homedir, HISHTORY_PATH, "config.sh"))
_ = os.Remove(path.Join(homedir, HISHTORY_PATH, "config.zsh"))
_ = os.Remove(path.Join(homedir, HISHTORY_PATH, "config.fish"))
}
func BackupAndRestore(t *testing.T) func() {
@ -67,6 +68,7 @@ func BackupAndRestoreWithId(t *testing.T, id string) func() {
path.Join(homedir, HISHTORY_PATH, "hishtory"),
path.Join(homedir, HISHTORY_PATH, "config.sh"),
path.Join(homedir, HISHTORY_PATH, "config.zsh"),
path.Join(homedir, HISHTORY_PATH, "config.fish"),
path.Join(homedir, ".bash_history"),
path.Join(homedir, ".zsh_history"),
}