Replies: 2 comments
Ubuntu 向け
|
0 replies
rich でコマンド出力表示#!/usr/bin/env python3
import subprocess
from rich.console import Console
from rich.panel import Panel
from rich.live import Live
from rich.text import Text
def main():
console = Console()
# 初期表示
console.print("sudo apt-get update を実行中...")
try:
# プロセス開始
process = subprocess.Popen(
['sudo', 'apt-get', 'update'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
bufsize=1
)
output_text = Text()
# Live更新でリアルタイム表示
with Live(Panel(output_text, title="APT Update - 実行中"),
console=console, refresh_per_second=10) as live:
while True:
line = process.stdout.readline()
if not line and process.poll() is not None:
break
if line:
output_text.append(line)
# パネルを更新
live.update(Panel(output_text, title="APT Update - 実行中"))
# 最終結果
return_code = process.wait()
if return_code == 0:
final_panel = "APT Update - 完了 ✅"
else:
final_panel = "APT Update - エラー (終了コード: {return_code}) ❌"
console.print(final_panel)
except KeyboardInterrupt:
console.print(Panel("中断されました", title="APT Update - 中断"))
except Exception as e:
console.print(Panel(f"エラー: {e}", title="APT Update - エラー"))
if __name__ == "__main__":
main() |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Today I Learned ...
All reactions