4
4
PartialPrimitiveTargetDescriptor ,
5
5
ScopeType ,
6
6
} from "@cursorless/common" ;
7
- import globRaw from "glob" ;
8
- import { groupBy , map } from "lodash" ;
9
- import { promisify } from "node:util" ;
7
+ import { groupBy , map , sum } from "lodash" ;
10
8
import { asyncIteratorToList } from "./asyncIteratorToList" ;
11
9
import { canonicalizeAndValidateCommand } from "./core/commandVersionUpgrades/canonicalizeAndValidateCommand" ;
12
10
import { generateCommandHistoryEntries } from "./generateCommandHistoryEntries" ;
@@ -15,8 +13,6 @@ import { getPartialTargetDescriptors } from "./util/getPartialTargetDescriptors"
15
13
import { getPartialPrimitiveTargets } from "./util/getPrimitiveTargets" ;
16
14
import { getScopeType } from "./util/getScopeType" ;
17
15
18
- export const glob = promisify ( globRaw ) ;
19
-
20
16
/**
21
17
* Analyzes the command history for a given time period, and outputs a report
22
18
*/
@@ -36,21 +32,22 @@ class Period {
36
32
37
33
toString ( ) : string {
38
34
return [
39
- `[ ${ this . period } ] ` ,
40
- `Total commands : ${ this . count } ` ,
35
+ `# ${ this . period } ` ,
36
+ `Total command count : ${ this . count } ` ,
41
37
this . serializeMap ( "Actions" , this . actions ) ,
42
38
this . serializeMap ( "Modifiers" , this . modifiers ) ,
43
39
this . serializeMap ( "Scope types" , this . scopeTypes ) ,
44
40
] . join ( "\n\n" ) ;
45
41
}
46
42
47
43
private serializeMap ( name : string , map : Record < string , number > ) {
44
+ const total = sum ( Object . values ( map ) ) ;
48
45
const entries = Object . entries ( map ) ;
49
46
entries . sort ( ( a , b ) => b [ 1 ] - a [ 1 ] ) ;
50
47
const entriesSerialized = entries
51
- . map ( ( [ key , value ] ) => ` ${ key } : ${ value } ` )
48
+ . map ( ( [ key , value ] ) => ` ${ key } : ${ value } ( ${ toPercent ( value / total ) } ) ` )
52
49
. join ( "\n" ) ;
53
- return `${ name } ( ${ entries . length } ) :\n${ entriesSerialized } ` ;
50
+ return `${ name } :\n${ entriesSerialized } ` ;
54
51
}
55
52
56
53
private append ( entry : CommandHistoryEntry ) {
@@ -102,10 +99,21 @@ function getMonth(entry: CommandHistoryEntry): string {
102
99
export async function analyzeCommandHistory ( dir : string ) {
103
100
const entries = await asyncIteratorToList ( generateCommandHistoryEntries ( dir ) ) ;
104
101
105
- const content = map (
106
- Object . entries ( groupBy ( entries , getMonth ) ) ,
107
- ( [ key , entries ] ) => new Period ( key , entries ) . toString ( ) ,
108
- ) . join ( "\n\n" ) ;
102
+ const content = [
103
+ new Period ( "Totals" , entries ) . toString ( ) ,
104
+
105
+ ...map ( Object . entries ( groupBy ( entries , getMonth ) ) , ( [ key , entries ] ) =>
106
+ new Period ( key , entries ) . toString ( ) ,
107
+ ) ,
108
+ ] . join ( "\n\n\n" ) ;
109
109
110
110
await ide ( ) . openUntitledTextDocument ( { content } ) ;
111
111
}
112
+
113
+ function toPercent ( value : number ) {
114
+ return Intl . NumberFormat ( undefined , {
115
+ style : "percent" ,
116
+ minimumFractionDigits : 0 ,
117
+ maximumFractionDigits : 1 ,
118
+ } ) . format ( value ) ;
119
+ }
0 commit comments