aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrahiel <rahielkasim@gmail.com>2014-11-18 20:50:32 +0100
committerrahiel <rahielkasim@gmail.com>2014-11-18 20:50:32 +0100
commit855f6d46c6dc31203e6caaad970d02164643d807 (patch)
treef4ae1423f33ee2dfd4991c9370b37cc0c243e24b
parentb2ef81f2c1e8287c8fdddbe1a0adc5833ec4db5d (diff)
properly get command for fish shell, fix bug if history is very small
-rw-r--r--shellstats.py30
1 files changed, 17 insertions, 13 deletions
diff --git a/shellstats.py b/shellstats.py
index 9569b21..c010f8f 100644
--- a/shellstats.py
+++ b/shellstats.py
@@ -6,11 +6,9 @@ from os.path import isfile
def main():
- history_file = get_history()
- if not history_file:
+ history = get_history()
+ if not history:
print("Shell history not found.")
- with open(history_file, 'r') as h:
- history = [l.strip() for l in h.readlines() if l.strip()]
commands = {}
for line in history:
cmd = line.split()
@@ -23,7 +21,8 @@ def main():
total = len(history)
frequency = sorted(commands.keys(), key=lambda x: commands[x], reverse=True)
print("{:>3} {:<20} {:<10} {:<3}".format('', "Command", "Count", "Percentage"))
- for i in range(10):
+ # min for when history is very small
+ for i in min(range(10), range(len(frequency)), key=len):
cmd = frequency[i]
count = commands[cmd]
print("{i:>3} {cmd:<20} {count:<10} {percent:<3.3}%"
@@ -31,16 +30,21 @@ def main():
return frequency
-def get_history():
- """Get the history file for the shell in use."""
+def get_history(history_file=None):
+ """Get usage history for the shell in use."""
shell = getenv("SHELL").split('/')[-1]
home = getenv("HOME") + '/'
- hist = {"bash": [".bash_history"], "fish": [".config/fish/fish_history"],
- "zsh": [".zhistory", ".zsh_history"]}
- if shell in hist:
- for history in hist[shell]:
- if isfile(home + history):
- return home + history
+ hist_files = {"bash": [".bash_history"], "fish": [".config/fish/fish_history"],
+ "zsh": [".zhistory", ".zsh_history"]}
+ if shell in hist_files:
+ for hist_file in hist_files[shell]:
+ if isfile(home + hist_file):
+ history_file = home + hist_file
+ with open(history_file, 'r') as h:
+ history = [l.strip() for l in h.readlines() if l.strip()]
+ if shell == "fish":
+ history = [l[7:] for l in history if l.startswith("- cmd:")]
+ return history
if __name__ == "__main__":