VSCode小说神器Thief-Book-VSCode页数获取
VSCode小說神器Thief-Book-VSCode頁數獲取
寫在前面
- 做這件事的原因:
- 用這個工具摸魚看小說吧,有一個問題,那就是摸魚的時候很爽,但是有空了可以光明正大看小說的時候,這么一行一行的看就十分離譜,不能跨設備同步看小說的進度,但是如果拿出來看吧(比如用手機看,手機可以搜索字段嘛),很容易定位自己摸魚看的進度。可是看了一段時間,又回到摸魚的時候,就很難定位自己的進度。
- 然后就是,在設置每一行的字數長度的時候(默認是50嘛),設置一次,進度全亂了,很氣。但是根據vscode不同的縮放比例(ctrl + ***+***),設置不同的每行字數,很重要,懂我什么意思叭(歪頭)。
- 所以,我就尋思,得想個辦法,可以根據某個關鍵詞,定位我看到哪兒了,然后計算這個是拿一頁。于是我開始了我的艱難的閱讀源碼之旅,然后寫個python腳本來解決這個問題
- 如果有需求的話,覺得腳本不香的話,我可以做個軟件,不懂編程的人也能用啦(但是不懂編程的人用vscode干神魔呢?(歪頭))
- 參考連接:
- Thief-Book-VSCode的github地址
- 主要參考源碼地址
- 插件安裝地址
解決方案
如果不會編程的話,我可以想辦法把程序做成小軟件,留言告訴我哦。(沒需求我就不做了)
以下是python代碼,環境python3.7
#!/usr/bin/env python # -*- coding: UTF-8 -*- # coding=utf-8 """ @author: Li Tian @contact: litian_cup@163.com @software: pycharm @file: xiaoshuo_locatoin.py @time: 2020/12/19 14:07 @desc: Thief-Book-VSCode查找小說的顯示進度 """from dataclasses import dataclass from math import ceil@dataclass class XiaoshuoLocation:line_length: intfile_path: strdef __post_init__(self):self.pages, self.view_list = self.get_location()def depart_line(self, line):count = int(len(line) / 30) + 1result_line = []for i in range(count):start = i * self.line_lengthend = start + self.line_lengthresult_line.append(line[start: end])return result_linedef get_location(self):"""獲取小說的顯示頁數"""with open(self.file_path, 'r', encoding='utf-8') as f:result = f.read()temp = result.replace('\n', " ").replace('\r', " ").replace(' ', " ").replace(' ', " ")return ceil(len(temp) / self.line_length), self.depart_line(temp)def search_words(self, aim_words):count = 1for line in self.view_list:if aim_words in line:print(count, ": ", line)count += 1if __name__ == '__main__':XiaoshuoLocation(30, 'E:\study_books\《明朝那些事》(全集).txt').search_words('小弟的肩膀')使用方式不用我教了叭
-
30:每行多少個字嘛,就是配置里面的那個Page Size
-
后面的就是小說的txt路徑啦
-
最后一個參數:就是要搜索的內容,如果查不到的話,如果你改了PageSize,是有可能查不到的,因為原來是一個page 的,可能分成兩個page了,被隔斷了,那就搜內容的前后一小部分看看。
-
結果展示:冒號前面的數字,就是你要的目標頁數啦。
源碼解析
講道理,看完源碼,覺得內存燒的有點多,內容啊、頁碼啊獲取一次就好啦,我看每次翻頁都要搞一次IO,很影響性能的。希望作者可以優化一下代碼,早日添加內容搜索定位頁碼的功能嗷!
首先啊,先貼上這個神器的源碼
import { ExtensionContext, workspace, window } from 'vscode'; import * as fs from "fs";export class Book {// 現在是哪一頁curr_page_number: number = 1;// 每一頁顯示的長度page_size: number | undefined = 50;// text分幾頁,總頁數page = 0;start = 0;end = this.page_size;// 文件路徑filePath: string | undefined = "";extensionContext: ExtensionContext;constructor(extensionContext: ExtensionContext) {this.extensionContext = extensionContext;}getSize(text: string) {// text長度let size = text.length;// text分幾頁,向上取整this.page = Math.ceil(size / this.page_size!);}getFileName() {var file_name: string | undefined = this.filePath!.split("/").pop();console.log(file_name);}getPage(type: string) {// 取出當前頁var curr_page = <number>workspace.getConfiguration().get('thiefBook.currPageNumber');var page = 0;if (type === "previous") {// 如果按了上一頁,當前頁減一或為第一頁if (curr_page! <= 1) {page = 1;} else {page = curr_page - 1;}} else if (type === "next") {// 如果按了下一頁,當前頁加一if (curr_page! >= this.page) {page = this.page;} else {page = curr_page + 1;}} else if (type === "curr") {page = curr_page;}this.curr_page_number = page;// this.curr_page_number = this.extensionContext.globalState.get("book_page_number", 1);}updatePage() {workspace.getConfiguration().update('thiefBook.currPageNumber', this.curr_page_number, true);}getStartEnd() {this.start = this.curr_page_number * this.page_size!;this.end = this.curr_page_number * this.page_size! - this.page_size!;}readFile() {if (this.filePath === "" || typeof (this.filePath) === "undefined") {window.showWarningMessage("請填寫TXT格式的小說文件路徑 & Please fill in the path of the TXT format novel file");}var data = fs.readFileSync(this.filePath!, 'utf-8');var line_break = <string>workspace.getConfiguration().get('thiefBook.lineBreak');return data.toString().replace(/\n/g, line_break).replace(/\r/g, " ").replace(/ /g, " ").replace(/ /g, " ");}init() {this.filePath = workspace.getConfiguration().get('thiefBook.filePath');var is_english = <boolean>workspace.getConfiguration().get('thiefBook.isEnglish');if (is_english === true) {this.page_size = <number>workspace.getConfiguration().get('thiefBook.pageSize') * 2;} else {this.page_size = workspace.getConfiguration().get('thiefBook.pageSize');}}getPreviousPage() {this.init();let text = this.readFile();this.getSize(text);this.getPage("previous");this.getStartEnd();var page_info = this.curr_page_number.toString() + "/" + this.page.toString();this.updatePage();return text.substring(this.start, this.end) + " " + page_info;}getNextPage() {this.init();let text = this.readFile();this.getSize(text);this.getPage("next");this.getStartEnd();var page_info = this.curr_page_number.toString() + "/" + this.page.toString();this.updatePage();return text.substring(this.start, this.end) + " " + page_info;}getJumpingPage() {this.init();let text = this.readFile();this.getSize(text);this.getPage("curr");this.getStartEnd();var page_info = this.curr_page_number.toString() + "/" + this.page.toString();this.updatePage();return text.substring(this.start, this.end) + " " + page_info;} }我們先看readFile()函數
readFile() {if (this.filePath === "" || typeof (this.filePath) === "undefined") {window.showWarningMessage("請填寫TXT格式的小說文件路徑 & Please fill in the path of the TXT format novel file");}var data = fs.readFileSync(this.filePath!, 'utf-8');var line_break = <string>workspace.getConfiguration().get('thiefBook.lineBreak');return data.toString().replace(/\n/g, line_break).replace(/\r/g, " ").replace(/ /g, " ").replace(/ /g, " "); }-
先讀文件路徑
-
然后讀文件
-
獲取line_break,也就是分隔符(默認是一個空格)
-
替換各個符號
-
/[\r]/g在js中是正則表達式對象,在兩個/之間的部分是表達式的主體,表示要匹配的字符串;g表示在整個字符串中搜索。所以這段代碼中要匹配的字符串是[\r]所代表的字符串,其中[]”表示字符的可選范圍。
-
首先是\n,即軟回車,替換為line_break
-
然后是把\r,即軟空格,表示回到當前行的行首,替換為一個空格
print('asdf\rqw') # qw -
然后是一個unicode為12288的字符,為全角空格,這里替換為一個空格,
print(ord(' ')) # 12288 -
然后是將空格轉換為空格,這里我蒙了。。。
-
最后返回的就是各種處理之后的小說文字了,string。
-
然后從按鍵的邏輯粗發
getPage(type: string) {// 取出當前頁var curr_page = <number>workspace.getConfiguration().get('thiefBook.currPageNumber');var page = 0;if (type === "previous") {// 如果按了上一頁,當前頁減一或為第一頁if (curr_page! <= 1) {page = 1;} else {page = curr_page - 1;}} else if (type === "next") {// 如果按了下一頁,當前頁加一if (curr_page! >= this.page) {page = this.page;} else {page = curr_page + 1;}} else if (type === "curr") {page = curr_page;}this.curr_page_number = page;- 先獲取當前頁,從設置里面的當前頁獲取這個值
- !:用在賦值的內容后時,使null和undefined類型可以賦值給其他類型并通過編譯
- 然后是if-else邏輯了:
- 如果是 上一頁 :先檢查當前頁是不是<=1,是的話就把page=1,否則page=當前頁-1
- 如果是 下一頁 :先檢查當前頁是不是>=小說的總頁數,是的話page=小說的總頁數,否則page=當前頁+1
- 如果是 當前頁 :page=當前頁
- 設置全局當前頁為page
初始化
init() {this.filePath = workspace.getConfiguration().get('thiefBook.filePath');var is_english = <boolean>workspace.getConfiguration().get('thiefBook.isEnglish');if (is_english === true) {this.page_size = <number>workspace.getConfiguration().get('thiefBook.pageSize') * 2;} else {this.page_size = workspace.getConfiguration().get('thiefBook.pageSize');} }- 獲取文件路徑,放到全局變量filePath
- 英文處理:這就不說了
- 獲取每一頁顯示的長度,默認50
- 所以初始化就這兩步
上一頁的邏輯
getPreviousPage() {this.init();let text = this.readFile();this.getSize(text);this.getPage("previous");this.getStartEnd();var page_info = this.curr_page_number.toString() + "/" + this.page.toString();this.updatePage();return text.substring(this.start, this.end) + " " + page_info; }- 初始化
- 獲取小說string
- getSize:獲取整篇小說的長度,并進行分頁
- 獲取上一頁,這里將全局變量curr_page_number設置為 上一頁
- 獲取開始和結束:
- 全局變量開始 = 全局變量curr_page_number × 每一頁長度
- 全局變量結束 = 全局變量curr_page_number × 每一頁長度 - 每一頁長度
- page_info:這里顯示當前頁/總頁數
- updatePage:將當前頁的值寫到配置文件中
- 返回:每頁的文字 + 四個空格 + page_info
- 下一頁和跳頁的邏輯幾乎一樣
我的CSDN:https://blog.csdn.net/qq_21579045
我的博客園:https://www.cnblogs.com/lyjun/
我的Github:https://github.com/TinyHandsome
紙上得來終覺淺,絕知此事要躬行~
歡迎大家過來OB~
by 李英俊小朋友
總結
以上是生活随笔為你收集整理的VSCode小说神器Thief-Book-VSCode页数获取的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最流行的统计假设检验速查表
- 下一篇: java if 小于等于_java –