-
Notifications
You must be signed in to change notification settings - Fork 3
/
welcome-dashboard.el
710 lines (615 loc) · 29.2 KB
/
welcome-dashboard.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
;;; welcome-dashboard.el --- Simple welcome-dashboard screen -*- lexical-binding: t -*-
;; Welcome-dashboard screen
;; Author: Mikael Konradsson <mikael.konradsson@outlook.com>
;; Maintainer: Mikael Konradsson <mikael.konradsson@outlook.com>
;; Keywords: dashboard
;; Created: 2023
;; Version: 0.2
;; Package-Requires: ((emacs "27.1") (all-the-icons "5.0.0") (async "1.9.7") (nerd-icons "0.0.1"))
;; Homepage: https://github.com/konrad1977/welcome-dashboard
;;; Commentary:
;;; Minimalistic dashboard for Emacs.
;;; Code:
(require 'async)
(require 'json)
(require 'recentf)
(require 'url)
(require 'vc)
(require 'nerd-icons)
(require 'all-the-icons)
(require 'package)
(defvar welcome-dashboard-mode nil)
(defvar welcome-dashboard-recentfiles '()
"Recent list.")
(defvar welcome-dashboard-recent-projects '()
"List of recent projects.")
(defvar welcome-dashboard-todos '()
"Todos.")
(defvar welcome-dashboard-last-project-name nil)
(defvar welcome-dashboard-temperature nil)
(defvar welcome-dashboard-weatherdescription nil)
(defvar welcome-dashboard-weathericon nil)
(defvar welcome-dashboard-use-nerd-icons nil)
(defcustom welcome-dashboard-title "Quick access [C-number to open file]"
"Welcome-dashboard title."
:group 'welcome-dashboard
:type '(string))
(defcustom welcome-dashboard-use-fahrenheit nil
"Show weather temperature in Fahrenheit."
:group 'welcome-dashboard
:type '(boolean))
(defcustom welcome-dashboard-show-file-path nil
"Show file path in welcome-dashboard."
:group 'welcome-dashboard
:type '(boolean))
(defcustom welcome-dashboard-min-left-padding 10
"Minimum left padding when resizing window."
:group 'welcome-dashboard
:type '(natnum))
(defcustom welcome-dashboard-path-max-length 72
"Latitude for weather information."
:group 'welcome-dashboard
:type '(natnum))
(defcustom welcome-dashboard-max-number-of-todos 5
"Maximum number of todos to show."
:group 'welcome-dashboard
:type '(natnum))
(defcustom welcome-dashboard-max-number-of-projects 5
"Maximum number of projects to show."
:group 'welcome-dashboard
:type '(natnum))
(defcustom welcome-dashboard-latitude nil
"Latitude for weather information."
:group 'welcome-dashboard
:type '(float))
(defcustom welcome-dashboard-longitude nil
"Longitude for weather information in welcome-dashboard package."
:group 'welcome-dashboard
:type '(float))
(defcustom welcome-dashboard-image-file ""
"Image file in welcome-dashboard package."
:group 'welcome-dashboard
:type '(file))
(defcustom welcome-dashboard-image-width 200
"Image width for weather information."
:group 'welcome-dashboard
:type '(natnum))
(defcustom welcome-dashboard-image-height 200
"Image width for weather information."
:group 'welcome-dashboard
:type '(natnum))
(defgroup welcome-dashboard nil
"Welcome-dashboard group."
:group 'applications)
(defconst welcome-dashboard-buffer "*welcome*"
"Welcome-dashboard buffer name.")
(defvar welcome-dashboard--file-icon-cache (make-hash-table :test 'equal)
"Cache for file icons.")
(defvar welcome-dashboard--padding-cache nil
"Cache for padding.")
(defvar welcome-dashboard--last-window-width nil
"Last window width.")
(defface welcome-dashboard-title-face
'((t :inherit font-lock-constant-face :height 1.3 :weight bold))
"Title face."
:group 'welcome-dashboard)
(defface welcome-dashboard-subtitle-face
'((t :foreground "#9399b2"))
"Subtitle face."
:group 'welcome-dashboard)
(defface welcome-dashboard-separator-face
'((t :inherit 'font-lock-comment-face))
"Subtitle face."
:group 'welcome-dashboard)
(defface welcome-dashboard-info-face
'((t :inherit font-lock-property-name-face :height 0.9 :bold t :italic t))
"Face added to code-usage display."
:group 'welcome-dashboard)
(defface welcome-dashboard-text-info-face
'((t :inherit default :height 0.9 :bold nil))
"Face added to code-usage display."
:group 'welcome-dashboard)
(defface welcome-dashboard-path-face
'((t :inherit font-lock-builtin-face :height 0.9 :weight thin :bold nil :italic nil))
"Face for the file path."
:group 'welcome-dashboard)
(defface welcome-dashboard-filename-face
'((t :inherit default :weight semi-bold))
"Face for the file name."
:group 'welcome-dashboard)
(defface welcome-dashboard-time-face
'((t :foreground "#a6adc8" :height 0.9 :weight thin))
"Face for time."
:group 'welcome-dashboard)
(defface welcome-dashboard-weather-description-face
'((t :foreground "#f9e2af" :height 0.9 :weight thin :bold nil :italic nil))
"Face for weather description."
:group 'welcome-dashboard)
(defface welcome-dashboard-startup-time-face
'((t :foreground "#C2A4F8" :height 0.9 :weight thin :bold nil :italic nil))
"Face for startup time."
:group 'welcome-dashboard)
(defface welcome-dashboard-shortcut-face
'((t :inherit font-lock-constant-face :height 0.9 :bold t))
"Face for recent files shortcuts."
:group 'welcome-dashboard)
(defface welcome-dashboard-shortcut-todo-face
'((t :foreground "#abd47c" :height 0.9 :bold t))
"Face for todo shortcuts."
:group 'welcome-dashboard)
(defface welcome-dashboard-todo-type-face
'((t :foreground "#585b70" :height 0.9 :bold t))
"Face for todo shortcuts."
:group 'welcome-dashboard)
(defface welcome-dashboard-weather-icon-face
'((t :height 0.9))
"Face for weather icon."
:group 'welcome-dashboard)
(defface welcome-dashboard-weather-temperature-face
'((t :foreground "#f38ba8" :height 0.9 :weight thin :bold nil :italic nil))
"Face for temperature."
:group 'welcome-dashboard)
(defface welcome-dashboard-project-face
'((t :inherit font-lock-function-name-face :weight semi-bold))
"Face for project name."
:group 'welcome-dashboard)
(defvar welcome-dashboard-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'welcome-dashboard--open-recent-file)
(define-key map (kbd "<return>") 'welcome-dashboard--open-recent-file)
(define-key map (kbd "o") 'welcome-dashboard--open-recent-file)
(dolist (i (number-sequence 1 welcome-dashboard-max-number-of-projects))
(define-key map (kbd (concat "M-" (number-to-string i)))
`(lambda ()
(interactive)
(welcome-dashboard--open-project-at-index ,i))))
(dolist (i (number-sequence 1 9))
(define-key map (kbd (concat "C-" (number-to-string i)))
`(lambda ()
(interactive)
(welcome-dashboard--open-recent-file-at-index ,i)))
(define-key map (kbd (concat "C-x " (number-to-string i)))
`(lambda ()
(interactive)
(welcome-dashboard--open-todos-at-index ,i))))
map)
"Keymap for `welcome-dashboard-mode'.")
(define-derived-mode welcome-dashboard-mode fundamental-mode "Welcome-dashboard"
"Major mode for the welcome-dashboard screen."
:group 'welcome-dashboard
:syntax-table nil
:abbrev-table nil
(buffer-disable-undo)
(setq-local display-line-numbers nil)
(setq-local truncate-lines t)
(setq-local mode-line-format nil)
(setq-local global-hl-line-mode nil)
(setq-local buffer-read-only t)
(use-local-map welcome-dashboard-mode-map))
(defun welcome-dashboard--ensure-recentf ()
"Ensure recentf is initialized and loaded."
(unless recentf-mode
(recentf-mode 1))
(unless (bound-and-true-p recentf-list)
(setq recentf-list nil))
(when (and (boundp 'recentf-list)
(not recentf-list))
(recentf-load-list)))
(defun welcome-dashboard--get-recent-projects ()
"Get list of recent project roots from welcome-dashboard-recentfiles."
(welcome-dashboard--ensure-recentf)
(let ((projects '()))
(dolist (file welcome-dashboard-recentfiles)
(when-let* ((root (vc-find-root file ".git")))
(unless (member root projects)
(push root projects))))
(seq-take (nreverse projects) welcome-dashboard-max-number-of-projects)))
(defun welcome-dashboard--insert-recent-projects ()
"Insert the recent projects in the welcome-dashboard buffer."
(unless welcome-dashboard-recent-projects
(setq welcome-dashboard-recent-projects (welcome-dashboard--get-recent-projects)))
(when welcome-dashboard-recent-projects
(dolist (project welcome-dashboard-recent-projects)
(let* ((project-name (file-name-nondirectory (directory-file-name project)))
(index (cl-position project welcome-dashboard-recent-projects :test #'equal))
(shortcut (+ index 1))
(project-line (format "%s %s %s"
(if welcome-dashboard-use-nerd-icons
(nerd-icons-octicon "nf-oct-repo_forked")
(all-the-icons-octicon "nf-oct-repo_forked"))
(propertize project-name 'face 'welcome-dashboard-project-face)
(propertize (number-to-string shortcut)
'face 'welcome-dashboard-shortcut-face))))
(welcome-dashboard--insert-text
(propertize project-line 'project-path project))))))
(defun welcome-dashboard--open-project-at-index (index)
"Open the project at INDEX."
(interactive "nIndex: ")
(let ((projects welcome-dashboard-recent-projects))
(when (<= 1 index (length projects))
(let ((project (nth (1- index) projects)))
(find-file project)))))
(defun welcome-dashboard--weather-icon-from-code (code)
"Maps a weather (as CODE) to a corresponding string."
(if welcome-dashboard-use-nerd-icons
(progn
(nerd-icons-wicon
(pcase code
(`0 "nf-weather-day_sunny")
((or `1 `2 `3) "nf-weather-cloudy")
((or `45 `48) "nf-weather-fog")
((or `51 `53 `55) "nf-weather-sleet")
((or `56 `57) "nf-weather-snow")
((or `61 `63 `65) "nf-weather-day_rain_mix")
((or `66 `67) "nf-weather-rain-mix")
((or `71 `73 `75) "nf-weather-snow")
(`77 "nf-weather-snow")
((or `80 `81 `82) "nf-weather-rain")
((or `85 `86) "nf-weather-rain-mix")
((or `95 `96 `99) "nf-weather-thunderstorm")
(_ "Unknown"))))
(progn
(all-the-icons-icon-for-weather
(pcase code
(`0 "wi-day-sunny")
((or `1 `2 `3) "wi-day-cloudy")
((or `45 `48) "wi-day-fog")
((or `51 `53 `55) "sleet")
((or `56 `57) "wi-snow")
((or `61 `63 `65) "wi-day-rain")
((or `66 `67) "wi-day-rain-mix")
((or `71 `73 `75) "wi-snow")
(`77 "wi-snow")
((or `80 `81 `82) "wi-rain")
((or `85 `86) "wi-rain-mix")
((or `95 `96 `99) "wi-thunderstorm")
(_ "Unknown"))))))
(defun welcome-dashboard--weather-code-to-string (code)
"Maps a weather (as CODE) to a corresponding string."
(pcase code
(`0 "Clear sky")
((or `1 `2 `3) "Partly cloudy")
((or `45 `48) "Fog")
((or `51 `53 `55) "Drizzle")
((or `56 `57) "Freezing drizzle")
((or `61 `63 `65) "Rain")
((or `66 `67) "Freezing rain")
((or `71 `73 `75) "Snowfall")
(`77 "Snow grains")
((or `80 `81 `82) "Rain showers")
((or `85 `86) "Snow showers")
((or `95 `96 `99) "Thunderstorm")
(_ "Unknown")))
(defun welcome-dashboard--insert-centered (text)
"Insert TEXT at the center of the current line."
(let ((width (window-width)))
(insert (make-string (/ (- width (length text)) 2) ?\ ))
(insert text)))
(defun welcome-dashboard--open-recent-file ()
"Open the recent file on the current line."
(interactive)
(let* ((line-start (line-beginning-position))
(line-end (line-end-position))
(prop-pos (next-single-property-change line-start 'path nil line-end)))
(when prop-pos
(let ((file (get-text-property prop-pos 'path)))
(if (file-exists-p file)
(find-file file)
(error "File %s does not exist" file))))))
(defun welcome-dashboard--open-recent-file-at-index (index)
"Open the recent file at the given INDEX in the list."
(interactive "nIndex: ")
(let ((files welcome-dashboard-recentfiles))
(when (<= 1 index (length files))
(find-file (nth (1- index) files)))))
(defun welcome-dashboard--open-todos-at-index (index)
"Open the todo at INDEX."
(interactive "nIndex: ")
(let ((todos welcome-dashboard-todos))
(when (<= 1 index (length todos))
(let* ((todo (nth (1- index) todos))
(file (nth 0 todo))
(line (string-to-number (nth 1 todo)))
(column (string-to-number (nth 2 todo))))
(find-file file)
(goto-char (point-min))
(forward-line (1- line))
(forward-char (1- column))))))
(defun welcome-dashboard--truncate-path-in-middle (path n)
"Truncate the middle of PATH to length N by removing characters.
And adding an ellipsis."
(if (<= (length path) n)
path
(let* ((left (/ (- n 3) 2))
(right (- n left 3))
(head (substring path 0 (+ left 1)))
(tail (substring path (- (length path) right)))
(ellipsis "..."))
(concat head ellipsis tail))))
(defun welcome-dashboard--file-icon (file)
"Get the icon for (FILE)."
(or (gethash file welcome-dashboard--file-icon-cache)
(puthash file
(if welcome-dashboard-use-nerd-icons
(propertize (cond ((not (file-exists-p file)) (nerd-icons-mdicon "nf-md-alert_remove" :face '(:inherit nerd-icons-orange)))
((file-directory-p file) (nerd-icons-icon-for-dir file))
(t (nerd-icons-icon-for-file file))))
(propertize (all-the-icons-icon-for-file file :v-adjust -0.05) 'face '(:family "all-the-icons" :height 1.0)))
welcome-dashboard--file-icon-cache)))
(defun welcome-dashboard--insert-recent-files ()
"Insert the first x recent files with icons in the welcome-dashboard buffer."
(recentf-mode)
(setq welcome-dashboard-recentfiles (seq-take recentf-list 9))
(let* ((files welcome-dashboard-recentfiles)
(left-margin (welcome-dashboard--calculate-padding-left)))
(dolist (file files)
(let* ((index (cl-position file files :test #'equal))
(full-path (file-truename file))
(shortcut (format "%d" (+ index +1)))
(file-name (file-name-nondirectory file))
(file-dir (file-name-directory file))
(title (if welcome-dashboard-show-file-path
(format "%s %s%s"
(welcome-dashboard--file-icon file)
(propertize (welcome-dashboard--truncate-path-in-middle file-dir welcome-dashboard-path-max-length) 'face 'welcome-dashboard-path-face)
(propertize file-name 'face 'welcome-dashboard-filename-face))
(format "%s %s"
(welcome-dashboard--file-icon file)
(propertize file-name 'face 'welcome-dashboard-filename-face))))
(title-with-path (propertize title 'path full-path))
(title-with-path-and-shortcut (concat title-with-path (propertize (format " [%s]" shortcut) 'face 'welcome-dashboard-shortcut-face))))
(insert (format "%s%s\n" (make-string left-margin ?\s) title-with-path-and-shortcut))))))
(defun welcome-dashboard--todo-icon ()
"Todo icon."
(if welcome-dashboard-use-nerd-icons
(propertize (nerd-icons-octicon "nf-oct-alert")
'display '(raise 0))
(propertize (all-the-icons-octicon "alert")
'face `(:height 1.0)
'display '(raise 0))))
(defun welcome-dashboard--insert-todos ()
"Insert todos."
(when (> (length welcome-dashboard-todos) 0)
(welcome-dashboard--insert-text
(format "%s %s!"
(propertize "You got work todo in" 'face 'welcome-dashboard-subtitle-face)
(propertize welcome-dashboard-last-project-name 'face 'welcome-dashboard-project-face)))
(dolist (todo welcome-dashboard-todos)
(let* ((index (cl-position todo welcome-dashboard-todos :test #'equal))
(shortcut (format "%d" (+ index +1)))
(path (nth 0 todo))
(type (nth 3 todo))
(text (nth 4 todo))
(title (format "%s %s %s %s"
(welcome-dashboard--todo-icon)
(propertize type 'face 'welcome-dashboard-todo-type-face)
(propertize (string-trim-left (welcome-dashboard--truncate-text-right text)) 'face 'welcome-dashboard-filename-face)
(propertize (format "[%s]" shortcut) 'face 'welcome-dashboard-shortcut-todo-face))))
(welcome-dashboard--insert-text
(propertize title 'path path))))))
(defun welcome-dashboard--calculate-padding-left ()
"Calculate padding for left side."
(let ((current-width (window-width)))
(when (or (null welcome-dashboard--padding-cache)
(not (eq current-width welcome-dashboard--last-window-width)))
(setq welcome-dashboard--last-window-width current-width)
(setq welcome-dashboard--padding-cache
(if-let* ((files welcome-dashboard-recentfiles)
(max-length (apply #'max (mapcar (lambda (path)
(length (welcome-dashboard--truncate-path-in-middle path welcome-dashboard-path-max-length)))
files)))
(filenames (mapcar (lambda (path) (file-name-nondirectory path)) files))
(max-filename-length (/ (apply #'max (mapcar #'length filenames)) 2))
(left-margin (max (+ welcome-dashboard-min-left-padding max-filename-length)
(/ (- current-width max-length) 2))))
(- left-margin max-filename-length)
welcome-dashboard-min-left-padding)))
welcome-dashboard--padding-cache))
(defun welcome-dashboard--insert-text (text)
"Insert (as TEXT)."
(let ((left-margin (welcome-dashboard--calculate-padding-left)))
(insert (format "%s%s\n" (make-string left-margin ?\s) text))))
(defun welcome-dashboard--redisplay-buffer-on-resize (&rest _)
"Resize current buffer."
(when (equal (buffer-name) welcome-dashboard-buffer)
(welcome-dashboard--refresh-screen)))
(defun welcome-dashboard--fetch-weather-data (&optional initial)
"Fetch weather data from API. INITIAL indicates if this is the first fetch."
(let ((url-request-method "GET")
(url-request-extra-headers '(("Content-Type" . "application/json")))
(url (format "https://api.open-meteo.com/v1/forecast?latitude=%s&longitude=%s¤t_weather=true"
welcome-dashboard-latitude welcome-dashboard-longitude)))
(url-retrieve url
(lambda (_)
(goto-char (point-min))
(re-search-forward "^$")
(let* ((json-data (buffer-substring-no-properties (point) (point-max)))
(json-obj (json-read-from-string json-data))
(current-weather (cdr (assoc 'current_weather json-obj)))
(temp (cdr (assoc 'temperature current-weather)))
(weather-code (cdr (assoc 'weathercode current-weather)))
(weather-icon (welcome-dashboard--weather-icon-from-code weather-code)))
(setq welcome-dashboard-weathericon weather-icon)
(if welcome-dashboard-use-fahrenheit
(setq welcome-dashboard-temperature (format "%.1f" (+ (* temp 1.8) 32)))
(setq welcome-dashboard-temperature (format "%.1f" temp)))
(setq welcome-dashboard-weatherdescription
(format "%s" (welcome-dashboard--weather-code-to-string weather-code)))
;; Only set up the recurring timer after initial fetch
(when initial
(run-with-timer 900 900 #'welcome-dashboard--fetch-weather-data))
(when (welcome-dashboard--isActive)
(welcome-dashboard--refresh-screen))))
nil
t)))
(defun welcome-dashboard-create-welcome-hook ()
"Setup welcome-dashboard screen."
(when (< (length command-line-args) 2)
(remove-hook 'switch-to-buffer #'welcome-dashboard--redisplay-buffer-on-resize)
(add-hook 'window-configuration-change-hook #'welcome-dashboard--redisplay-buffer-on-resize)
(add-hook 'emacs-startup-hook (lambda ()
(welcome-dashboard--refresh-screen)
(run-with-idle-timer 0.1 nil #'welcome-dashboard--fetch-todos t)
(when (welcome-dashboard--show-weather-info)
(run-with-idle-timer 0.1 nil #'welcome-dashboard--fetch-weather-data t))))))
(defun welcome-dashboard--truncate-text-right (text)
"Truncate TEXT at the right to a maximum of 100 characters."
(if (> (length text) welcome-dashboard-path-max-length)
(concat (substring text 0 (- welcome-dashboard-path-max-length 3)) "...")
text))
(defun welcome-dashboard--clock-icon ()
"Get the clock icon."
(if welcome-dashboard-use-nerd-icons
(propertize (nerd-icons-octicon "nf-oct-clock")
'display '(raise 0))
(propertize (all-the-icons-octicon "clock")
'face `(:family ,(all-the-icons-octicon-family) :height 1.0)
'display '(raise 0))))
(defun welcome-dashboard--insert-startup-time ()
"Insert startup time."
(welcome-dashboard--insert-text (format "%s %s %s %s"
(welcome-dashboard--clock-icon)
(propertize "Startup time:" 'face 'welcome-dashboard-text-info-face)
(propertize (emacs-init-time "%.2f") 'face 'welcome-dashboard-startup-time-face)
(propertize "seconds" 'face 'welcome-dashboard-text-info-face))))
(defun welcome-dashboard--package-icon ()
"Get the package icon."
(if welcome-dashboard-use-nerd-icons
(propertize (nerd-icons-codicon "nf-cod-package")
'display '(raise -0.1))
(propertize (all-the-icons-octicon "package")
'face `(:family ,(all-the-icons-octicon-family) :height 1.0)
'display '(raise -0.1))))
(defun welcome-dashboard--insert-package-info (packages)
"Insert package info as (PACKAGES)."
(welcome-dashboard--insert-text (format "%s %s %s"
(welcome-dashboard--package-icon)
(propertize packages 'face 'welcome-dashboard-info-face 'display '(raise -0.1))
(propertize "packages loaded" 'face 'welcome-dashboard-text-info-face 'display '(raise -0.1)))))
(defun welcome-dashboard--temperature-symbol ()
"Get the correct type of temperature symbol."
(if welcome-dashboard-use-fahrenheit
"℉"
"℃"))
(defun welcome-dashboard--show-weather-info ()
"Check if we latitude and longitude and then show weather info."
(let ((latitude welcome-dashboard-latitude)
(longitude welcome-dashboard-longitude))
(if (and (floatp latitude) (floatp longitude) (> latitude 0.0) (> longitude 0.0))
t
nil)))
(defun welcome-dashboard--insert-weather-info ()
"Insert weather info."
(when (welcome-dashboard--show-weather-info)
(if welcome-dashboard-weatherdescription
(welcome-dashboard--insert-text (format "%s %s, %s%s"
(propertize welcome-dashboard-weathericon 'face '(:family "Weather icons" :height 1.0) 'display '(raise 0))
(propertize welcome-dashboard-weatherdescription 'face 'welcome-dashboard-weather-description-face)
(propertize welcome-dashboard-temperature 'face 'welcome-dashboard-weather-temperature-face)
(propertize (welcome-dashboard--temperature-symbol) 'face 'welcome-dashboard-text-info-face)))
(welcome-dashboard--insert-text (propertize "Loading weather data..." 'face 'welcome-dashboard-weather-temperature-face)))))
(defun welcome-dashboard--parse-todo-result (result)
"Parse the RESULT and create a list of todo items."
(let ((regex "\\(.+?\\):\\([0-9]+\\):\\([0-9]+\\):\s?\\W+\\(.*\\):\\(.*\\)$"))
(save-match-data
(let (matches todos)
(while (string-match regex result)
(setq matches (list (match-string 1 result)
(match-string 2 result)
(match-string 3 result)
(match-string 4 result)
(match-string 5 result)))
(setq result (substring result (match-end 0)))
(push matches todos))
(nreverse todos)))))
(cl-defun welcome-dashboard--async-command-to-string (&key command &key callback)
"Async shell command to JSON run async (as COMMAND)
and parse it json and call (as CALLBACK)."
(async-start
`(lambda ()
(shell-command-to-string ,command))
`(lambda (result)
(funcall ,callback result))))
(defun welcome-dashboard--last-root ()
"Get the version control root directory of the most recent file."
(when (> (length welcome-dashboard-recentfiles) 0)
(let ((file (car welcome-dashboard-recentfiles)))
(vc-find-root file ".git"))))
(defun welcome-dashboard--fetch-todos (&optional initial)
"Fetch todos. When INITIAL is t, set up recurring updates."
(when (> welcome-dashboard-max-number-of-todos 0)
(when (and (executable-find "rg") (welcome-dashboard--last-root))
(let* ((root (welcome-dashboard--last-root))
(projectname (file-name-nondirectory (directory-file-name root)))
(command (format "rg -e \"(TODO|FIX|FIXME|PERF|HACK|NOTE):\s+\" --color=never --no-heading --with-filename --line-number --column --sort path %s" root))
(is-initial initial)) ; Capture the initial value in closure
(setq welcome-dashboard-last-project-name projectname)
(welcome-dashboard--async-command-to-string
:command command
:callback `(lambda (result)
(setq welcome-dashboard-todos
(seq-take (welcome-dashboard--parse-todo-result result)
welcome-dashboard-max-number-of-todos))
;; Use the captured is-initial value
(when ,is-initial
(run-with-timer 300 300 #'welcome-dashboard--fetch-todos))
(when (welcome-dashboard--isActive)
(welcome-dashboard--refresh-screen))))))))
(defun welcome-dashboard--package-length ()
"Get the number of installed packages."
(cond
((bound-and-true-p package-alist)
(length package-activated-list))
((boundp 'straight--profile-cache)
(hash-table-count straight--profile-cache))
((boundp 'elpaca--queued)
(length elpaca--queued))
(t 0)))
(defun welcome-dashboard--isActive ()
"Check if buffer is active and visible."
(or (eq welcome-dashboard-buffer (window-buffer (selected-window)))
(get-buffer-window welcome-dashboard-buffer 'visible)))
(defun welcome-dashboard--refresh-screen ()
"Show the welcome-dashboard screen."
(setq welcome-dashboard-recentfiles (seq-take recentf-list 9))
(with-current-buffer (get-buffer-create welcome-dashboard-buffer)
(let* ((buffer-read-only)
(image (create-image welcome-dashboard-image-file 'png nil :width welcome-dashboard-image-width :height welcome-dashboard-image-height))
(size (image-size image))
(width (car size))
(left-margin (max welcome-dashboard-min-left-padding (floor (/ (- (window-width) width) 2))))
(packages (format "%d" (welcome-dashboard--package-length))))
(erase-buffer)
(goto-char (point-min))
(let ((inhibit-read-only t))
(welcome-dashboard--insert-text (propertize welcome-dashboard-title 'face 'welcome-dashboard-title-face))
(insert "\n")
(welcome-dashboard--insert-recent-projects)
(welcome-dashboard--insert-separator)
(welcome-dashboard--insert-recent-files)
(setq cursor-type nil)
(when (> (length welcome-dashboard-todos) 0)
(insert "\n")
(welcome-dashboard--insert-todos))
(welcome-dashboard--insert-separator)
(welcome-dashboard--insert-startup-time)
(welcome-dashboard--insert-package-info packages)
(when (welcome-dashboard--show-weather-info)
(welcome-dashboard--insert-weather-info))
(insert "\n")
(welcome-dashboard--insert-centered (propertize (format-time-string "%A, %B %d %R") 'face 'welcome-dashboard-time-face))
(insert "\n\n")
(insert (make-string left-margin ?\ ))
(insert-image image)
(when (not (welcome-dashboard--isActive))
(switch-to-buffer welcome-dashboard-buffer))
(welcome-dashboard-mode)
(goto-char (point-min))
(forward-line 3)))))
(defun welcome-dashboard--insert-separator ()
"Insert a separator line."
(insert "\n")
(welcome-dashboard--insert-text
(propertize (make-string (+ welcome-dashboard-path-max-length (* welcome-dashboard-min-left-padding 2)) ?─) 'face 'welcome-dashboard-separator-face)))
(provide 'welcome-dashboard)
;;; welcome-dashboard.el ends here