Skip to content

Commit

Permalink
rename vars, override __str__ for User model
Browse files Browse the repository at this point in the history
  • Loading branch information
SeveNChaK committed Dec 22, 2020
1 parent 1da82c3 commit 45bedb6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 40 deletions.
59 changes: 27 additions & 32 deletions QuipEditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import os
from sublime import Region
from .src.providers import QuipProvider
from .src.managers import TREE_VIEW_TAB_ID
from .src.managers import TabsManager
from .src.managers.ChatView import ChatView
from .src.managers import TREE_VIEW_TAB_ID, TabsManager, ChatView
from .src.deps.markdownify import markdownify as md


Expand Down Expand Up @@ -110,10 +108,16 @@ def run(self, edit):
self.view.set_name("Contacts")

user, friends = quip.get_contacts()
tree = self._print_user(user)
if user is None:
tree = "Пофикси меня"
else:
tree = user.__str__()
tree += "<ul>"
for friend in friends:
tree += "<li>%s</li>" % self._print_user(friend)
if user is None:
tree += "Пофикси меня"
else:
tree += "<li>%s</li>" % friend.__str__()
tree += "</ul>"

phantom = sublime.Phantom(
Expand All @@ -124,15 +128,6 @@ def run(self, edit):
)
sublime.PhantomSet(self.view, KEY_CONTACTS_PHANTOM_SET).update([phantom, phantom])

def _print_user(self, user):
if user is None:
return "Пофикси меня"
return "<div>{0} <span><a href=\'{1}\' title=\'Test hint\'>{2}</a></span></div>".format(
user.avatar,
user.chat_thread_id,
user.name
)

def _open_chat(self, chat_thread_id):
self.view.window().run_command(
COMMAND_OPEN_CHAT,
Expand All @@ -159,13 +154,13 @@ def _open_chat(self, chat_thread_id):
})
self.toggled = True

self.chat_view = ChatView()
self.chat_view.view = sublime.active_window().new_file()
self.chat_view.chat_id = chat_thread_id
manager.set_chat(self.chat_view)
self.chat = ChatView()
self.chat.view = sublime.active_window().new_file()
self.chat.id = chat_thread_id
manager.set_chat(self.chat)

self.chat_view.view.set_name(self.chat_view.chat_name)
self.chat_view.view.run_command(
self.chat.view.set_name(self.chat.name)
self.chat.view.run_command(
COMMAND_INSERT_CHAT_MESSAGES,
{"messages": [str(m) for m in quip.get_messages(chat_thread_id)]}
)
Expand All @@ -177,8 +172,8 @@ def _close_chat(self):
"cells": [[0, 0, 1, 1]]
})
self.toggled = False
self.window.focus_view(self.chat_view.view)
if self.window.active_view() == self.chat_view.view:
self.window.focus_view(self.chat.view)
if self.window.active_view() == self.chat.view:
self.window.run_command('close')
manager.reset_chat()

Expand All @@ -191,20 +186,20 @@ def run(self, edit, messages):
result = ""
for m in messages:
result += self._convert_to_html(m)
manager.chat_view.view.set_scratch(False)
manager.chat_view.view.set_read_only(False)
manager.chat.view.set_scratch(False)
manager.chat.view.set_read_only(False)

phantom = sublime.Phantom(
region=Region(0, self.view.size()),
content=result,
layout=sublime.LAYOUT_BLOCK
)
manager.chat_view.add_phantom(phantom)
manager.chat_view.add_phantom(phantom)
sublime.PhantomSet(self.view, KEY_MESSAGES_PHANTOM_SET).update(manager.chat_view.phantoms)
manager.chat.add_phantom(phantom)
manager.chat.add_phantom(phantom)
sublime.PhantomSet(self.view, KEY_MESSAGES_PHANTOM_SET).update(manager.chat.phantoms)

manager.chat_view.view.set_read_only(True)
manager.chat_view.view.set_scratch(True)
manager.chat.view.set_read_only(True)
manager.chat.view.set_scratch(True)

def _convert_to_html(self, message):
return "<div>%s</div>" % message
Expand All @@ -214,12 +209,12 @@ class SendChatMessageCommand(sublime_plugin.TextCommand):

def run(self, edit):
current_window = sublime.active_window()
if manager.chat_view.view:
if manager.chat.view:
current_window.show_input_panel('Enter chat message:', '', self._send_message, None, None)

def _send_message(self, text: str):
message = quip.send_message(manager.chat_view.chat_id, text)
manager.chat_view.view.run_command(COMMAND_INSERT_CHAT_MESSAGES, {"messages": [str(message)]})
message = quip.send_message(manager.chat.id, text)
manager.chat.view.run_command(COMMAND_INSERT_CHAT_MESSAGES, {"messages": [str(message)]})


class UploadChangesOnSave(sublime_plugin.EventListener):
Expand Down
7 changes: 7 additions & 0 deletions src/entities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ def __init__(self, user_id, name, chat_thread_id = None):
self.id = user_id
self.name = name
self.chat_thread_id = chat_thread_id

def __str__(self):
return "<div>{0} <span><a href=\'{1}\' title=\'Test hint\'>{2}</a></span></div>".format(
self.avatar,
self.chat_thread_id,
self.name
)
4 changes: 2 additions & 2 deletions src/managers/ChatView.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class ChatView:
def __init__(self):
self.view = None
self.phantoms = []
self.chat_id = None
self.chat_name = "Private chat"
self.id = None
self.name = "Private chat"

def add_phantom(self, phantom):
self.phantoms.append(phantom)
12 changes: 6 additions & 6 deletions src/managers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class TabsManager:

def __init__(self):
self._tabs = dict()
self.chat_view = None
self.chat = None

def add(self, thread: int, view):
self._tabs[thread] = view
Expand All @@ -22,13 +22,13 @@ def get_tab(self, thread_id):
def contains(self, view):
return view in self._tabs.values()

def set_chat(self, chat_view):
self.chat_view = chat_view
def set_chat(self, chat):
self.chat = chat

def reset_chat(self):
if self.chat_view.chat_id:
self.remove_tab(thread=self.chat_view.chat_id)
self.chat_view = None
if self.chat.id:
self.remove_tab(thread=self.chat.id)
self.chat = None

def remove_tab(self, thread=None, view=None):
""" You must provide one parameter, though both is fine too """
Expand Down

0 comments on commit 45bedb6

Please sign in to comment.