From af9345a403cdbd5a905b336c032ced5698863887 Mon Sep 17 00:00:00 2001 From: Siarhei Siniak Date: Wed, 20 Mar 2024 23:30:11 +0300 Subject: [PATCH] [~] Refactor --- dotfiles/.local/bin/commands | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/dotfiles/.local/bin/commands b/dotfiles/.local/bin/commands index 8a96971..a974406 100755 --- a/dotfiles/.local/bin/commands +++ b/dotfiles/.local/bin/commands @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import enum +import collections import datetime import functools import io @@ -83,8 +84,8 @@ def intercept_output( t1.register(current_subprocess.stdout, select.POLLIN) #print([current_subprocess, current_subprocess.poll()]) output = [] - buffer = [] - buffer_lines = [] + buffer = collections.deque() + buffer_lines = collections.deque() last_data = None @@ -107,25 +108,24 @@ def intercept_output( buffer.append(t3) if need_lines: - if len(buffer_lines) > 0: - yield dict( - aggegated=False, - line=buffer_lines[0], - ) - del buffer_lines[0] - if b'\n' in t3: t3_pos = t3.rfind(b'\n') buffer_lines.extend([ o + b'\n' for o in b''.join( - buffer[:-1] + [ + list(buffer)[:-1] + [ t3[:t3_pos] ], ).splitlines() ]) - del buffer[:-1] - buffer[0] = t3[t3_pos + 1:] + buffer.clear() + buffer.append(t3[t3_pos + 1:]) + while len(buffer_lines) > 0: + yield dict( + aggegated=False, + line=buffer_lines.popleft(), + ) + else: yield dict( data=t3, @@ -1466,6 +1466,7 @@ def pm_service(argv): cmd = None if 'powerd' in line: cmd = line + print(cmd) #cmd = subprocess.check_output(r''' # log stream | grep --line-buffered -i \