Repository navigation
Expand file tree
/
Copy pathextension.js
More file actions
175 lines (158 loc) · 5.76 KB
/
Copy pathextension.js
File metadata and controls
175 lines (158 loc) · 5.76 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const weather = require('./weather');
const AutoUpdate = vscode.workspace.getConfiguration().get('weather.autoUpdate')
const ShowLifeIndex = vscode.workspace.getConfiguration().get('weather.showLifeIndex')
const barPosition = vscode.workspace.getConfiguration().get('weather.position') == "left" ? vscode.StatusBarAlignment.Left : vscode.StatusBarAlignment.Right
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
let bar, barNext, barLife;
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let disposable = vscode.commands.registerCommand('extension.weather', async function (position = barPosition) {
let locationId = context.globalState.get('locationId')
let location = context.globalState.get('location')
if (!locationId || !location) {
location = await vscode.window.showInputBox({ placeHolder: '输入城市名( 中国/全球 )' })
locationId = await pickLocation(location)
}
if (locationId) {
const now = await getNowWeather(locationId)
bar ? bar.dispose() : ''
bar = vscode.window.createStatusBarItem(position)
bar.text = location.split('-')[0] + ' ' + now.text + ' ' + now.temp + '℃'
bar.tooltip = '当前实况'
bar.command = 'extension.replacecity'
bar.show()
const nextDay = await getForecast(locationId)
barNext ? barNext.dispose() : ''
barNext = vscode.window.createStatusBarItem(position)
barNext.text = `${nextDay.tempMin}℃~${nextDay.tempMax}℃ ${nextDay.textDay}/${nextDay.textNight}`
barNext.tooltip = `明日预报: 最低温度${nextDay.tempMin}℃ 最高温度${nextDay.tempMax}℃ 白天${nextDay.textDay}, 晚上${nextDay.textNight}`
barNext.show()
if (ShowLifeIndex) {
const lifeIndex = await getLifeIndex(locationId)
barLife ? barLife.dispose() : ''
barLife = vscode.window.createStatusBarItem(position)
barLife.text = `生活指数:${lifeIndex.category}`
barLife.tooltip = `生活指数等级: ${lifeIndex.level} ${lifeIndex.text || ''}`
barLife.command = {
title: 'open',
command: 'vscode.open',
arguments: [vscode.Uri.parse(lifeIndex.fxLink)]
}
barLife.show()
}
context.globalState.update('locationId', locationId)
context.globalState.update('location', location.split('-')[0])
}
})
// 更换城市
let replacecity = vscode.commands.registerCommand('extension.replacecity', async function () {
const location = await vscode.window.showInputBox({ placeHolder: '输入城市名( 中国/全球 )' })
if (location === undefined) {
return
}
const locationId = await pickLocation(location)
if (location && locationId) {
context.globalState.update('locationId', locationId)
context.globalState.update('location', location.split('-')[0])
vscode.commands.executeCommand('extension.weather')
}
})
/**
* 添加城市
* @param {number} 位置 left=1 right=2
*/
let addCity = vscode.commands.registerCommand('extension.addCity', async function (position = 2) {
const location = await vscode.window.showInputBox({ placeHolder: '输入城市名( 中国/全球 )' })
const locationId = await pickLocation(location);
if (location && locationId) {
context.globalState.update('locationIdRight', locationId)
context.globalState.update('locationRight', location.split('-')[0])
vscode.commands.executeCommand('extension.weather', position)
}
});
vscode.commands.executeCommand('extension.weather')
if (AutoUpdate) {
setInterval(function () {
vscode.commands.executeCommand('extension.weather')
}, 1000 * 60 * 60 * 2)
}
context.subscriptions.push(replacecity, disposable, addCity)
}
function getNowWeather(locationId) {
return new Promise(async (resolve) => {
const now = await weather.nowWeather(locationId)
if (now.data.code === '200') {
const res = now.data.now
resolve({
temp: res.temp,
text: res.text
})
} else {
netError()
}
})
}
function getForecast(locationId) {
return new Promise(async (resolve) => {
const now = await weather.forecast(locationId)
if (now.data.code === '200') {
const res = now.data.daily[0]
resolve({
tempMax: res.tempMax,
tempMin: res.tempMin,
textDay: res.textDay,
textNight: res.textNight
})
} else {
netError()
}
})
}
function getLifeIndex(locationId) {
return new Promise(async (resolve) => {
const index = await weather.getIndices(locationId)
if (index.data.code === '200') {
const res = index.data.daily[0]
resolve({
desc: res.text,
category: res.category,
level: res.level,
fxLink: index.data.fxLink
})
} else {
netError()
}
})
}
function pickLocation(location) {
return new Promise(async (resolve) => {
const result = await weather.getLocation(location);
if (result.data.code === '200') {
const detailLocation = await vscode.window.showQuickPick(result.data.location.map(v => v.name + '-' + v.id), {
placeHolder: '选择一个地区',
});
if (detailLocation) {
const locationId = detailLocation.split('-')[1]
resolve(locationId)
}
} else {
netError()
}
})
}
function netError() {
vscode.window.showErrorMessage('Weather: 请求错误');
}
exports.activate = activate
// this method is called when your extension is deactivated
function deactivate() { }
module.exports = {
activate,
deactivate
}