Skip to content

Commit

Permalink
Table view
Browse files Browse the repository at this point in the history
  • Loading branch information
twistezo committed Sep 15, 2021
1 parent c8964c0 commit ba2232a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 62 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.3.0

- Table view

## 3.2.0

- Added tabulators to logs for better readability
Expand Down
65 changes: 17 additions & 48 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"time-on-battery": "build/index.js",
"time-on-battery-m1": "build/index.js"
},
"version": "3.2.0",
"version": "3.3.0",
"description": "Mac OS app for measure real elapsed time on battery between charging. Without sleeping time unlike in system's Activity Monitor. Requires Big Sur and M1 processor.",
"homepage": "https://github.com/twistezo/time-on-battery-m1",
"author": "twistezo",
Expand Down Expand Up @@ -36,17 +36,18 @@
"cron": "^1.8.2",
"figlet": "^1.5.2",
"moment": "^2.29.1",
"shelljs": "^0.8.4"
"shelljs": "^0.8.4",
"table": "^6.7.1"
},
"devDependencies": {
"@types/cron": "^1.7.3",
"@types/figlet": "^1.5.4",
"@types/shelljs": "^0.8.9",
"@typescript-eslint/eslint-plugin": "^4.29.3",
"@typescript-eslint/parser": "^4.29.3",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
"eslint": "^7.32.0",
"pkg": "^5.3.1",
"ts-node": "^10.2.1",
"typescript": "^4.4.2"
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const APP_NAME = 'time-on-battery-m1'
export const VERSION = '3.2.0'
export const VERSION = '3.3.0'
export const DESCRIPTION = `Mac OS terminal app for measure real elapsed time on battery between charging.
Without sleeping time unlike in system's Activity Monitor.
v${VERSION}
Expand Down
30 changes: 20 additions & 10 deletions src/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
parseFormattedDate,
stringToBoolean,
} from './utils'
import { getBorderCharacters, table, TableUserConfig } from 'table'

const readRawData = (): string => fs.readFileSync(DATA_FILENAME).toString()

Expand Down Expand Up @@ -90,10 +91,8 @@ const calculatePeriodsOnBattery = (data: Data[], quantity: number): Log[] => {
batteryEnd = null
}

if (periodStart && !periodEnd) {
if (isLidClosedCurrent && isLidClosedNext) {
sleepDuration += durationBetween(dateNext, dateCurrent, DATE_FORMAT)
}
if (isLidClosedCurrent && isLidClosedNext) {
sleepDuration += durationBetween(dateNext, dateCurrent, DATE_FORMAT)
}

if (periodStart && periodEnd && batteryStart && batteryEnd) {
Expand Down Expand Up @@ -199,15 +198,26 @@ const print = ({

console.log(`\n${chalk.cyan(`Last ${quantity} periods on battery`)}`)
if (logs) {
logs.forEach((l: Log, i: number) => {
const data = logs.map((l: Log, i: number) => {
const [dateFrom, dateTo, timeOnBattery, batteryStart, batteryEnd] = l

console.log(
`${chalk.cyan(i + 1)} ${dateFrom} ${chalk.green(
batteryStart + '%'
)}\t- ${dateTo} ${chalk.yellow(batteryEnd + '%')}\t-> ${chalk.cyan(timeOnBattery)}`
)
const index = chalk.cyan(i + 1)
const batteryFrom = chalk.green(batteryStart + '%')
const batteryTo = chalk.yellow(batteryEnd + '%')
const tob = chalk.blue(timeOnBattery)

return [index, dateFrom, batteryFrom, dateTo, batteryTo, tob]
})

const headers = ['', chalk.cyan('From'), '%', 'To', '%', 'Time']
data.unshift(headers.map(h => chalk.cyan(h)))

const config: TableUserConfig = {
border: getBorderCharacters('ramac'),
drawHorizontalLine: (lineIndex, rowCount) =>
lineIndex === 0 || lineIndex === 1 || lineIndex === rowCount,
}
console.log(table(data, config))
} else {
notEnoughDataInfo()
}
Expand Down

0 comments on commit ba2232a

Please sign in to comment.