33from sys import stderr , stdout
44from shutil import copyfileobj
55from subprocess import Popen , PIPE , run
6+ import sys
67from typing import Dict , Any
78from .utils import check_pid , filesizepu , kill_pid , tail_bytes , tail_file
89from .main import Main , flag , arg
@@ -131,6 +132,26 @@ def start(self) -> None:
131132 sp .drop (x )
132133
133134
135+ def _tail (n : float , u = "" , out = "" , tab = None ):
136+ if u :
137+ stdout .buffer .write (tail_bytes (out , int (n )))
138+ elif n > 0 :
139+ if sys .platform .startswith ("win" ):
140+ cmd = [
141+ "powershell" ,
142+ "-c" ,
143+ f"Get-Content -Tail { int (n )} '{ out } '" ,
144+ ]
145+ else :
146+ cmd = ["tail" , "-n" , str (int (n )), out ]
147+ if tab :
148+ with Popen (cmd , stdout = PIPE ).stdout as o :
149+ for line in o :
150+ stdout .buffer .write (b"\t " + line )
151+ else :
152+ run (cmd )
153+
154+
134155class Tail (Main ):
135156 """Tail process output."""
136157
@@ -152,37 +173,18 @@ def start(self) -> None:
152173 hf = None
153174 else :
154175 hf = format_prep (self .format or r"{pid?}: {name}" )
155- lines = filesizepu (self .lines or "10" )
176+ n , u = filesizepu (self .lines or "10" )
156177 j = 0
157178 out = "err" if self .err else "out"
158179
159180 for x in Manager ().find_names (self .ids , ambiguous , no_record ):
160181 if self .existing and not check_pid (x ["pid" ]):
161182 continue
162183
163- j > 1 and lines [ 0 ] > 0 and print ()
184+ j > 1 and n > 0 and print ()
164185 if hf :
165186 print (f"{ self .p_open } { hf (x )} { self .p_close } " , flush = True )
166-
167- if lines [1 ]:
168- stdout .buffer .write (tail_bytes (x [out ], int (lines [0 ])))
169- elif lines [0 ] > 0 :
170- # TODO: pythonify
171- if sys .platform .startswith ("win" ):
172- cmd = [
173- "powershell" ,
174- "-c" ,
175- f"Get-Content -Tail { int (lines [0 ])} '{ x [out ]} '" ,
176- ]
177- else :
178- cmd = ["tail" , "-n" , str (int (lines [0 ])), x [out ]]
179- if self .tab :
180- with Popen (cmd , stdout = PIPE ).stdout as o :
181- for line in o :
182- stdout .buffer .write (b"\t " + line )
183- else :
184- run (cmd )
185- stdout .flush ()
187+ _tail (n , u , x [out ], self .tab )
186188 j += 1
187189
188190
@@ -193,11 +195,11 @@ class Run(Main):
193195 tail : int = flag ("t" , "tail" , "tail the output with n lines" , default = 0 )
194196 run_id : str = flag ("id" , "Unique run identifier" , default = "" )
195197 cwd : str = flag ("Working directory for the command" )
196- tail : int = flag (
198+ tail : str = flag (
197199 "t" ,
198200 "tail" ,
199201 "Tail the output (n lines). Use `-t -1` to print the entire output" ,
200- default = 0 ,
202+ # default=0,
201203 )
202204 overwrite : bool = flag ("overwrite" , "Overwrite existing entry" , default = False )
203205 cmd_after : str = flag ("run-after" , "Run command after" , metavar = "command" )
@@ -228,13 +230,8 @@ def start(self) -> None:
228230
229231 # Handle tail output
230232 if self .tail :
231- if self .tail < 0 :
232- with open (e ["out" ], "rb" ) as f :
233- copyfileobj (f , stdout .buffer )
234- elif self .tail > 0 :
235- for x in tail_file (e ["out" ], self .tail ):
236- stdout .buffer .write (x )
237- stdout .buffer .write (b"\n " )
233+ n , u = filesizepu (self .tail )
234+ _tail (n , u , e ["out" ])
238235
239236 # Run post-command if specified
240237 if self .cmd_after :
0 commit comments