2021-10-18 21:20:19 +02:00
|
|
|
tool
|
|
|
|
extends "res://addons/text_editor/TE_RichTextLabel.gd"
|
|
|
|
|
|
|
|
var chapter_info:Array = []
|
|
|
|
var sort_on:String = "words"
|
2021-10-24 17:27:27 +02:00
|
|
|
var sort_on_index:int = 1
|
|
|
|
var sort_reverse:Dictionary = { id=true, chaps=true, words=true, uwords=true, "%":true, modified=true }
|
|
|
|
var skip_words:PoolStringArray
|
2021-10-18 21:20:19 +02:00
|
|
|
|
|
|
|
func _ready():
|
2021-10-19 01:50:21 +02:00
|
|
|
var btn = get_parent().get_node("update")
|
|
|
|
btn.add_font_override("font", editor.FONT_R)
|
|
|
|
|
|
|
|
var _e = btn.connect("pressed", self, "_update")
|
2021-10-18 21:20:19 +02:00
|
|
|
|
2021-10-19 01:50:21 +02:00
|
|
|
func _update():
|
2021-10-18 21:20:19 +02:00
|
|
|
chapter_info.clear()
|
|
|
|
|
2021-10-24 17:27:27 +02:00
|
|
|
# load block list
|
|
|
|
var skip_list = editor.current_directory.plus_file("word_skip_list.txt")
|
|
|
|
skip_words = TE_Util.load_text(skip_list).replace("\n", " ").strip_edges().to_lower().split(" ")
|
|
|
|
|
2021-10-18 21:20:19 +02:00
|
|
|
for path in editor.file_paths:
|
|
|
|
var file = path.get_file()
|
|
|
|
var ext = file.get_extension()
|
|
|
|
match ext:
|
|
|
|
"md": _process_md(path)
|
|
|
|
|
2021-10-19 01:50:21 +02:00
|
|
|
# clear empty
|
|
|
|
for i in range(len(chapter_info)-1, -1, -1):
|
|
|
|
var info = chapter_info[i]
|
|
|
|
if not info.words:
|
|
|
|
chapter_info.remove(i)
|
|
|
|
|
2021-10-18 21:20:19 +02:00
|
|
|
_sort()
|
|
|
|
_redraw()
|
|
|
|
|
2021-10-24 17:27:27 +02:00
|
|
|
const WEEKDAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
|
|
|
|
const MONTHS = ["Januaray", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
|
|
|
|
|
|
|
|
const TIMES:Dictionary = {
|
|
|
|
"second": 60,
|
|
|
|
"minute": 60,
|
|
|
|
"hour": 24,
|
|
|
|
"day": INF
|
|
|
|
}
|
|
|
|
func get_time(t:int) -> String:
|
|
|
|
for k in TIMES:
|
|
|
|
if t < TIMES[k]:
|
|
|
|
return "%s %s ago" % [t, k + ("" if t == 1 else "s")]
|
|
|
|
t /= TIMES[k]
|
|
|
|
return "???"
|
|
|
|
|
2021-10-18 21:20:19 +02:00
|
|
|
func _process_md(path:String):
|
|
|
|
var lines = TE_Util.load_text(path).split("\n")
|
2021-10-24 17:27:27 +02:00
|
|
|
var file_time = File.new().get_modified_time(path)
|
|
|
|
var curr_time = OS.get_unix_time()
|
|
|
|
var diff_time = curr_time - file_time
|
|
|
|
var time_nice = get_time(diff_time)
|
2021-10-19 01:50:21 +02:00
|
|
|
|
2021-10-24 17:27:27 +02:00
|
|
|
if false and diff_time > 9999999:
|
|
|
|
time_nice = OS.get_datetime_from_unix_time(file_time)
|
|
|
|
time_nice.weekday = WEEKDAYS[time_nice.weekday-1].substr(0, 3).to_lower()
|
|
|
|
time_nice.month = MONTHS[time_nice.month-1].substr(0, 3).to_lower()
|
|
|
|
time_nice.hour12 = str(time_nice.hour % 12)
|
|
|
|
time_nice.ampm = "am" if time_nice.hour > 12 else "pm"
|
|
|
|
time_nice = "{weekday} {month} {day}, {year} {hour12}:{minute}:{second}{ampm}".format(time_nice)
|
|
|
|
|
|
|
|
var out = { path=path, line=0, id=editor.get_localized_path(path), modified=file_time, time_nice=time_nice, words=0, uwords={}, chaps=0, "%":0.0 }
|
|
|
|
chapter_info.append(out)
|
2021-10-18 21:20:19 +02:00
|
|
|
var i = 0
|
|
|
|
while i < len(lines):
|
|
|
|
# skip head meta
|
|
|
|
if i == 0 and lines[i].begins_with("---"):
|
|
|
|
i += 1
|
|
|
|
while i < len(lines) and not lines[i].begins_with("---"):
|
2021-10-24 17:27:27 +02:00
|
|
|
if ":" in lines[i]:
|
|
|
|
var p = lines[i].split(":", true, 1)
|
|
|
|
var k = p[0].strip_edges()
|
|
|
|
var v = p[1].strip_edges()
|
|
|
|
match k:
|
|
|
|
"name":
|
|
|
|
out.id = v
|
|
|
|
|
|
|
|
"prog", "progress":
|
|
|
|
out["%"] = float(v.replace("%", ""))
|
2021-10-23 05:51:34 +02:00
|
|
|
|
2021-10-18 21:20:19 +02:00
|
|
|
i += 1
|
|
|
|
|
2021-10-24 17:27:27 +02:00
|
|
|
# skip comments
|
|
|
|
elif "<!--" in lines[i]:
|
|
|
|
pass
|
|
|
|
|
2021-10-18 21:20:19 +02:00
|
|
|
# skip code blocks
|
|
|
|
elif lines[i].begins_with("~~~") or lines[i].begins_with("```"):
|
|
|
|
var head = lines[i].substr(0, 3)
|
|
|
|
i += 1
|
|
|
|
while i < len(lines) and not lines[i].begins_with(head):
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
# heading
|
|
|
|
elif lines[i].begins_with("#"):
|
|
|
|
var p = lines[i].split(" ", true, 1)
|
2021-10-19 01:50:21 +02:00
|
|
|
var id = lines[i].split(" ", true, 1)
|
|
|
|
var deep = len(id[0])
|
|
|
|
id = "???" if len(id) == 1 else id[1].strip_edges()
|
2021-10-24 17:27:27 +02:00
|
|
|
out.chaps += 1
|
2021-10-18 21:20:19 +02:00
|
|
|
|
|
|
|
else:
|
2021-10-24 17:27:27 +02:00
|
|
|
out.words += TE_Util.count_words(lines[i], out.uwords, skip_words)
|
2021-10-18 21:20:19 +02:00
|
|
|
|
|
|
|
i += 1
|
2021-10-24 17:27:27 +02:00
|
|
|
|
|
|
|
# sort word counts
|
|
|
|
TE_Util.sort_vals(out.uwords)
|
|
|
|
var words = PoolStringArray(out.uwords.keys())
|
|
|
|
var words_top = words
|
|
|
|
if len(words_top) > 16:
|
|
|
|
words_top.resize(16)
|
|
|
|
out.uwords = words_top.join(" ")
|
|
|
|
|
|
|
|
var word_lines = [""]
|
|
|
|
for word in words:
|
|
|
|
if len(word_lines[-1]) >= 64:
|
|
|
|
word_lines.append("")
|
|
|
|
if word_lines[-1]:
|
|
|
|
word_lines[-1] += " "
|
|
|
|
word_lines[-1] += word
|
|
|
|
out.uwords_all = PoolStringArray(word_lines).join("\n")
|
|
|
|
|
2021-10-18 21:20:19 +02:00
|
|
|
func _clicked(args):
|
|
|
|
match args[0]:
|
|
|
|
"sort_table":
|
|
|
|
var key = args[1]
|
|
|
|
if sort_on != key:
|
|
|
|
sort_on = key
|
2021-10-24 17:27:27 +02:00
|
|
|
sort_on_index = sort_reverse.keys().find(sort_on)
|
2021-10-18 21:20:19 +02:00
|
|
|
else:
|
|
|
|
sort_reverse[key] = not sort_reverse[key]
|
|
|
|
|
|
|
|
_sort()
|
|
|
|
_redraw()
|
|
|
|
|
|
|
|
"goto":
|
|
|
|
var tab = editor.open_file(args[1])
|
|
|
|
editor.select_file(args[1])
|
|
|
|
tab.goto_line(args[2])
|
|
|
|
|
|
|
|
func _sort():
|
|
|
|
if sort_reverse[sort_on]:
|
|
|
|
chapter_info.sort_custom(self, "_sort_chapters_r")
|
2021-10-19 01:50:21 +02:00
|
|
|
else:
|
|
|
|
chapter_info.sort_custom(self, "_sort_chapters")
|
2021-10-18 21:20:19 +02:00
|
|
|
|
2021-10-19 01:50:21 +02:00
|
|
|
func _sort_chapters(a, b):
|
|
|
|
return a[sort_on] < b[sort_on]
|
|
|
|
|
|
|
|
func _sort_chapters_r(a, b):
|
|
|
|
return a[sort_on] > b[sort_on]
|
2021-10-18 21:20:19 +02:00
|
|
|
|
|
|
|
func _redraw():
|
|
|
|
clear()
|
|
|
|
|
|
|
|
var c1 = Color.white.darkened(.4)
|
|
|
|
var c2 = Color.white.darkened(.3)
|
2021-10-24 17:27:27 +02:00
|
|
|
var ch1 = lerp(c1, Color.yellowgreen, .5)
|
|
|
|
var ch2 = lerp(c2, Color.yellowgreen, .5)
|
|
|
|
|
|
|
|
var cols = ["id", "chaps", "words", "uwords", "%", "modified"]
|
2021-10-18 21:20:19 +02:00
|
|
|
push_align(RichTextLabel.ALIGN_CENTER)
|
2021-10-19 01:50:21 +02:00
|
|
|
push_table(len(cols))
|
2021-10-24 17:27:27 +02:00
|
|
|
add_constant_override("table_hseparation", 8)
|
|
|
|
|
2021-10-19 01:50:21 +02:00
|
|
|
for id in cols:
|
2021-10-18 21:20:19 +02:00
|
|
|
push_cell()
|
|
|
|
push_bold()
|
|
|
|
push_meta(add_meta(["sort_table", id], "sort on %s" % id))
|
|
|
|
add_text(id)
|
|
|
|
if sort_on == id:
|
2021-10-19 01:50:21 +02:00
|
|
|
push_color(Color.greenyellow.darkened(.25))
|
|
|
|
add_text(" ⯅" if sort_reverse[id] else " ⯆")
|
|
|
|
pop()
|
|
|
|
else:
|
|
|
|
push_color(Color.white.darkened(.7))
|
2021-10-18 21:20:19 +02:00
|
|
|
add_text(" ⯅" if sort_reverse[id] else " ⯆")
|
|
|
|
pop()
|
|
|
|
pop()
|
|
|
|
pop()
|
|
|
|
pop()
|
|
|
|
|
|
|
|
for i in len(chapter_info):
|
|
|
|
var item = chapter_info[i]
|
|
|
|
var clr = c1 if i%2==0 else c2
|
2021-10-24 17:27:27 +02:00
|
|
|
var clrh = ch1 if i%2==0 else ch2
|
2021-10-18 21:20:19 +02:00
|
|
|
|
|
|
|
# id
|
|
|
|
push_cell()
|
2021-10-24 17:27:27 +02:00
|
|
|
push_color(clrh if sort_on_index == 0 else clr)
|
|
|
|
push_meta(add_meta(["goto", item.path, item.line], item.path + "\n" + item.uwords_all))
|
2021-10-18 21:20:19 +02:00
|
|
|
add_text(item.id)
|
|
|
|
pop()
|
|
|
|
pop()
|
|
|
|
pop()
|
|
|
|
|
2021-10-24 17:27:27 +02:00
|
|
|
# chapters
|
|
|
|
push_cell()
|
|
|
|
push_color(clrh if sort_on_index == 1 else clr)
|
|
|
|
add_text(TE_Util.commas(item.chaps))
|
|
|
|
pop()
|
|
|
|
pop()
|
|
|
|
|
2021-10-18 21:20:19 +02:00
|
|
|
# word cound
|
2021-10-24 17:27:27 +02:00
|
|
|
push_cell()
|
|
|
|
push_color(clrh if sort_on_index == 2 else clr)
|
|
|
|
add_text(TE_Util.commas(item.words))
|
|
|
|
pop()
|
|
|
|
pop()
|
|
|
|
|
|
|
|
# unique words
|
|
|
|
push_cell()
|
|
|
|
push_color(clrh if sort_on_index == 3 else clr)
|
|
|
|
add_text(item.uwords)
|
|
|
|
pop()
|
|
|
|
pop()
|
2021-10-23 05:51:34 +02:00
|
|
|
|
|
|
|
# percent
|
|
|
|
push_cell()
|
2021-10-24 17:27:27 +02:00
|
|
|
push_color(clrh if sort_on_index == 4 else clr)
|
2021-10-23 05:51:34 +02:00
|
|
|
add_text(str(int(item["%"])))
|
|
|
|
pop()
|
|
|
|
pop()
|
2021-10-24 17:27:27 +02:00
|
|
|
|
|
|
|
# time
|
|
|
|
push_cell()
|
|
|
|
push_color(clrh if sort_on_index == 5 else clr)
|
|
|
|
add_text(item.time_nice)
|
|
|
|
pop()
|
|
|
|
pop()
|
2021-10-18 21:20:19 +02:00
|
|
|
|
|
|
|
pop()
|
|
|
|
pop()
|