Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature show random note #80

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion sublime_zk.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from collections import defaultdict, deque
import threading
import io
import random
from subprocess import Popen, PIPE
import struct
import imghdr
Expand Down Expand Up @@ -73,7 +74,7 @@ def do_layout(window):
window.run_command('set_layout', {
'cols': [0.0, 0.7, 1.0],
'rows': [0.0, 0.8, 1.0],
'cells': [[0, 0, 1, 2], [1, 0, 2, 1], [1,1,2,2]]
'cells': [[0, 0, 1, 2], [1, 0, 2, 1], [1, 1, 2, 2]]
})

@staticmethod
Expand Down Expand Up @@ -2434,3 +2435,37 @@ def on_window_command(self, window, command_name, args):
view.run_command('zk_hide_images')
view.run_command('zk_show_images')


class ZkShowRandomNoteCommand(sublime_plugin.WindowCommand):
"""
Command that creates a new view containing a sorted list of all notes
"""
def run(self):
global F_EXT_SEARCH
# sanity check: do we have a project
if self.window.project_file_name():
# yes we have a project!
folder = os.path.dirname(self.window.project_file_name())
# sanity check: do we have an open folder
elif self.window.folders():
# yes we have an open folder!
folder = os.path.abspath(self.window.folders()[0])
else:
# don't know where to grep
return
settings = get_settings()
extension = settings.get('wiki_extension')
note_files = get_all_notes_for(folder, extension)
note_id_matcher = re.compile('[0-9.]{12,18}')
note_files = [f for f in note_files if
note_id_matcher.match(os.path.basename(f))]
note_files = [
random.choice(note_files), ]
assert len(note_files) == 1
note_files_str = '\n'.join(note_files)
ExternalSearch.externalize_note_links(note_files_str, folder, extension,
prefix='# Random Note:')
lines = open(ExternalSearch.external_file(folder), mode='r',
encoding='utf-8', errors='ignore').read().split('\n')
ExternalSearch.show_search_results(self.window, folder, 'Notes', lines,
'show_all_tags_in_new_pane')
1 change: 1 addition & 0 deletions sublime_zk.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
{ "caption": "ZK: Insert Link", "command": "zk_get_wiki_link" },
{ "caption": "ZK: Show all Notes", "command": "zk_show_all_notes" },
{ "caption": "ZK: Enter Zettelkasten Mode", "command": "zk_enter_zk_mode" },
{ "caption": "ZK: Show random Note", "command": "zk_show_random_note" },
]