Fix new config usage

This commit is contained in:
Paul Nameless 2020-05-22 14:39:09 +08:00
parent ece42b5846
commit c05880faf5
5 changed files with 42 additions and 35 deletions

View file

@ -7,16 +7,16 @@ import platform
import runpy
_os_name = platform.system()
_darwin = 'Darwin'
_linux = 'Linux'
_darwin = "Darwin"
_linux = "Linux"
DEFAULT_CONFIG = os.path.expanduser("~/.config/tg/conf.py")
DEFAULT_FILES = os.path.expanduser("~/.cache/tg/")
LOG_LEVEL = "INFO"
API_ID = '559815'
API_HASH = 'fd121358f59d764c57c55871aa0807ca'
API_ID = "559815"
API_HASH = "fd121358f59d764c57c55871aa0807ca"
PHONE = None
ENC_KEY = None
@ -26,17 +26,24 @@ TDLIB_VERBOSITY = 0
MAX_DOWNLOAD_SIZE = "10MB"
# TODO: check if darwin
# TODO: check platform
NOTIFY_CMD = "/usr/local/bin/terminal-notifier -title '{title}' -subtitle '{subtitle}' -message '{msg}' -appIcon '{icon_path}'"
# TODO: check if darwin
# TODO: check platform
RECORD_CMD = "ffmpeg -f avfoundation -i ':0' -ar 22050 -b:a 32k '{file_path}'"
# TODO: use mailcap instead of editor
LONG_MSG_CMD = "vim -c 'startinsert' {file_path}"
EDITOR = os.environ.get("EDITOR", "vi")
# TODO: if os == darwin else open-xdb?
DEFAULT_OPEN = "open '{file_path}'" if _os_name == _linux else "open '{file_path}'"
if _os_name == _linux:
DEFAULT_OPEN = "xdg-open '{file_path}'"
else:
DEFAULT_OPEN = "open '{file_path}'"
if _os_name == _linux:
DEFAULT_COPY = "xclip -selection c"
else:
DEFAULT_COPY = "pbcopy"
if os.path.isfile(DEFAULT_CONFIG):

View file

@ -23,16 +23,11 @@ from tg.utils import (
from tg.views import View
log = logging.getLogger(__name__)
MSGS_LEFT_SCROLL_THRESHOLD = 10
# start scrolling to next page when number of the msgs left is less than value.
# note, that setting high values could lead to situations when long msgs will
# be removed from the display in order to achive scroll threshold. this could
# cause blan areas on the msg display screen
MSGS_LEFT_SCROLL_THRESHOLD = 2
key_bind_handler_type = Callable[[Any], Any]
@ -249,7 +244,7 @@ class Controller:
def send_voice(self):
file_path = f"/tmp/voice-{datetime.now()}.oga"
with suspend(self.view) as s:
s.call(config.record_cmd.format(file_path=file_path))
s.call(config.RECORD_CMD.format(file_path=file_path))
resp = self.view.status.get_input(
f"Do you want to send recording: {file_path}? [Y/n]"
)
@ -331,7 +326,7 @@ class Controller:
) as s:
f.write(msg.text_content)
f.flush()
s.call(f"{config.editor} {f.name}")
s.call(f"{config.EDITOR} {f.name}")
with open(f.name) as f:
if text := f.read().strip():
self.model.edit_message(text=text)
@ -341,7 +336,7 @@ class Controller:
with NamedTemporaryFile("r+", suffix=".txt") as f, suspend(
self.view
) as s:
s.call(config.long_msg_cmd.format(file_path=f.name))
s.call(config.LONG_MSG_CMD.format(file_path=f.name))
with open(f.name) as f:
if msg := f.read().strip():
self.model.send_message(text=msg)

View file

@ -14,23 +14,13 @@ from tg.views import ChatView, MsgView, StatusView, View
log = logging.getLogger(__name__)
def main(stdscr: window) -> None:
utils.setup_log(config.LOG_LEVEL)
tg = Tdlib(
api_id=config.API_ID,
api_hash=config.API_HASH,
phone=config.PHONE,
database_encryption_key=config.ENC_KEY,
files_directory=config.DEFAULT_FILES,
tdlib_verbosity=config.TDLIB_VERBOSITY,
library_path=config.TDLIB_PATH,
)
tg.login()
def run(tg: Tdlib, stdscr: window) -> None:
# handle ctrl+c, to avoid interrupting tg when subprocess is called
def interrupt_signal_handler(sig, frame):
# TODO: draw on status pane: to quite press <q>
log.info("Interrupt signal is handled and ignored on purpose")
log.info("Interrupt signal is handled and ignored on purpose.")
signal.signal(signal.SIGINT, interrupt_signal_handler)
model = Model(tg)
@ -39,6 +29,7 @@ def main(stdscr: window) -> None:
chat_view = ChatView(stdscr)
view = View(stdscr, chat_view, msg_view, status_view)
controller = Controller(model, view, tg)
# hanlde resize of terminal correctly
signal.signal(signal.SIGWINCH, controller.resize_handler)
@ -50,5 +41,22 @@ def main(stdscr: window) -> None:
t.join()
def main():
utils.setup_log(config.LOG_LEVEL)
tg = Tdlib(
api_id=config.API_ID,
api_hash=config.API_HASH,
phone=config.PHONE,
database_encryption_key=config.ENC_KEY,
files_directory=config.DEFAULT_FILES,
tdlib_verbosity=config.TDLIB_VERBOSITY,
library_path=config.TDLIB_PATH,
)
tg.login()
wrapper(partial(run, tg))
if __name__ == "__main__":
wrapper(main())
main()

View file

@ -57,7 +57,7 @@ def update_new_msg(controller: Controller, update: Dict[str, Any]):
)
if current_chat_id == msg.chat_id:
controller.refresh_msgs()
if msg.file_id and msg.size <= config.max_download_size:
if msg.file_id and msg.size <= config.MAX_DOWNLOAD_SIZE:
controller.download(msg.file_id, msg.chat_id, msg["id"])
controller._notify_for_message(msg.chat_id, msg)

View file

@ -120,10 +120,7 @@ def setup_log(level="DEBUG"):
def notify(
msg,
subtitle="",
title="tg",
cmd=config.NOTIFY_CMD,
msg, subtitle="", title="tg", cmd=config.NOTIFY_CMD,
):
if not cmd:
return