Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: export history to local history file #1073

Closed
zxh326 opened this issue Jun 25, 2023 · 4 comments
Closed

Feature request: export history to local history file #1073

zxh326 opened this issue Jun 25, 2023 · 4 comments

Comments

@zxh326
Copy link

zxh326 commented Jun 25, 2023

After I set up atuin sync on the new device, I found that fish Auto Suggestions are not working.

Guess it's because there is no command history in the fish's history file.

So, can we add an command to export atuin history to the local history file?

@zxh326
Copy link
Author

zxh326 commented Jun 28, 2023

Wrote a simple script to export history into fish local file.

package main

import (
	"database/sql"
	"fmt"
	"os"

	_ "github.com/mattn/go-sqlite3"
)

var (
	home string
)

func init() {
	home, _ = os.UserHomeDir()
}

func writeFishHistoryFile(t int, cmd string) {
	file, err := os.OpenFile(home+"/.local/share/fish/fish_history", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		panic(err)
	}
	defer file.Close()
	content := fmt.Sprintf("- cmd: %s\n  when: %d\n", cmd, t/1000000000)

	_, err = file.WriteString(content)
	if err != nil {
		panic(err)
	}
}

func main() {
	db, err := sql.Open("sqlite3", home+"/.local/share/atuin/history.db")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer db.Close()

	rows, err := db.Query("SELECT * FROM history ORDER BY timestamp ASC")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer rows.Close()

	for rows.Next() {
		var id, command, cwd, session, hostname string
		var timestamp, duration, exit int
		var deleted_at interface{}

		err = rows.Scan(&id, &timestamp, &duration, &exit, &command, &cwd, &session, &hostname, &deleted_at)
		if err != nil {
			fmt.Println(err)
			continue
		}

		writeFishHistoryFile(timestamp, command)
	}

	err = rows.Err()
	if err != nil {
		fmt.Println(err)
		return
	}
}

@ellie
Copy link
Member

ellie commented Jul 3, 2023

Thank you for the script! Otherwise, this is a a dupe of #816

@ellie ellie closed this as completed Jul 3, 2023
@tjheeta
Copy link

tjheeta commented Jan 27, 2024

$ atuin history list | cut -f2- | rev | cut -f2-|rev gives a .bash_history compatible export by removing the first and last columns.

@potoo0
Copy link

potoo0 commented Apr 10, 2024

for zsh_history

import subprocess
from datetime import datetime

dst = 'zsh_history.out'
cmd = 'atuin history list --format {time}:0;{command}'
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
with open(dst, 'w', encoding='utf8') as f_dst:
    line_continue = False
    while True:
        line = p.stdout.readline()
        if line == b'':
            break

        line = line.decode(encoding='utf-8')
        if line_continue:
            f_dst.write(line)
        else:
            try:
                dt = datetime.strptime(line[0:19], '%Y-%m-%d %H:%M:%S')
                f_dst.write(f': {int(dt.timestamp())}')
                f_dst.write(line[19:])
            except ValueError as e:
                print(f'error={e}, line={line}')
        line_continue = line.rstrip().endswith('\\')

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

No branches or pull requests

4 participants