forked from chendonming/weather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
545 lines (505 loc) · 18.5 KB
/
extension.js
File metadata and controls
545 lines (505 loc) · 18.5 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// 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");
// Get configuration dynamically
function getConfig() {
const config = vscode.workspace.getConfiguration("weather");
return {
autoUpdate: config.get("autoUpdate"),
showLifeIndex: config.get("showLifeIndex"),
position:
config.get("position") === "left"
? vscode.StatusBarAlignment.Left
: vscode.StatusBarAlignment.Right,
language: config.get("language") || "en",
key: config.get("key"),
apiUrl: config.get("apiUrl"),
};
}
// Localization strings
const strings = {
zh: {
enterCity: "输入城市名( 中国/全球 )",
selectLocation: "选择一个地区",
currentConditions: "当前实况",
tomorrowForecast: "明日预报",
minTemp: "最低温度",
maxTemp: "最高温度",
day: "白天",
night: "晚上",
lifeIndex: "生活指数",
lifeIndexLevel: "生活指数等级",
requestError: "Weather: 请求错误",
setupWelcome: "欢迎使用天气扩展!让我们进行设置。",
setupLanguage: "选择显示语言",
setupPosition: "选择状态栏位置",
setupAutoUpdate: "启用自动更新(每2小时)",
setupShowLifeIndex: "显示生活指数",
setupApiKey: "输入您的和风天气 API 密钥(可选,按 Enter 跳过)",
setupApiUrl: "输入 API URL(可选,按 Enter 使用默认值)",
setupComplete: "设置完成!正在加载天气...",
openSettings: "打开设置",
yes: "是",
no: "否",
left: "左侧",
right: "右侧",
apiKeyWarning:
"警告:默认 API 密钥即将过期。建议您配置自己的 API 密钥。",
configureNow: "立即配置",
later: "稍后",
},
ug: {
enterCity: "شەھەر نامىنى كىرگۈزۈڭ (جۇڭگو/دۇنيا)",
selectLocation: "بىر جاينى تاللاڭ",
currentConditions: "ھازىرقى ھاۋارايى",
tomorrowForecast: "ئەتە",
minTemp: "ئەڭ تۆۋەن",
maxTemp: "ئەڭ يۇقىرى",
day: "كۈندۈز",
night: "كېچە",
lifeIndex: "تۇرمۇش كۆرسەتكۈچى",
lifeIndexLevel: "تۇرمۇش كۆرسەتكۈچ دەرىجىسى",
requestError: "ھاۋارايى: خاتالىق كۆرۈلدى",
setupWelcome: "ھاۋارايى كېڭەيتمىسىگە خۇش كەپسىز! سەپلەيلى.",
setupLanguage: "كۆرسىتىش تىلىنى تاللاڭ",
setupPosition: "ھالەت بالداق ئورنىنى تاللاڭ",
setupAutoUpdate: "ئاپتوماتىك يېڭىلاشنى قوزغىتىش (ھەر 2 سائەت)",
setupShowLifeIndex: "تۇرمۇش كۆرسەتكۈچنى كۆرسىتىش",
setupApiKey:
"QWeather API ئاچقۇچىڭىزنى كىرگۈزۈڭ (تاللاش، Enter بېسىپ ئاتلاڭ)",
setupApiUrl:
"API URL كىرگۈزۈڭ (تاللاش، Enter بېسىپ كۆڭۈلدىكىنى ئىشلىتىڭ)",
setupComplete: "سەپلەش تامام! ھاۋارايى يۈكلىنىۋاتىدۇ...",
openSettings: "تەڭشەكلەرنى ئېچىش",
yes: "شۇنداق",
no: "ياق",
left: "سول",
right: "ئوڭ",
apiKeyWarning:
"ئاگاھلاندۇرۇش: كۆڭۈلدىكى API ئاچقۇچىنىڭ ۋاقتى توشماقچى. ئۆزىڭىزنىڭ API ئاچقۇچىنى سەپلىشىڭىزنى تەۋسىيە قىلىمىز.",
configureNow: "ھازىر سەپلەش",
later: "كېيىن",
},
en: {
enterCity: "Enter city name (China/Global)",
selectLocation: "Select a location",
currentConditions: "Current Conditions",
tomorrowForecast: "Tomorrow: Min",
minTemp: "Min",
maxTemp: "Max",
day: "Day",
night: "Night",
lifeIndex: "Life Index",
lifeIndexLevel: "Life Index Level",
requestError: "Weather: Request Error",
setupWelcome: "Welcome to Weather Extension! Let's set it up.",
setupLanguage: "Select display language",
setupPosition: "Choose status bar position",
setupAutoUpdate: "Enable auto-update (every 2 hours)",
setupShowLifeIndex: "Show life index",
setupApiKey:
"Enter your QWeather API key (optional, press Enter to skip)",
setupApiUrl: "Enter API URL (optional, press Enter to use default)",
setupComplete: "Setup complete! Loading weather...",
openSettings: "Open Settings",
yes: "Yes",
no: "No",
left: "Left",
right: "Right",
apiKeyWarning:
"Warning: The default API key will expire soon. It's recommended to configure your own API key.",
configureNow: "Configure Now",
later: "Later",
},
};
function t(key) {
const lang = getConfig().language;
return strings[lang] && strings[lang][key]
? strings[lang][key]
: strings["en"][key];
}
// Helper function to ensure API URL has protocol
function normalizeApiUrl(url) {
if (!url) return url;
url = url.trim();
if (!url.startsWith("http://") && !url.startsWith("https://")) {
return "https://" + url;
}
return url;
}
// Helper function to format location name with proper case
function formatLocationName(name) {
// For English names, convert to title case (e.g., HAIDIAN -> Haidian)
if (!/[\u4e00-\u9fa5]/.test(name)) {
// No Chinese characters, format as title case
return name
.split(" ")
.map(
(word) =>
word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
)
.join(" ");
}
// Keep Chinese names as-is
return name;
}
// First-time setup wizard
async function runSetupWizard(context) {
const config = vscode.workspace.getConfiguration("weather");
// Language selection
const langChoice = await vscode.window.showQuickPick(
[
{ label: "English (US)", value: "en" },
{ label: "简体中文 (Simplified Chinese)", value: "zh" },
{ label: "ئۇيغۇرچە (Uighur)", value: "ug" },
{ label: "Español (Spanish)", value: "es" },
{ label: "Français (French)", value: "fr" },
{ label: "Deutsch (German)", value: "de" },
{ label: "日本語 (Japanese)", value: "ja" },
{ label: "한국어 (Korean)", value: "ko" },
],
{
placeHolder: "Select display language / 选择显示语言",
ignoreFocusOut: true,
}
);
if (langChoice) {
await config.update(
"language",
langChoice.value,
vscode.ConfigurationTarget.Global
);
// Position selection
const posChoice = await vscode.window.showQuickPick(
[
{ label: t("left"), value: "left" },
{ label: t("right"), value: "right" },
],
{
placeHolder: t("setupPosition"),
ignoreFocusOut: true,
}
);
if (posChoice) {
await config.update(
"position",
posChoice.value,
vscode.ConfigurationTarget.Global
);
}
// Auto-update selection
const autoUpdateChoice = await vscode.window.showQuickPick(
[
{ label: t("yes"), value: true },
{ label: t("no"), value: false },
],
{
placeHolder: t("setupAutoUpdate"),
ignoreFocusOut: true,
}
);
if (autoUpdateChoice !== undefined) {
await config.update(
"autoUpdate",
autoUpdateChoice.value,
vscode.ConfigurationTarget.Global
);
}
// Show life index selection
const lifeIndexChoice = await vscode.window.showQuickPick(
[
{ label: t("yes"), value: true },
{ label: t("no"), value: false },
],
{
placeHolder: t("setupShowLifeIndex"),
ignoreFocusOut: true,
}
);
if (lifeIndexChoice !== undefined) {
await config.update(
"showLifeIndex",
lifeIndexChoice.value,
vscode.ConfigurationTarget.Global
);
}
// API key (optional)
const apiKey = await vscode.window.showInputBox({
prompt: t("setupApiKey"),
placeHolder: "e.g., 1234567890abcdef",
ignoreFocusOut: true,
});
if (apiKey && apiKey.trim()) {
await config.update(
"key",
apiKey.trim(),
vscode.ConfigurationTarget.Global
);
// API URL (optional)
const apiUrl = await vscode.window.showInputBox({
prompt: t("setupApiUrl"),
placeHolder: "https://api.qweather.com",
ignoreFocusOut: true,
});
if (apiUrl && apiUrl.trim()) {
const normalizedUrl = normalizeApiUrl(apiUrl.trim());
await config.update(
"apiUrl",
normalizedUrl,
vscode.ConfigurationTarget.Global
);
}
}
// Mark setup as complete
await context.globalState.update("setupComplete", true);
vscode.window.showInformationMessage(t("setupComplete"));
}
}
// 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;
let updateInterval;
/**
* @param {vscode.ExtensionContext} context
*/
async function activate(context) {
// Check if this is first-time setup
const setupComplete = context.globalState.get("setupComplete");
if (!setupComplete) {
await runSetupWizard(context);
}
// Warn about default API key
const config = getConfig();
if (config.key === "579fbf44a7b24519a9d50e35258c1467") {
const choice = await vscode.window.showWarningMessage(
t("apiKeyWarning"),
t("configureNow"),
t("later")
);
if (choice === t("configureNow")) {
vscode.commands.executeCommand(
"workbench.action.openSettings",
"weather"
);
}
}
let disposable = vscode.commands.registerCommand(
"extension.weather",
async function (customPosition) {
const config = getConfig();
const position =
customPosition !== undefined ? customPosition : config.position;
let locationId = context.globalState.get("locationId");
let location = context.globalState.get("location");
if (!locationId || !location) {
location = await vscode.window.showInputBox({
placeHolder: t("enterCity"),
value: "Haidian",
});
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 = t("currentConditions");
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 = `${t("tomorrowForecast")} ${
nextDay.tempMin
}℃ ${t("maxTemp")} ${nextDay.tempMax}℃ ${t("day")} ${
nextDay.textDay
}, ${t("night")} ${nextDay.textNight}`;
barNext.show();
if (config.showLifeIndex) {
const lifeIndex = await getLifeIndex(locationId);
barLife ? barLife.dispose() : "";
barLife = vscode.window.createStatusBarItem(position);
barLife.text = `${t("lifeIndex")}: ${lifeIndex.category}`;
barLife.tooltip = `${t("lifeIndexLevel")}: ${
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: t("enterCity"),
value: "Haidian",
});
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: t("enterCity"),
value: "Haidian",
});
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);
}
}
);
// Setup wizard command
let setupWizard = vscode.commands.registerCommand(
"extension.setupWizard",
async function () {
await runSetupWizard(context);
vscode.commands.executeCommand("extension.weather");
}
);
// Initial weather display
vscode.commands.executeCommand("extension.weather");
// Setup auto-update with dynamic config
function setupAutoUpdate() {
if (updateInterval) {
clearInterval(updateInterval);
updateInterval = null;
}
const config = getConfig();
if (config.autoUpdate) {
updateInterval = setInterval(function () {
vscode.commands.executeCommand("extension.weather");
}, 1000 * 60 * 60 * 2);
}
}
setupAutoUpdate();
// Listen for configuration changes
context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration("weather")) {
setupAutoUpdate();
// Refresh weather display
vscode.commands.executeCommand("extension.weather");
}
})
);
context.subscriptions.push(replacecity, disposable, addCity, setupWizard);
}
function getNowWeather(locationId) {
return weather.nowWeather(locationId).then((now) => {
if (now.data.code === "200") {
const res = now.data.now;
return {
temp: res.temp,
text: res.text,
};
} else {
netError();
throw new Error("Weather API error");
}
});
}
function getForecast(locationId) {
return weather.forecast(locationId).then((now) => {
if (now.data.code === "200") {
const res = now.data.daily[0];
return {
tempMax: res.tempMax,
tempMin: res.tempMin,
textDay: res.textDay,
textNight: res.textNight,
};
} else {
netError();
throw new Error("Weather API error");
}
});
}
function getLifeIndex(locationId) {
return weather.getIndices(locationId).then((index) => {
if (index.data.code === "200") {
const res = index.data.daily[0];
return {
desc: res.text,
category: res.category,
level: res.level,
fxLink: index.data.fxLink,
};
} else {
netError();
throw new Error("Weather API error");
}
});
}
function pickLocation(location) {
return weather.getLocation(location).then(async (result) => {
if (result.data.code === "200") {
const detailLocation = await vscode.window.showQuickPick(
result.data.location.map(
(v) => formatLocationName(v.name) + "-" + v.id
),
{
placeHolder: t("selectLocation"),
}
);
if (detailLocation) {
const locationId = detailLocation.split("-")[1];
return locationId;
}
} else {
netError();
throw new Error("Location API error");
}
});
}
function netError() {
vscode.window.showErrorMessage(t("requestError"));
}
// this method is called when your extension is deactivated
function deactivate() {
if (updateInterval) {
clearInterval(updateInterval);
}
if (bar) bar.dispose();
if (barNext) barNext.dispose();
if (barLife) barLife.dispose();
}
module.exports = {
activate,
deactivate,
};