Make a TIME Management Tool for the CLI (8)
This is the ninth day of my participation in the August More Text Challenge. For details, see “August More Text Challenge”.
preface
The previous article focused on deleting tasks and developing instructions related to business management
This article will focus on the parts of the writeRecord method that were not covered earlier, which automatically outputs recorded things to a configured file
This effect
I found a bug during the demo. Fix it
The function development
Automatic record transaction
First, complete the logic that prints the transaction time
- Deconstruct the transaction object from the obtained configuration file
thing
, contains two properties- name
- startTime
- use
new Date(startTime)
Conversion start date - call
mmsToNormal
Calculates the difference between the current time and the start time
const { thing } = config
const s = new Date(thing.startTime)
console.log('Transaction time:${thing.name} ${mmsToNormal(Date.now() - s)}`);
Copy the code
MmsToNormal is a simple method that replaces a millisecond representation of time with days, hours, minutes, and seconds
- The bitwise operator is used to round the number
>>
, move 0 bit to the right - Step by step values
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}day${hour}when${minute}points${mms}Second `
}
Copy the code
The task time is printed when the current transaction is finished and when a new transaction is started directly, and the result is then written to md
The logic for starting a new task is as follows:
- Print the time-consuming
- The output is recorded in a file
- Update the transaction value to the latest transaction
- Update the configuration
console.log('Transaction time:${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))
Copy the code
WriteRecord accepts four parameters
- FilePath: indicates the path of the output target file
- Task: Indicates the name of an ongoing task
- Thing: Name of the current ongoing transaction
- StartTime: indicates the startTime of the transaction
function writeRecord(filePath, task, thing, startTime){
/ /... code
}
Copy the code
The getJSON and getFileContent methods work together to convert the output target file to JSON
Convert the start time to a Date object and call the date.prototype. format method to get the Date at which the transaction started
The format method of logical sources and online gods write regular
The transaction duration is reserved to 5 decimal places
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)
Copy the code
Json is exported using the outPutMarkdown method. By default, the result is exported without time, for example
# time
# # task name
* content
*Transaction 2Copy the code
Modify the obtained JSON to add the time to the content content and the output after the time
# time
# # task name
* content time
*Transaction 2 0.02Copy the code
Modify the logic of content and time as follows:
- All tasks are traversed through the Reduce method
- All the things in the task are read by the map method
- Call the flat method to expand the array
- Iterate over each thing again, adding time to its 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}`
})
Copy the code
Let’s start with the core logic
The JSON is traversed to determine whether the date for the transaction already exists
If it does not exist, it is the first data of the day. Just insert the complete object into the JSON object
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))}Copy the code
If it’s not the first business of the day, then decide if it’s a new task
Check whether the task name is the same as the current task
- If so, the task and transaction-related data is inserted into the data for the day
dataItem
Represents the data of the day
const dataItem = json[dayIdx]
const taskIdx = dataItem.tasks.findIndex(v= > v.title === task)
// New task
if (taskIdx === -1) {
dataItem.tasks.push({
title: task,
things: [{content: `${thing} ${hours}`.time: '0'}]})return writeFileSync(filePath, outPutMarkdown(json, false))}Copy the code
The final step is to insert the transaction directly into the old task
const taskItem = dataItem.tasks[taskIdx]
taskItem.things.push({
content: `${thing} ${hours}`.time: '0'
})
return writeFileSync(filePath, outPutMarkdown(json, false))
Copy the code
Timc thing [option] [name
--stop,-s
: Ends the current transaction
other
Because of the limited free time each day, this article will stop there
If you don’t have enough, stay tuned for updates, or keep an eye on the status of the warehouse
Welcome to comment section to raise demand, exchange discussion
This series will continue to be updated and iterated until the first generation of the product is completed
- The warehouse address