Contents
  1. 1. tree
  2. 2. 思路

tree

模仿tree指令写的脚本 file.py

效果如下:显示当前目录的文件树

1
2
3
4
5
6
7
8
9
10
11
12
13
~/workspace/shell/tree » ./file.py xana@localhost
.
├── arr.js
├── arr.py
├── file.py
├── test
│ ├── 1中文.txt
│ ├── 2.txt
│ └── test
│ └── 1.txt
└── z.txt
2 directories, 7 files

(实际上路径显示为蓝色,文件显示为绿色)

思路

最开始的时候是在数据结构课做完了作业没事干,开始沿着二叉树的递归思想,准备写一个文件树脚本,并准备用当前的shell知识给不同类型的文件染上不同的颜色。

我首先想到的就是用JS就能较为容易的实现。(大雾

1、首先用一个复杂的数组(相当于Lisp的广义表)来进行递归测试

2、递归过程中,用一个数字来代替文件名字之前的特殊图形符号组。

1
2
3
4
5
// 用一个4进制数表示图像
// 0: ( )
// 1 (│ ) 当前数组i!=a.length-1,而level++下层时
// 2: (├── ) 当前数组i!=a.length-1,本层时
// 3: (└── ) 当前数组i==a.length-1

3、因为符号有四种,用一个4进制数字理论上就能代表所有的图形。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*从右往左
.
├── 4.txt //002
├── index.js //002
├── package.json //002
├── qwe //002
├── test //002
│   ├── 1.txt //021
│   ├── 2.txt //021
│   └── test //031
│   └── 1.txt //301
└── tree.js //003
//000
*/

4、递归函数应当包含以下特征:1)根据是否为数组而决定是否递归 2)根据是否为数组的最后一项而决定是两种图形组数的处理方式的哪一种。

5、想法测试

arr.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env node
var tree = [
"1001",
"1002",
"1003",
[],
[
"fuck",
[
"shit",
"bitch"
],
//"fuck"
],
"1004",
"1005",
"1006",
"status",
[
"AC",
"TL",
"WA",
"CE",
[
"a",
"b"
],
"~",
"OUT!"
],
[
[
"a",
"c"
],
"b"
],
[
[
["a"]
]
],
//"status",
];
function flo(level){
var str = "";
while (level) {
var lev = level%4;
switch(lev) {
case 0:str+=" " ;break;
case 1:str+="│ " ;break;
case 2:str+="├── " ;break;
case 3:str+="└── " ;break;
}
level = (level-lev)/4
}
return str;
}
function max(level){ // 查看level最高位 3->1 33->2
var count=0;
while(level){
level=Math.floor(level/4);
count++;
}
return count
}
function draw(floor,element){
if (typeof(element) == "object") {
element="object: ["+element+"] ";
}
console.log(floor+element);
}
function ele2(i,level){
var end = i.length-1;
var t = Math.pow(4,max(level)-1);
for(var x in i) {
if (x<end) { // not end
draw(flo(level),i[x]);
if (typeof i[x] == "object") { // 2x -> 20x
ele2(i[x],level+t*4+3*t); //todo: add | mid
}
} else { // end
draw(flo(level+t),i[x]);
if (typeof i[x] == "object") { // the place is tested out, not cal
ele2(i[x],level+t*4+2*t); //todo add | end
}
}
}
}
console.log('.');
var lev = 2;
ele2(tree, lev);

测试结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
~/workspace/shell/tree » ./arr.js xana@localhost
.
├── 1001
├── 1002
├── 1003
├── object: []
├── object: [fuck,shit,bitch]
│ ├── fuck
│ └── object: [shit,bitch]
│ ├── shit
│ └── bitch
├── 1004
├── 1005
├── 1006
├── status
├── object: [AC,TL,WA,CE,a,b,~,OUT!]
│ ├── AC
│ ├── TL
│ ├── WA
│ ├── CE
│ ├── object: [a,b]
│ │ ├── a
│ │ └── b
│ ├── ~
│ └── OUT!
├── object: [a,c,b]
│ ├── object: [a,c]
│ │ ├── a
│ │ └── c
│ └── b
└── object: [a]
└── object: [a]
└── object: [a]
└── a

6、用node改写处理文件树,将是否为数组判断改为文件 isDirectory() 修改时间期望15min

7、控制不了异步返回结果,开始重写为回调。

