@@ -3,6 +3,47 @@ var path = require('path');
33
44var _ = require ( 'underscore' ) ;
55
6+ var UNITS_SIZE = [
7+ { unit : 'B' , name : 'Bytes' , count : 1024 } ,
8+ { unit : 'K' , name : 'KBytes' , count : 1024 } ,
9+ { unit : 'M' , name : 'MBytes' , count : 1024 } ,
10+ { unit : 'G' , name : 'GBytes' , count : - 1 }
11+ ] ;
12+
13+ var UNITS_TIME = [
14+ { unit : 's' , name : 'seconds' , count : 60 } ,
15+ { unit : 'm' , name : 'minutes' , count : 60 } ,
16+ { unit : 'h' , name : 'hours' , count : 24 } ,
17+ { unit : 'd' , name : 'days' , count : 7 } ,
18+ { unit : 'w' , name : 'weeks' , count : 4 } ,
19+ { unit : 'm' , name : 'months' , count : 12 } ,
20+ { unit : 'y' , name : 'years' , count : - 1 }
21+ ] ;
22+
23+ function getUnit ( units , v ) {
24+ for ( var i = 0 ; i < units . length ; ++ i ) {
25+ if ( units [ i ] . count <= 0 || v < units [ i ] . count )
26+ return [ v , units [ i ] ] ;
27+ v /= units [ i ] . count ;
28+ }
29+ }
30+
31+ var LANGS = [
32+ { lang : 'bash' , ext : '.sh' , style : '#' } ,
33+ { lang : 'c' , ext : '.c' , style : 'c' } ,
34+ { lang : 'cpp' , ext : '.cpp' , style : 'c' } ,
35+ { lang : 'csharp' , ext : '.cs' , style : 'c' } ,
36+ { lang : 'golang' , ext : '.go' , style : 'c' } ,
37+ { lang : 'java' , ext : '.java' , style : 'c' } ,
38+ { lang : 'javascript' , ext : '.js' , style : 'c' } ,
39+ { lang : 'mysql' , ext : '.sql' , style : '#' } ,
40+ { lang : 'python' , ext : '.py' , style : '#' } ,
41+ { lang : 'python3' , ext : '.py3' , style : '#' } ,
42+ { lang : 'ruby' , ext : '.rb' , style : '#' } ,
43+ { lang : 'scala' , ext : '.scala' , style : 'c' } ,
44+ { lang : 'swift' , ext : '.swift' , style : 'c' }
45+ ] ;
46+
647var h = { } ;
748
849h . prettyState = function ( state ) {
@@ -23,33 +64,14 @@ h.prettyText = function(text, yesNo) {
2364 }
2465} ;
2566
26- h . prettySize = function ( size ) {
27- var units = 'BKMG' ;
28- var i = 0 ;
29- while ( size >= 1024 && i < units . length ) {
30- size /= 1024.0 ;
31- ++ i ;
32- }
33- return size . toFixed ( 2 ) + units [ i ] ;
34- } ;
35-
36- h . prettyTime = function ( d ) {
37- var units = [
38- [ 60 , 'secs' ] ,
39- [ 60 , 'mins' ] ,
40- [ 24 , 'hours' ] ,
41- [ 7 , 'days' ] ,
42- [ 4 , 'weeks' ] ,
43- [ 12 , 'months' ] ,
44- [ 9999 , 'years' ]
45- ] ;
46-
47- var i = 0 ;
48- while ( d >= units [ i ] [ 0 ] && i < units . length ) {
49- d /= units [ i ] [ 0 ] ;
50- ++ i ;
51- }
52- return d . toFixed ( 0 ) + ' ' + units [ i ] [ 1 ] ;
67+ h . prettySize = function ( n ) {
68+ var res = getUnit ( UNITS_SIZE , n ) ;
69+ return res [ 0 ] . toFixed ( 2 ) + res [ 1 ] . unit ;
70+ } ;
71+
72+ h . prettyTime = function ( n ) {
73+ var res = getUnit ( UNITS_TIME , n ) ;
74+ return res [ 0 ] . toFixed ( 0 ) + ' ' + res [ 1 ] . name ;
5375} ;
5476
5577h . levelToName = function ( level ) {
@@ -77,71 +99,28 @@ h.statusToName = function(sc) {
7799} ;
78100
79101h . langToExt = function ( lang ) {
80- switch ( lang ) {
81- case 'bash' : return '.sh' ;
82- case 'c' : return '.c' ;
83- case 'cpp' : return '.cpp' ;
84- case 'csharp' : return '.cs' ;
85- case 'golang' : return '.go' ;
86- case 'java' : return '.java' ;
87- case 'javascript' : return '.js' ;
88- case 'mysql' : return '.sql' ;
89- case 'python' : return '.py' ;
90- case 'python3' : return '.py3' ;
91- case 'ruby' : return '.rb' ;
92- case 'scala' : return '.scala' ;
93- case 'swift' : return '.swift' ;
94- default : return '.raw' ;
95- }
102+ var res = _ . find ( LANGS , function ( x ) {
103+ return x . lang === lang ;
104+ } ) ;
105+ return res ? res . ext : '.raw' ;
96106} ;
97107
98108h . extToLang = function ( fullpath ) {
99109 var ext = path . extname ( fullpath ) ;
100- switch ( ext ) {
101- case '.c' : return 'c' ;
102- case '.cpp' : return 'cpp' ;
103- case '.cs' : return 'csharp' ;
104- case '.go' : return 'golang' ;
105- case '.java' : return 'java' ;
106- case '.js' : return 'javascript' ;
107- case '.py' : return 'python' ;
108- case '.py3' : return 'python3' ;
109- case '.rb' : return 'ruby' ;
110- case '.scala' : return 'scala' ;
111- case '.sh' : return 'bash' ;
112- case '.sql' : return 'mysql' ;
113- case '.swift' : return 'swift' ;
114- default : return 'unknown' ;
115- }
110+ var res = _ . find ( LANGS , function ( x ) {
111+ return x . ext === ext ;
112+ } ) ;
113+ return res ? res . lang : 'unknown' ;
116114} ;
117115
118116h . langToCommentStyle = function ( lang ) {
119- switch ( lang ) {
120- case 'c' :
121- case 'cpp' :
122- case 'csharp' :
123- case 'golang' :
124- case 'java' :
125- case 'javascript' :
126- case 'scala' :
127- case 'swift' :
128- default :
129- return {
130- commentHeader : '/*' ,
131- commentLine : ' *' ,
132- commentFooter : ' */'
133- } ;
134- case 'bash' :
135- case 'mysql' :
136- case 'python' :
137- case 'python3' :
138- case 'ruby' :
139- return {
140- commentHeader : '#' ,
141- commentLine : '#' ,
142- commentFooter : '#'
143- } ;
144- }
117+ var res = _ . find ( LANGS , function ( x ) {
118+ return x . lang === lang ;
119+ } ) ;
120+
121+ return ( res && res . style === '#' ) ?
122+ { commentHeader : '#' , commentLine : '#' , commentFooter : '#' } :
123+ { commentHeader : '/*' , commentLine : ' *' , commentFooter : ' */' } ;
145124} ;
146125
147126h . getFileData = function ( p ) {
0 commit comments