Skip to content

Instantly share code, notes, and snippets.

@marcbelmont
Last active May 24, 2023 03:38
Show Gist options
  • Save marcbelmont/e715f807a30d96cf74f66006d0e5e4a6 to your computer and use it in GitHub Desktop.
Save marcbelmont/e715f807a30d96cf74f66006d0e5e4a6 to your computer and use it in GitHub Desktop.
Manage notes stored in a text file using Rofi. Bonus: tiny calculator using Python's eval. For details, see first comment.
#! /usr/bin/python3
import os
import sys
from pathlib import Path
NOTES = Path('/home/marc/Documents/notes.txt')
def to_clipboard(text):
os.system('echo "%s"|xclip -f -r -sel c > /dev/null' % str(text).strip())
def calc_mode(user_input):
try:
result = eval(user_input)
print(user_input)
to_clipboard(result)
print(f"\0message\x1f🐍: {result} <i>(copied to clipboard)</i>\n")
return True
except Exception:
return False
def main():
if not NOTES.exists():
print(f'{NOTES} does not exist.')
return
user_input = sys.argv[1] + '\n' if len(sys.argv) > 1 else None
if user_input and calc_mode(user_input):
return
# Notes
with open(NOTES) as fp:
lines = fp.readlines()
if user_input:
try:
deleted = lines.pop(lines.index(user_input))
to_clipboard(deleted)
except Exception:
if '[empty]' not in user_input:
lines.append(user_input)
with open(NOTES, 'w') as fp:
fp.writelines(lines)
# Display list
print("\0message\x1f<i>(Type text or a Python expression)</i>\n")
if lines:
print('\n'.join(reversed(lines)))
else:
print('[empty]')
if __name__ == "__main__":
main()
@marcbelmont
Copy link
Author

marcbelmont commented May 13, 2021

Installation

  • Save this file to /path/to/rofi_notes.py
  • Open the file and set NOTES to a text file in your local file system.
  • Call rofi -show notes -modi notes:/path/to/rofi_notes.py

Features

  • Items that you remove from the list are copied to the clipboard (via xclip).
  • An input that's a valid Python expression will get evaluated by Python instead of getting stored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment