From 0f6016015dc8d21eaa4b9112230fdf29202573b2 Mon Sep 17 00:00:00 2001 From: "R. Steve McKown" Date: Sat, 24 Jan 2015 12:35:14 -0700 Subject: [PATCH] Write to log, clean up output Log file is in user's home directory, dispcfg.log --- dispcfg | 76 +++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/dispcfg b/dispcfg index 140ff22..02f1f8d 100755 --- a/dispcfg +++ b/dispcfg @@ -9,9 +9,36 @@ information, then reconfigure outputs via xrandr. import sys import subprocess import re +import datetime +import time dry_run = False +class record: + def open(self, filename): + self.of = open(filename, 'a') + + def out(self, line): + data = '%s\n' % line + self.of.write(data) + sys.stdout.write(data) + + def outTime(self, line): + tstamp = datetime.datetime.fromtimestamp(time.time()) + data = '%s %s\n' % (tstamp.strftime('%Y-%m-%d %H:%M:%S'), line) + self.of.write(data) + sys.stdout.write(data) + + def foutTime(self, line): + tstamp = datetime.datetime.fromtimestamp(time.time()) + data = '%s %s\n' % (tstamp.strftime('%Y-%m-%d %H:%M:%S'), line) + self.of.write(data) + + def close(self): + self.of.close() + +out = record() + class OutputInfo(): def __init__(self): self.clear() @@ -85,19 +112,19 @@ def xrandr_on(output): if output['posx'] == 0: command += ['--primary'] if not dry_run: - print 'xrandr_on:', command + out.out(' '.join(command)) subprocess.call(command) else: - print '(dry-run) xrandr_on:', command + out.out('(dry-run) %s' % ' '.join(command)) def xrandr_off(output): command = ['xrandr', '--output', output['name'], '--off'] if not dry_run: subprocess.call(command) - print 'xrandr_off:', command + out.out(' '.join(command)) else: - print '(dry-run) xrandr_off:', command + out.out('(dry-run) %s' % ' '.join(command)) def output_by_name(outputs, name): @@ -107,13 +134,6 @@ def output_by_name(outputs, name): return None -def print_outputs(header, outputs): - print '%s:' % header - for output in outputs: - print_output(output) - print - - def all_done(outputs): for output in outputs: if not output['done']: @@ -142,19 +162,35 @@ def enable_an_entry(outputs, new_enable): if __name__ == '__main__': - def print_output(output): - print 'name:%s connected:%r enabled:%r width:%d height:%d pos:%dx%d' % ( - output['name'], output['connected'], output['enabled'], - output['width'], output['height'], output['posx'], - output['posy']) def main(argv): global dry_run if len(argv) >= 2 and argv[1] == '--dry-run': dry_run = True + global out + from os.path import expanduser, join + path = join(expanduser("~"), 'dispcfg.log') + out.open(path) + out.outTime('started') + outputs = parse() - print_outputs('Outputs', outputs) + + for output in outputs: + s = '%s' % output['name'] + if output['connected']: + s += ' connected' + else: + s += ' disconnected' + if output['enabled']: + s += ' enabled' + t = '%dx%d' % (output['width'], output['height']) + if t != '-1x-1': + s += ' size:%s' % t + t = '%dx%d' % (output['posx'], output['posy']) + if t != '-1x-1': + s += ' pos:%s' % t + out.out(s) # Find the first two connected devices, in reverse list order. On the # Thinkpad X201, the reversed list order is the preferred order of @@ -162,15 +198,17 @@ if __name__ == '__main__': # LVDS panel used as a last resort. Also find new posx values. use_count = 0 posx = 0 + use = [] for output in reversed(outputs): if output['connected']: - print 'use', output['name'] + use.append(output['name']) output['use'] = True output['posx'] = posx posx += output['width'] use_count += 1 if use_count >= 2: break + out.out('use %s' % ' '.join(use)) # How many outputs are currently enabled? enabled_count = 0 @@ -193,5 +231,7 @@ if __name__ == '__main__': if not dry_run: subprocess.call('/usr/local/bin/setctrl') + out.outTime('completed') + main(sys.argv) -- 2.39.2