Skip to content

Commit 45fe166

Browse files
committed
feat: press 'd' filter by difficulties
closes #49
1 parent 8b39296 commit 45fe166

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ In leetcode problems list buffer:
5353
|--------|----------------------------------------|
5454
| n | cursor move down |
5555
| p | cursor move up |
56-
| s | filter problem by regex |
57-
| t | filter problem by tag |
56+
| s | filter problems by regex |
57+
| t | filter problems by tag |
58+
| d | filter problems by difficulty |
5859
| / | clear filters |
5960
| g | refresh without fetching from LeetCode |
6061
| G | refresh all data |

leetcode.el

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ The elements of :problems has attributes:
122122

123123
(defvar leetcode-retry-threshold 20 "`leetcode-try' or `leetcode-submit' retry times.")
124124
(defvar leetcode--filter-regex nil "Filter rows by regex.")
125-
(defvar leetcode--filter-tag nil "Filter rows by leetcode tag.")
125+
(defvar leetcode--filter-tag nil "Filter rows by tag.")
126+
(defvar leetcode--filter-difficulty nil
127+
"Filter rows by difficulty, it can be \"easy\", \"medium\" and \"hard\".")
128+
(defconst leetcode--all-difficulties '("easy" "medium" "hard"))
126129

127130
(defconst leetcode--checkmark "" "Checkmark for accepted problem.")
128131
(defconst leetcode--buffer-name "*leetcode*")
@@ -446,8 +449,16 @@ Return a list of rows, each row is a vector:
446449
rows)))
447450
rows))
448451

452+
(defun leetcode--row-tags (row)
453+
"Get tags from ROW."
454+
(aref row 6))
455+
456+
(defun leetcode--row-difficulty (row)
457+
"Get difficulty from ROW."
458+
(aref row 5))
459+
449460
(defun leetcode--filter (rows)
450-
"Filter ROWS by `leetcode--filter-regex' and `leetcode--filter-tag'."
461+
"Filter ROWS by `leetcode--filter-regex', `leetcode--filter-tag' and `leetcode--filter-difficulty'."
451462
(leetcode--debug "filter rows: %s" rows)
452463
(seq-filter
453464
(lambda (row)
@@ -457,8 +468,12 @@ Return a list of rows, each row is a vector:
457468
(string-match-p leetcode--filter-regex title))
458469
t)
459470
(if leetcode--filter-tag
460-
(let* ((tags (split-string (aref row 6) ", ")))
471+
(let ((tags (split-string (leetcode--row-tags row) ", ")))
461472
(member leetcode--filter-tag tags))
473+
t)
474+
(if leetcode--filter-difficulty
475+
(let ((difficulty (leetcode--row-difficulty row)))
476+
(string= difficulty leetcode--filter-difficulty))
462477
t)))
463478
rows))
464479

@@ -467,21 +482,29 @@ Return a list of rows, each row is a vector:
467482
(interactive)
468483
(setq leetcode--filter-regex nil)
469484
(setq leetcode--filter-tag nil)
485+
(setq leetcode--filter-difficulty nil)
470486
(leetcode-refresh))
471487

472488
(defun leetcode-set-filter-regex (regex)
473-
"Set `leetcode--filter-regex' as REGEX."
489+
"Set `leetcode--filter-regex' as REGEX and refresh."
474490
(interactive "sSearch: ")
475491
(setq leetcode--filter-regex regex)
476492
(leetcode-refresh))
477493

478494
(defun leetcode-set-filter-tag ()
479-
"Set `leetcode--filter-tag'."
495+
"Set `leetcode--filter-tag' from `leetcode--all-tags' and refresh."
480496
(interactive)
481497
(setq leetcode--filter-tag
482498
(completing-read "Tags: " leetcode--all-tags))
483499
(leetcode-refresh))
484500

501+
(defun leetcode-set-filter-difficulty ()
502+
"Set `leetcode--filter-difficulty' from `leetcode--all-difficulties' and refresh."
503+
(interactive)
504+
(setq leetcode--filter-difficulty
505+
(completing-read "Difficulty: " leetcode--all-difficulties))
506+
(leetcode-refresh))
507+
485508
(aio-defun leetcode--fetch-all-tags ()
486509
(let* ((url-request-method "GET")
487510
(url-request-extra-headers
@@ -1024,6 +1047,7 @@ for current problem."
10241047
(define-key map "p" #'previous-line)
10251048
(define-key map "s" #'leetcode-set-filter-regex)
10261049
(define-key map "t" #'leetcode-set-filter-tag)
1050+
(define-key map "d" #'leetcode-set-filter-difficulty)
10271051
(define-key map "g" #'leetcode-refresh)
10281052
(define-key map "G" #'leetcode-refresh-fetch)
10291053
(define-key map "/" #'leetcode-reset-filter)

0 commit comments

Comments
 (0)