Skip to content

Instantly share code, notes, and snippets.

@Nia-TN1012
Last active August 8, 2019 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nia-TN1012/4d0b1dd9553b8c9e5f5f51595a48d045 to your computer and use it in GitHub Desktop.
Save Nia-TN1012/4d0b1dd9553b8c9e5f5f51595a48d045 to your computer and use it in GitHub Desktop.
iTerm2のステータスバーにディスクの使用量を表示するコンポーネント
#!/usr/bin/env python3.7
# iTerm2のステータスバーにストレージの使用量を表示するコンポーネント
# The component that displays storage usage in the iTerm2 status bar
from iterm2 import Connection, StatusBarComponent, StatusBarRPC, run_forever
from iterm2.statusbar import Knob
import os
from decimal import Decimal, ROUND_HALF_UP
# SI接頭辞 SI prefix
si_list = ["", "K", "M", "G", "T", "P", "X", "Z", "Y"]
# 最適なSI接頭辞付きの値に計算します。
# Calculates to the value with the best SI prefix
def calc_si( val ):
dval = Decimal( val )
expr = int( dval.log10() )
si = int( dval.log10() / 3 )
mantissa = dval / ( 10 ** expr )
# e.g.: 12,340,000,000 -> 12.34 G
qval = float( mantissa.quantize( Decimal( '.001' ), rounding = ROUND_HALF_UP ) * ( 10 ** ( expr % 3 ) ) )
return ( qval, si_list[si] )
async def main( connection ):
# コンポーネントのメタデータ Metadata
componet = StatusBarComponent(
short_description = "Storage",
detailed_description = "Show storage usage",
knobs = [],
exemplar = "💾 50.0 GB / 1.0 TB",
update_cadence = 300, # 5分毎更新 5 min.
identifier = "net.chronoir.iterm2.storage"
)
# ストレージの使用量と総容量を計測し、ステータス文字列を生成します。
# Measures storage usage and total capacity, and generates a status string.
@StatusBarRPC
async def storage_status( knobs ):
try:
storage = os.statvfs( "/" )
except:
print( "ERROR: Failed to measure storage." )
return "❌[ERROR]"
storage_used = calc_si( storage.f_frsize * ( storage.f_blocks - storage.f_bfree ) )
storage_total = calc_si( storage.f_frsize * storage.f_blocks )
status_str = f"💾 {storage_used[0]} {storage_used[1]}B / {storage_total[0]} {storage_total[1]}B"
return status_str
# コンポーネントにコルーチンを登録します。 Registers the co-routine to the component.
await componet.async_register( connection = connection, coro = storage_status, timeout = None )
run_forever( main )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment