Skip to content

Commit d792fdb

Browse files
committed
Merge branch 'develop'
fix #89, #90, #85
2 parents 9c44791 + b174b0c commit d792fdb

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ LeetCode brings you offer, and now Emacs brings you LeetCode!
1616
| l | change prefer language |
1717
| s | filter problems by regex |
1818
| t | filter problems by tag |
19+
| T | toggle tag display |
1920
| d | filter problems by difficulty |
2021
| / | clear filters |
2122
| g | refresh without fetching from LeetCode |
@@ -71,6 +72,9 @@ manually: `pip3 install my_cookies`.
7172
You can set your preferred LeetCode programming language and SQL by setting
7273
`leetcode-prefer-language` and `leetcode-prefer-sql`:
7374

75+
If you prefer not to see problems' tags in the `*leetcode**`buffer by default.
76+
set `leetcode-prefer-tag-display` to nil
77+
7478
```elisp
7579
(setq leetcode-prefer-language "python3")
7680
(setq leetcode-prefer-sql "mysql")

leetcode.el

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
;; Keywords: extensions, tools
77
;; URL: https://github.com/kaiwk/leetcode.el
88
;; Package-Requires: ((emacs "26") (dash "2.16.0") (graphql "0.1.1") (spinner "1.7.3") (aio "1.0") (log4e "0.3.3"))
9-
;; Version: 0.1.21
9+
;; Version: 0.1.22
1010

1111
;; This program is free software; you can redistribute it and/or modify
1212
;; it under the terms of the GNU General Public License as published by
@@ -103,7 +103,7 @@ The object with following attributes:
103103
The object with following attributes:
104104
:num Number
105105
:tag String
106-
:problems Vector
106+
:problems List
107107
108108
The elements of :problems has attributes:
109109
:status String
@@ -300,19 +300,20 @@ USER-AND-PROBLEMS is an alist comes from
300300
:easy .ac_easy
301301
:medium .ac_medium
302302
:hard .ac_hard))
303-
(leetcode--debug "problem status pairs: %s" .stat_status_pairs)
303+
(leetcode--debug "set user: %s, solved %s in %s problems" .user_name .num_solved .num_total)
304304
;; problem list
305305
(setq leetcode--all-problems
306306
(list
307307
:num .num_total
308308
:tag "all"
309309
:problems
310310
(let* ((len .num_total)
311-
(problems (make-vector len nil)))
311+
(problems nil))
312312
(dotimes (i len)
313313
(let-alist (aref .stat_status_pairs i)
314-
(aset problems (1- .stat.frontend_question_id)
315-
(list
314+
(leetcode--debug "frontend_question_id: %s, question_id: %s, title: %s"
315+
.stat.frontend_question_id .stat.question_id .stat.question__title)
316+
(push (list
316317
:status .status
317318
:id .stat.frontend_question_id
318319
:backend-id .stat.question_id
@@ -323,15 +324,15 @@ USER-AND-PROBLEMS is an alist comes from
323324
(/ (float .stat.total_acs)
324325
.stat.total_submitted)))
325326
:difficulty .difficulty.level
326-
:paid-only (eq .paid_only t)))))
327+
:paid-only (eq .paid_only t))
328+
problems)))
327329
problems)))))
328330

329331
(defun leetcode--set-tags (all-tags)
330332
"Set `leetcode--all-tags' and `leetcode--all-problems' with ALL-TAGS."
331-
(leetcode--debug "all tags: %s" all-tags)
332333
(let-alist all-tags
333334
;; set problems tags
334-
(dovec (problem (plist-get leetcode--all-problems :problems))
335+
(dolist (problem (plist-get leetcode--all-problems :problems))
335336
(dolist (topic (to-list .topics))
336337
(let-alist topic
337338
(when (member (plist-get problem :backend-id) (to-list .questions))
@@ -341,7 +342,7 @@ USER-AND-PROBLEMS is an alist comes from
341342
;; set leetcode--all-tags
342343
(dolist (topic (to-list .topics))
343344
(let-alist topic
344-
(unless (member topic leetcode--all-tags)
345+
(unless (member .slug leetcode--all-tags)
345346
(push .slug leetcode--all-tags))))))
346347

347348
(defun leetcode--slugify-title (title)
@@ -458,7 +459,7 @@ Return a list of rows, each row is a vector:
458459
(medium-tag "medium")
459460
(hard-tag "hard")
460461
rows)
461-
(dovec (p problems)
462+
(dolist (p problems)
462463
(setq rows
463464
(cons
464465
(vector
@@ -473,8 +474,8 @@ Return a list of rows, each row is a vector:
473474
(number-to-string (plist-get p :id))
474475
;; title
475476
(concat
476-
(plist-get p :title)
477-
" "
477+
(plist-get p :title)
478+
" "
478479
(if (eq (plist-get p :paid-only) t)
479480
(prog1 leetcode--paid
480481
(put-text-property
@@ -486,7 +487,7 @@ Return a list of rows, each row is a vector:
486487
;; difficulty
487488
(leetcode--stringify-difficulty (plist-get p :difficulty))
488489
;; tags
489-
(string-join (plist-get p :tags) ", "))
490+
(if leetcode--display-tags (string-join (plist-get p :tags) ", ") ""))
490491
rows)))
491492
(reverse rows)))
492493

@@ -500,7 +501,6 @@ Return a list of rows, each row is a vector:
500501

501502
(defun leetcode--filter (rows)
502503
"Filter ROWS by `leetcode--filter-regex', `leetcode--filter-tag' and `leetcode--filter-difficulty'."
503-
(leetcode--debug "filter rows: %s" rows)
504504
(seq-filter
505505
(lambda (row)
506506
(and
@@ -553,6 +553,12 @@ Return a list of rows, each row is a vector:
553553
(completing-read "Difficulty: " leetcode--all-difficulties))
554554
(leetcode-refresh))
555555

556+
(defun leetcode-toggle-tag-display ()
557+
"Toggle `leetcode--display-tags` and refresh"
558+
(interactive)
559+
(setq leetcode--display-tags (not leetcode--display-tags))
560+
(leetcode-refresh))
561+
556562
(aio-defun leetcode--fetch-all-tags ()
557563
(let* ((url-request-method "GET")
558564
(url-request-extra-headers
@@ -587,7 +593,8 @@ Return a list of rows, each row is a vector:
587593
(defun leetcode-refresh ()
588594
"Make `tabulated-list-entries'."
589595
(interactive)
590-
(let* ((header-names '(" " "#" "Problem" "Acceptance" "Difficulty" "Tags"))
596+
(let* ((header-names (append '(" " "#" "Problem" "Acceptance" "Difficulty")
597+
(if leetcode--display-tags '("Tags"))))
591598
(rows (leetcode--filter (leetcode--problems-rows)))
592599
(headers (leetcode--make-tabulated-headers header-names rows)))
593600
(with-current-buffer (get-buffer-create leetcode--buffer-name)
@@ -611,6 +618,7 @@ Return a list of rows, each row is a vector:
611618
(leetcode--set-user-and-problems users-and-problems)
612619
(leetcode--set-tags all-tags))
613620
(leetcode--warn "LeetCode parse user and problems failed"))
621+
(setq leetcode--display-tags leetcode-prefer-tag-display)
614622
(leetcode-reset-filter)
615623
(leetcode-refresh))
616624

@@ -1033,6 +1041,13 @@ Call `leetcode-solve-problem' on the current problem id."
10331041
(get-buffer (leetcode--get-code-buffer-name title))))
10341042
leetcode--problem-titles))
10351043

1044+
(defcustom leetcode-prefer-tag-display t
1045+
"Whether to display tags by default in the *leetcode* buffer."
1046+
:type :boolean)
1047+
1048+
(defvar leetcode--display-tags leetcode-prefer-tag-display
1049+
"(Internal) Whether tags are displayed the *leetcode* buffer.")
1050+
10361051
(defvar leetcode-prefer-language "python3"
10371052
"LeetCode programming language.
10381053
c, cpp, csharp, golang, java, javascript, kotlin, php, python,
@@ -1104,11 +1119,9 @@ python3, ruby, rust, scala, swift, mysql, mssql, oraclesql.")
11041119

11051120
(defun leetcode--get-problem-by-id (id)
11061121
"Get problem from `leetcode--all-problems' by ID."
1107-
(let ((num (plist-get leetcode--all-problems :num))
1108-
(problems (plist-get leetcode--all-problems :problems)))
1109-
(when (or (< id 1) (> id num))
1110-
(user-error "Not found: No such problem with given id `%d'" id))
1111-
(aref problems (1- id))))
1122+
(seq-find (lambda (p)
1123+
(equal id (plist-get p :id)))
1124+
(plist-get leetcode--all-problems :problems)))
11121125

11131126
(defun leetcode--get-problem-id (slug-title)
11141127
"Get problem id by SLUG-TITLE."
@@ -1189,6 +1202,7 @@ major mode by `leetcode-prefer-language'and `auto-mode-alist'."
11891202
(define-key map "s" #'leetcode-set-filter-regex)
11901203
(define-key map "l" #'leetcode-set-prefer-language)
11911204
(define-key map "t" #'leetcode-set-filter-tag)
1205+
(define-key map "T" #'leetcode-toggle-tag-display)
11921206
(define-key map "d" #'leetcode-set-filter-difficulty)
11931207
(define-key map "g" #'leetcode-refresh)
11941208
(define-key map "G" #'leetcode-refresh-fetch)

0 commit comments

Comments
 (0)