Commit e9d40b7c authored by Gaurav Kukreja's avatar Gaurav Kukreja

Added new ways to use the plugin and demo gifs

parent 4c657d0c
...@@ -6,36 +6,88 @@ import re ...@@ -6,36 +6,88 @@ import re
extension_language_dict = {".py" : "python",".cpp" : "cpp",".c" : "c",".sh" : "bash"} extension_language_dict = {".py" : "python",".cpp" : "cpp",".c" : "c",".sh" : "bash"}
class CheatSheetCommand(sublime_plugin.TextCommand): syntaxFile_language_dict = {"Packages/Python/Python.sublime-syntax": "python"}
def print_save_file_error(self, edit):
for region in self.view.sel():
if not region.empty():
s = self.view.substr(region)
s = "!!! \n To use cheat.sh plugin, the file needs to be saved with an appopriate \n extension to indicate the programming language being used. (eg. .py, cpp etc) \n!!!"
self.view.replace(edit, region, s)
def print_unsupported_programming_language(self, edit, fileext):
for region in self.view.sel():
if not region.empty():
s = self.view.substr(region)
s = "!!! \n It seems that the programming language, as indicated by the extension \"" + fileext + "\" \n is not supported by cheat.sh plugin currently! Please contact the developer to include \n support for this programming langauge. \n!!!"
self.view.replace(edit, region, s)
def run(self, edit): def getAnswer(language, query, recommendationNum = 0, withComments = False):
recommendationStr = ""
if (recommendationNum > 0):
recommendationStr = '/' + str(recommendationNum)
commentsStr = ""
if withComments == False:
commentsStr = "?Q"
requestStr = 'http://cht.sh/' + language + '/' + query + recommendationStr + commentsStr + '?T'
return requests.get(requestStr).text
class CheatSheetUtils:
def error_programming_language_unknown(self):
sublime.message_dialog("Cheat.sh plugin could not deduce the programming language.\n\nPlease save the file with an appropriate extension or set the syntax from the Command Palette (Cmd + Shift + p) and typing \"Set Syntax:\"")
def error_unsupported_programming_language(self):
sublime.message_dialog("Cheat.sh plugin does not seem to support this programming language.\n\nPlease contact the developer at gaurav@gauravk.in to include support for this programming language.")
def getProgrammingLanguage(self):
fname = self.view.file_name() fname = self.view.file_name()
if fname == None: if fname is not None:
self.print_save_file_error(edit) filename, fileext = os.path.splitext(self.view.file_name());
return if fileext in extension_language_dict:
filename, fileext = os.path.splitext(self.view.file_name()); return extension_language_dict[fileext]
if fileext in extension_language_dict: else:
language = extension_language_dict[fileext] self.error_unsupported_programming_language()
return
else: else:
self.print_unsupported_programming_language(edit, fileext) syntaxFile = self.view.settings().get('syntax')
return if syntaxFile in syntaxFile_language_dict:
for region in self.view.sel(): return syntaxFile_language_dict[syntaxFile]
if not region.empty(): else:
s = self.view.substr(region) if syntaxFile == 'Packages/Text/Plain text.tmLanguage':
s = re.sub(r'\s+(?!\n)', r'+', s) self.error_programming_language_unknown()
r = requests.get('http://cht.sh/' + language + '/' + s + '?T') else:
s = r.text self.error_unsupported_programming_language()
self.view.replace(edit, region, s) return None
\ No newline at end of file
class CheatSheetCommand(sublime_plugin.TextCommand, CheatSheetUtils):
def run(self, edit):
language = self.getProgrammingLanguage()
if language is not None:
for region in self.view.sel():
if not region.empty():
s = self.view.substr(region)
s = re.sub(r'\s+(?!\n)', r'+', s)
self.view.replace(edit, region, getAnswer(language, s))
class CheatSheetMultipleSuggestionsCommand(sublime_plugin.TextCommand, CheatSheetUtils):
def on_done(self, user_input):
language = self.getProgrammingLanguage()
if language is not None:
newView = self.view.window().new_file()
newView.settings().set('auto_indent', False)
newView.settings().set('word_wrap', False)
# newView.set_syntax_file(self.view.settings().get('syntax'))
sublime.active_window().focus_view(newView)
separator = '\n\n' + "-"*80 + '\n' + "-"*80 + '\n' + "-"*80 + '\n\n'
if sublime.active_window().active_view() == newView:
newView.run_command('insert', {"characters": getAnswer(language, user_input)})
newView.run_command('insert', {"characters": separator})
newView.run_command('insert', {"characters": getAnswer(language, user_input, 1)})
newView.run_command('insert', {"characters": separator})
newView.run_command('insert', {"characters": getAnswer(language, user_input, 2)})
def run(self, edit):
self.view.window().show_input_panel("Cheat.sh", "", self.on_done, None, None)
class CheatSheetInputPanelCommand(sublime_plugin.TextCommand, CheatSheetUtils):
def on_done(self, user_input):
language = self.getProgrammingLanguage()
if language is not None:
old_auto_indent_status = self.view.settings().get('auto_indent')
if (old_auto_indent_status == True):
self.view.settings().set('auto_indent', False)
self.view.run_command('insert', {"characters": getAnswer(language, user_input)})
if (old_auto_indent_status == True):
self.view.settings().set('auto_indent', True)
def run(self, edit):
self.view.window().show_input_panel("Cheat.sh", "", self.on_done, None, None)
...@@ -2,6 +2,13 @@ ...@@ -2,6 +2,13 @@
{ {
"keys": ["ctrl+shift+b"], "keys": ["ctrl+shift+b"],
"command": "cheat_sheet" "command": "cheat_sheet"
},
{
"keys": ["ctrl+shift+n"],
"command": "cheat_sheet_multiple_suggestions"
},
{
"keys": ["ctrl+shift+m"],
"command": "cheat_sheet_input_panel"
} }
] ]
\ No newline at end of file
[ [
{ {
"keys": ["super+b"], "keys": ["super+shift+b"],
"command": "cheat_sheet" "command": "cheat_sheet"
},
{
"keys": ["super+shift+n"],
"command": "cheat_sheet_multiple_suggestions"
},
{
"keys": ["super+shift+m"],
"command": "cheat_sheet_input_panel"
} }
] ]
\ No newline at end of file
[ [
{ {
"keys": ["ctrl+shift+b"], "keys": ["ctrl+shift+b"],
"command": "cheat_sheet" "command": "cheat_sheet"
},
{
"keys": ["ctrl+shift+n"],
"command": "cheat_sheet_multiple_suggestions"
},
{
"keys": ["ctrl+shift+m"],
"command": "cheat_sheet_input_panel"
} }
] ]
\ No newline at end of file
...@@ -14,17 +14,28 @@ Command Palette from "Tools -> Command Palette" and write "Package Control: Sati ...@@ -14,17 +14,28 @@ Command Palette from "Tools -> Command Palette" and write "Package Control: Sati
Meanwhile, we are working on publishing this plugin using Package Control, which will make it easy to install and update. Stay tuned for updates on this. Meanwhile, we are working on publishing this plugin using Package Control, which will make it easy to install and update. Stay tuned for updates on this.
## How to Use ## Demos
1. Open your source file. The extension of the source file should indicate the programming language (eg. .py for python). ### Insert answer in editor
2. Write your query, eg. "append file". Select and press the key sequence to send request to "cht.sh". On Linux the key combination
is "Ctrl + Shift + b". On MacOS, the key combination is "Cmd + b".
3. The query will then be replaced by the code generated from "cht.sh".
### Demo 1. Write your query string.
2. Select the query string.
3. Press "Cmd + Shift + B" to replace the selected query string by the answer generated from cht.sh.
<p align="center"> ![Preview](/contrib/cheat_demo_1.gif)
<a href="http://www.youtube.com/watch?feature=player_embedded&v=cseFoBnu1_Q
" target="_blank"><img src="http://img.youtube.com/vi/cseFoBnu1_Q/0.jpg" ### List top 3 answers in a new tab
alt="cheat.sh-sublime-plugin A Sublime Text 3 plugin for cht.sh" width="700" height="490" border="10" /></a>
</p> 1. Press "Cmd + Shift + N" to launch the input panel in Sublime Text.
2. Enter your query and press Enter.
3. A new tab will be opened, and the top 3 best answers for the query will be listed in this page.
![Preview](/contrib/cheat_demo_2.gif)
### Another way to insert answer directly in the editor
1. Press "Cmd + Shift + M" to launch the input panel in Sublime Text.
2. Enter your query and press Enter.
3. The answer will be pasted in the open editor.
![Preview](/contrib/cheat_demo_3.gif)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment