-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfish-history-to-zsh-history.py
159 lines (126 loc) · 4.79 KB
/
fish-history-to-zsh-history.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
import os
import re
regexp_fish_history_entries = re.compile(r"^[ -] cmd: (?P<cmd>.+)\s+when: (?P<when>[0-9]+)", re.MULTILINE)
ZSH_HISTORY_ENTRY = ": {}:0;{}\n"
MIGRATE_STRATEGY = ["abort", "overwrite", "merge"]
def convert_fish_cmd_to_zsh_cmd(cmd: str) -> str:
"""
Convert given fish cmd to zsh cmd
:param cmd: Fish cmd
:return: Zsh cmd
"""
return cmd.replace('; and ', '&&').replace('; or ', '||')
def convert_fish_history_to_zsh_history(fish_history: str) -> (str, int):
"""
Convert given fish history content to zsh history format
:param fish_history: Fish history
:return: Zsh history
"""
iter_fish_history_entries = regexp_fish_history_entries.finditer(fish_history)
entries_num = 0
zsh_history = ""
for entry in iter_fish_history_entries:
entries_num += 1
zsh_history += ZSH_HISTORY_ENTRY.format(entry.group("when"), convert_fish_cmd_to_zsh_cmd(entry.group("cmd")))
return zsh_history, entries_num
def _write_and_overwrite(zsh_dst: str, zsh_history: str):
"""
@private
Write zsh_history to zsh_dst, and overwrite if exists
:param zsh_dst:
:param zsh_history:
:return:
"""
try:
with open(zsh_dst, "w") as zs:
zs.write(zsh_history)
except Exception as err:
print(f"Fatal error while writing data: {err}")
exit(1)
def _write_or_abort(zsh_dst: str, zsh_history: str):
"""
@private
Write zsh_history to zsh_dst, or abort if exists
:param zsh_dst:
:param zsh_history:
:return:
"""
if os.path.exists(zsh_dst):
print(f"'{zsh_dst}' already exists. Aborting.")
return
_write_and_overwrite(zsh_dst, zsh_history)
def _write_and_merge(zsh_dst: str, zsh_history: str):
"""
@private
Write zsh_history to zsh_dst, and merge content if exists
:param zsh_dst:
:param zsh_history:
:return:
"""
current_content = ""
if os.path.exists(zsh_dst):
try:
with open(zsh_dst, "r") as zs:
current_content = zs.read()
except Exception:
print(f"Could not read '{zsh_dst}' to merge")
exit(1)
merged_content = current_content.split("\n") + zsh_history.split("\n")
merged_content = list(set(merged_content))
merged_content.sort()
if merged_content[0] == "":
merged_content.pop(0)
merged_content.append("")
merged_zsh_history = "\n".join(merged_content)
_write_and_overwrite(zsh_dst, merged_zsh_history)
def migrate(fish_src: str, zsh_dst: str, strategy: str):
"""
Migrate fish_history in fish_src, to zsh_history in zsh_dst, with given migrate strategy
:param fish_src: fish_history src path
:param zsh_dst: zsh_history dst path
:param strategy: Migrate strategy
:return:
"""
if strategy not in MIGRATE_STRATEGY:
print(f"Unsupported merge strategy '{strategy}'")
exit(1)
print(f"Reading '{fish_src}' ...")
try:
with open(fish_src, "r") as fs:
fish_history = fs.read()
except Exception:
print(f"Could not read '{fish_src}'")
exit(1)
print("Looking for fish_history entries ...")
zsh_history, entries_num = convert_fish_history_to_zsh_history(fish_history)
if entries_num == 0:
print("No entries found. zsh_history not created")
exit(0)
print(f"{entries_num} entries found. Saving to '{zsh_dst}'")
if strategy == MIGRATE_STRATEGY[0]:
_write_or_abort(zsh_dst, zsh_history)
elif strategy == MIGRATE_STRATEGY[1]:
_write_and_overwrite(zsh_dst, zsh_history)
elif strategy == MIGRATE_STRATEGY[2]:
_write_and_merge(zsh_dst, zsh_history)
else:
print(f"Unsupported migrate strategy '{strategy}'")
exit(1)
print("Finished")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(prog="fish_history to zsh_history")
parser.add_argument('-f', '--fish-src', default="~/.local/share/fish/fish_history",
help='fish_history location | Default: ~/.local/share/fish/fish_history')
parser.add_argument('-z', '--zsh-dst', default="~/.zsh_history",
help='zsh_history location | Default: ~/.zsh_history')
parser.add_argument('-s', '--strategy', choices=MIGRATE_STRATEGY, default=MIGRATE_STRATEGY[0],
help='Migrate strategy. What to do when zsh_dst is existing and is not empty?'
' | abort - Abort migration (Default)'
' | overwrite - Overwrite file'
' | merge - Merge existing and the new content')
args = parser.parse_args()
fish_src = os.path.expanduser(args.fish_src)
zsh_dst = os.path.expanduser(args.zsh_dst)
migrate(fish_src, zsh_dst, args.strategy)