8、(十年后)卒。

9、吃饭的时候想到用非异步就好了。

10、重用Python,重复步骤1-5.

11、测试想法

arr.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
import os, sys, math
tree = [ '1001','1002','1003',[], [ 'fuck', [ 'shit', 'bitch' ] ], '1004', '1005', '1006', 'status', [ 'AC', 'TL', 'WA', 'CE', [ 'a', 'b' ], '~', 'OUT!' ], [ [ 'a', 'c' ], 'b' ], [ [ [ ] ] ] ]
#4进制 最高位 3->1 5->2
def maxf(level):
count=0
while level:
level = math.floor(level/4)
count += 1
return count
# 用一个4进制数表示图像
# 0: ( )
# 1 (│ ) 当前数组i!=a.length-1,而level++下层时
# 2: (├── ) 当前数组i!=a.length-1,本层时
# 3: (└── ) 当前数组i==a.length-1
img = [" ", "│ ", "├── ", "└── "]
# shape of floor
def flo(level):
pic = ""
while level:
lev = level%4
pic += img[lev]
level = math.floor(level/4)
return pic
# draw the floor
def draw(floor, element):
if isinstance(element,list):
element = "list: " + str(element)
print(floor + element)
# 递归函数
def ele(i, level):
t = pow(4, maxf(level)-1)
for x in range(len(i)):
if x < len(i)-1:
draw(flo(level), i[x])
if isinstance(i[x],list):
ele(i[x], level+t*4+3*t)
else:
draw(flo(level+t), i[x])
if isinstance(i[x],list):
ele(i[x], level+t*4+2*t)
print('.')
ele(tree,2)

测试结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
~/workspace/shell/tree » ./arr.py xana@localhost
.
├── 1001
├── 1002
├── 1003
├── list: []
├── list: ['fuck', ['shit', 'bitch']]
│ ├── fuck
│ └── list: ['shit', 'bitch']
│ ├── shit
│ └── bitch
├── 1004
├── 1005
├── 1006
├── status
├── list: ['AC', 'TL', 'WA', 'CE', ['a', 'b'], '~', 'OUT!']
│ ├── AC
│ ├── TL
│ ├── WA
│ ├── CE
│ ├── list: ['a', 'b']
│ │ ├── a
│ │ └── b
│ ├── ~
│ └── OUT!
├── list: [['a', 'c'], 'b']
│ ├── list: ['a', 'c']
│ │ ├── a
│ │ └── c
│ └── b
└── list: [[[]]]
└── list: [[]]
└── list: []

12、改写处理文件树,将是否为数组判断改为文件 os.path.isdir([path]), 修改时间期望15min

13、done.

file.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
import os, sys, math
path = os.getcwd()
root = os.listdir(path)
img = [ ' ', '│ ', '├── ', '└── ' ]
Color = {'default': '\033[0m', 'red': '\033[31m', 'green': '\033[32m', 'grey': '\033[36m'}
counts = {'directories': 0, 'files': 0}
#4进制 最高位 3->1 5->2
def maxf(level):
count=0
while level:
level = math.floor(level/4)
count += 1
return count
# shape the floor
def flo(level):
pic = ''
while level:
lev = level % 4
pic += img[lev]
level = math.floor(level/4)
return pic
# draw the floor
def draw(floor, element, isDir):
color = 'default'
if isDir:
color = 'grey'
counts['directories'] += 1
else:
counts['files'] += 1
print(floor + Color[color] + str(element) + Color['default'])
# 递归函数
def ele(i, level, dire):
i = list(filter(lambda x:x[0] != '.', i))
t = pow(4, maxf(level) - 1)
for x in range(len(i)):
if x < len(i) - 1:
Dire = os.path.join(dire, i[x])
draw(flo(level), i[x], os.path.isdir(Dire))
if os.path.isdir(Dire):
dirlist = os.listdir(Dire)
ele(dirlist, level+t*4+3*t, Dire)
else:
Dire = os.path.join(dire, i[x])
draw(flo(level+t), i[x], os.path.isdir(Dire))
if os.path.isdir(Dire):
dirlist = os.listdir(Dire)
ele(dirlist, level+t*4+2*t, Dire)
print('.')
ele(root, 2, '.')
print()
print(counts['directories'],'directories,',counts['files'],'files')
Contents
  1. 1. tree
  2. 2. 思路