Skip to content

Commit 4afab2c

Browse files
committed
Check GTM version constraint on load of plug-in
1 parent 74f61c7 commit 4afab2c

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ Use the command line to report on time logged for your commits.
3232
Here are some examples of insights GTM can provide you.
3333

3434
**Git commits with time spent**
35-
3635
```
36+
> gtm report -total-only -n 3
37+
3738
9361c18 Rename packages
3839
Sun Jun 19 09:56:40 2016 -0500 Michael Schenk 34m 30s
3940
@@ -47,6 +48,8 @@ Thu Jun 16 22:28:45 2016 -0500 Michael Schenk 1h 1m 0s
4748
**Git commits with detailed time spent by file**
4849

4950
```
51+
> gtm report
52+
5053
b2d16c8 Refactor discovering of paths when recording events
5154
Thu Jun 16 11:08:47 2016 -0500 Michael Schenk
5255
@@ -63,6 +66,8 @@ Thu Jun 16 11:08:47 2016 -0500 Michael Schenk
6366
**Timeline of time spent by day**
6467

6568
```
69+
> gtm report -format timeline -n 3
70+
6671
0123456789012345678901234
6772
Fri Jun 24 * 22m 0s
6873
Sat Jun 25 ** 1h 28m 0s

gtm-plugin.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
import os
66
import subprocess
7+
import re
78

89
gtm_settings = {}
910

@@ -39,47 +40,49 @@ def plugin_loaded():
3940
set_status_bar()
4041

4142
def set_status_bar():
42-
if GTM.status_option_found and gtm_settings.get('gtm_status_bar', True):
43+
if gtm_settings.get('gtm_status_bar', True):
4344
GTM.status_option = '--status'
4445
print("Enabling reporting time in status bar")
4546
else:
4647
GTM.status_option = ''
4748
print("Disabling reporting time in status bar")
4849

4950
class GTM(sublime_plugin.EventListener):
51+
gtm_ver_req = '>= 1.0-beta.6'
52+
5053
update_interval = 30
5154
last_update = 0
5255
last_path = None
53-
status_option = ""
56+
status_option = ''
5457

55-
no_gtm_err = ("GTM executable not found. "
58+
no_gtm_err = ("GTM executable not found.\n\n"
5659
"Install GTM and/or update your system path. "
57-
"Make sure to restart Sublime after install. \n\n"
58-
"See https://www.github.com/git-time-metric/gtm")
60+
"Make sure to restart Sublime after install.\n\n"
61+
"See https://github.com/git-time-metric/gtm/blob/master/README.md")
5962

60-
record_err = ("GTM error saving time. "
61-
"Install GTM and/or update your system path. "
63+
record_err = ("GTM error saving time.\n\n"
64+
"Install GTM and/or update the system path. "
6265
"Make sure to restart Sublime after install.\n\n"
63-
"See https://www.github.com/git-time-metric/gtm")
66+
"See https://github.com/git-time-metric/gtm/blob/master/README.md")
6467

65-
ver_warn = ("GTM executable does not support all required features. "
68+
ver_warn = ("GTM executable is out of date.\n\n"
69+
"The plug-in may not work properly. "
6670
"Please install the latest GTM version and restart Sublime.\n\n"
67-
"See https://www.github.com/git-time-metric/gtm")
71+
"See https://github.com/git-time-metric/gtm/blob/master/README.md")
6872

6973
gtm_path = find_gtm_path()
7074

7175
if not gtm_path:
7276
sublime.error_message(no_gtm_err)
7377
else:
74-
# check support for [gtm record --status] feature
75-
p = subprocess.Popen('"{0}" record --help'.format(gtm_path),
78+
p = subprocess.Popen('"{0}" verify "{1}"'.format(gtm_path, gtm_ver_req),
7679
shell=True,
7780
stdin=subprocess.PIPE,
7881
stdout=subprocess.PIPE,
7982
stderr=subprocess.STDOUT)
8083
output = p.stdout.read()
81-
status_option_found = '-status' in output.decode('utf-8')
82-
if not status_option_found:
84+
version_ok = 'true' in output.decode('utf-8')
85+
if not version_ok:
8386
sublime.error_message(ver_warn)
8487

8588
def on_post_save_async(self, view):
@@ -109,9 +112,17 @@ def record(self, view, path):
109112

110113
try:
111114
cmd_output = subprocess.check_output(cmd, shell=True)
112-
if GTM.status_option != "":
113-
view.set_status("gtm-statusbar", cmd_output.decode('utf-8'))
115+
if GTM.status_option:
116+
view.set_status(
117+
"gtm-statusbar",
118+
GTM.format_status(cmd_output))
114119
else:
115120
view.erase_status("gtm-statusbar")
116121
except subprocess.CalledProcessError as e:
117122
sublime.error_message(GTM.record_err)
123+
124+
def format_status(t):
125+
s = t.decode('utf-8').strip()
126+
if s:
127+
s = ' '.join(re.sub("\\s*\\d*s\\s*", "", s).split())
128+
return "[ {0} ]".format(s)

0 commit comments

Comments
 (0)