Created
August 8, 2019 10:02
-
-
Save Nia-TN1012/1ab8e601a47d4aaa8221d742a1d910b2 to your computer and use it in GitHub Desktop.
iTerm2のステータスバーにSubversionのリポジトリ情報を表示するコンポーネント
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.7 | |
# iTerm2のステータスバーにSubversionのリポジトリ情報を表示するコンポーネント | |
# The component that displays a subversion repository infomation in the iTerm2 status bar | |
from iterm2 import Connection, Reference, StatusBarComponent, StatusBarRPC, RPC, util, run_forever, async_get_app | |
from iterm2.statusbar import Knob | |
from subprocess import CalledProcessError, check_output | |
from xml.etree import ElementTree | |
async def main( connection ): | |
app = await async_get_app( connection ) | |
component = StatusBarComponent( | |
short_description = "svn info", | |
detailed_description = "Show Subversion repository information", | |
knobs = [], | |
exemplar = "svn: {repo}:{revision}", | |
update_cadence = 5, | |
identifier = "net.chronoir.iterm2.svninfo" | |
) | |
# Subversionのリポジトリ情報をポップアップで表示します。 | |
# Displays Subversion repository information in a popup. | |
@RPC | |
async def svn_info_popup( session_id ): | |
# セッションIDからセッションを取得し、セッション変数「path」から、そのセッションのカレントディレクトリを取得します。 | |
# Gets the session from the session ID, and get the current directory of the session from the session variable “path”. | |
session = app.get_session_by_id( session_id ) | |
cur_path = await session.async_get_variable( "path" ) | |
try: | |
result = check_output( args = ["/usr/bin/svn", "info", cur_path] ).decode( "utf-8" ).replace( "&", "&" ).replace( "<", "<" ) | |
except CalledProcessError: | |
# セッションのカレントディレクトリがSubversionのリポジトリでない時 | |
# When the current session directory is not a subversion repository | |
result = f"'{cur_path}' is not svn repository" | |
await component.async_open_popover( session_id, f"<pre>{result}</pre>", util.Size( 200, 200 ) ) | |
# SubversionのリポジトリのRelative URLとリビジョン番号を取得し、ステータス文字列を生成します。 | |
# Gets Relative URL and revision number of Subversion repository and generates a status string. | |
@StatusBarRPC | |
async def svn_info( knobs, cur_path = Reference( "path" ) ): | |
# iTerm2のPythonスクリプトを実行した時のカレントディレクトリはルート「/」です。 | |
# そのため、アクティブなセッションのカレントディレクトリを取得するには、Referenceの変数「path」から取得します。 | |
# The current directory when iTerm2's Python script is executed is the root "/". | |
# Therefore, to get the current directory of the active session, get it from the reference variable "path". | |
try: | |
result = check_output( args = ["/usr/bin/svn", "info", cur_path, "--xml"] ).decode( "utf-8" ) | |
except CalledProcessError: | |
# セッションのカレントディレクトリがSubversionのリポジトリでない時 | |
# When the current session directory is not a subversion repository | |
return "svn:" | |
tree = ElementTree.fromstring( result ) | |
repo_name = tree[0][1].text[2:] | |
revision = tree[0].attrib['revision'] | |
return f"svn: {repo_name}:{revision}" | |
await component.async_register( connection = connection, coro = svn_info, timeout = None, onclick = svn_info_popup ) | |
run_forever( main ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment