作一個CLI版的時間管理工具(八)

作一個CLI版的時間管理工具(八)

這是我參與8月更文挑戰的第9天,活動詳情查看:8月更文挑戰git

前言

上一篇文章主要闡述了刪除任務與事物管理相關的指令開發github

本文將着重介紹上文沒講解完的部分writeRecord方法,將記錄的事物自動輸出到配置的文件中json

本期效果

演示過程當中發現了一個bug,趕忙修復一下數組

圖片

功能開發

自動記錄事務

首先補齊打印事務耗時的邏輯markdown

  • 從獲取的配置文件中解構出事務對象thing,包含兩個屬性
    • name
    • startTime
  • 使用new Date(startTime)轉換開始日期
  • 調用mmsToNormal計算當前時間與開始時間的差值
const { thing } = config
const s = new Date(thing.startTime)
console.log(`事務耗時:${thing.name} ${mmsToNormal(Date.now() - s)}`);
複製代碼

mmsToNormal方法很是樸素,將一個毫秒錶示的時間換成天,時,分,秒app

  • 其中數字取整採用位運算符>>,右移0位
  • 逐級取值
function mmsToNormal(mms) {
    mms = (mms / 1000) >> 0
    const day = (mms / (24 * 60 * 60)) >> 0
    mms -= day * 24 * 60 * 60
    const hour = (mms / (60 * 60)) >> 0
    mms -= hour * 60 * 60
    const minute = (mms / 60) >> 0
    mms -= minute * 60
    return `${day}${hour}${minute}${mms}秒`
}
複製代碼

在結束當前事務和直接開始新的事務的時候都會打印這個任務的耗時,而後將其結果寫入到md中ide

開始新任務的邏輯以下:工具

  • 打印耗時
  • 輸出記錄到文件中
  • 更新事務的值爲最新的事務
  • 更新配置
console.log(`事務耗時:${thing.name} ${mmsToNormal(Date.now() - s)}`);
writeRecord(recordFilepath, task, thing.name, thing.startTime)

thing.name = name
thing.startTime = new Date().getTime()
writeFileSync(configPath, JSON.stringify(config))
複製代碼

下面開始介紹writeRecord的實現,接收4個參數oop

  • filePath:輸出目標文件的路徑
  • task:當前進行中的任務名
  • thing:當前進行中的事務名
  • startTime:事務開始時間
function writeRecord(filePath, task, thing, startTime){
    // ...code
}
複製代碼

經過getJSONgetFileContent方法協力將輸出目標文件轉爲jsonpost

將開始時間轉爲Date對象,調用Date.prototype.format方法獲取事務開始時間的日期

其中format方法的邏輯來源與網上大神寫的正則

事務持續時間保留5位小數

const json = getJSON(getFileContent(filePath))
const date = new Date(startTime)
const title = date.format('yyyy-MM-dd')
const hours = ((Date.now() - date.getTime()) / 3600000).toFixed(5)
複製代碼

導出json使用的是outPutMarkdown方法,默認導出結果不會帶時間,例如

# 時間
## 任務名
* content
* 事務2
複製代碼

經過修改獲取的json,將content內容加上時間,加上時間後的輸出結果

# 時間
## 任務名
* content time
* 事務2 0.02
複製代碼

修改原數據content加上time的邏輯以下:

  • 經過reduce方法遍歷全部的tasks
  • 經過map方法將全部task中的things讀取出來
  • 調用flat方法展開數組
  • 再遍歷每個thing,爲其content加上時間
const things = json.reduce((pre, v) => {
    const { tasks } = v
    const things = tasks.map(v => v.things).flat(2)
    return pre.concat(things)
}, [])
things.forEach(t => {
    const { content, time } = t
    t.content = `${content} ${time}`
})
複製代碼

下面開始核心邏輯

遍歷json判斷事務對應的日期是否已經存在

不存在則爲當天的首個數據,直接向json對象中插入這個完整的對象便可

const dayIdx = json.findIndex(v => v.title === title)

if (dayIdx === -1) {
    const item = {
        title,
        tasks: [
            {
                title: task,
                things: [
                    {
                        content: `${thing} ${hours}`,
                        time: '0'
                    }
                ]
            }
        ]
    }
    json.push(item)
    return writeFileSync(filePath, outPutMarkdown(json, false))
}
複製代碼

若是不是當天首個事務,接着就判斷是不是一個新的任務

遍歷這一天數據中的每個任務,判斷任務名是否和當前的任務一致

  • 若是是,則將這個任務及事務相關數據插入到這一天的數據中
  • dataItem表示這一天的數據
const dataItem = json[dayIdx]
const taskIdx = dataItem.tasks.findIndex(v => v.title === task)
// 新的任務
if (taskIdx === -1) {
    dataItem.tasks.push({
        title: task,
        things: [
            {
                content: `${thing} ${hours}`,
                time: '0'
            }
        ]
    })
    return writeFileSync(filePath, outPutMarkdown(json, false))
}
複製代碼

最後就是直接將事務插入到舊的任務當中

const taskItem = dataItem.tasks[taskIdx]
taskItem.things.push({
    content: `${thing} ${hours}`,
    time: '0'
})

return writeFileSync(filePath, outPutMarkdown(json, false))
複製代碼

到此timc thing [option] [name]指令基本開發完畢

  • --stop,-s:結束當前事務

其它

因爲天天空閒時間有限,本文就先到這

若是讀者還感受意猶未盡,敬請期待後續更新,或持續關注一下倉庫的狀態

歡迎評論區提需求,交流探討

本系列會不斷的更新迭代,直至產品初代完成

相關文章
相關標籤/搜索