Browse Source

Merge pull request #3 from thcipriani/command-log-n

Make command log accept -[NUM] argument
Brennen Bearnes 2 years ago
parent
commit
7eea2541a3
1 changed files with 47 additions and 1 deletions
  1. 47
    1
      bin/commandlog-log

+ 47
- 1
bin/commandlog-log View File

@@ -1,3 +1,49 @@
1 1
 #!/usr/bin/env bash
2 2
 
3
-sqlite3 -line ~/cli.db 'SELECT * FROM commands ORDER BY datetime DESC LIMIT 3;'
3
+set -e
4
+
5
+usage() {
6
+    cat<<HELP
7
+$(basename "$0") [OPTIONS]
8
+
9
+OPTIONS:
10
+    -[NUM]       Where [NUM] is the number of commands to show
11
+
12
+EXAMPLE:
13
+
14
+    Show last 5 commands: $ $(basename "$0") -5
15
+HELP
16
+}
17
+
18
+show_log() {
19
+    local log_count cmd
20
+    log_count="$1"
21
+
22
+    cmd=$(printf \
23
+        'SELECT * FROM commands ORDER BY datetime DESC LIMIT %d;' \
24
+        "$log_count")
25
+
26
+    exec sqlite3 -line ~/cli.db "$cmd"
27
+}
28
+
29
+main() {
30
+    local log_count
31
+    log_count=3
32
+
33
+    while [ -n "$1" ]; do
34
+        case "$1" in
35
+            --help|-h)
36
+                usage
37
+                exit 0
38
+            ;;
39
+        -*)
40
+            log_count="${1:1}"
41
+            ;;
42
+        esac
43
+        shift
44
+    done
45
+
46
+    show_log "$log_count"
47
+}
48
+
49
+main "$@"

Loading…
Cancel
Save