Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/weibobo/stock-bar into weib…
Browse files Browse the repository at this point in the history
…obo-main
  • Loading branch information
Chef5 committed Oct 11, 2024
2 parents fcc8c72 + 0b47596 commit 73e077c
Show file tree
Hide file tree
Showing 14 changed files with 557 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ dist
node_modules
.vscode-test/
*.vsix
pnpm-lock.yaml
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"useTabs": true,
"singleQuote": true,
"trailingComma": "all"
"trailingComma": "all",
"endOfLine": "auto"
}
45 changes: 35 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@

VScode 插件 | A 股 | 港股 | 实时股票数据 | 状态栏实时更新

`Stock Bar`会在开盘期间自动刷新股票数据,并在VScode底部状态栏显示股票基本数据,让你在使用VScode期间能随时关注到你的股票
`Stock Bar`会在开盘期间自动刷新股票数据,并在 VScode 底部状态栏显示股票基本数据,让你在使用 VScode 期间能随时关注到你的股票

为了隐秘性,`Stock Bar`默认只会显示股价、百分点这样的纯数字,当你将鼠标移上去就可以查看详情。当然为了区分,也可以自定义显示股票的名称(建议使用英文字母,别问我为什么)

![image](https://raw.githubusercontent.com/Chef5/stock-bar/main/stock-bar-plugin.png)

插件已开源,开源地址:[Github](https://github.com/Chef5/stock-bar),欢迎点星星⭐️、提issue或者pr
插件已开源,开源地址:[Github](https://github.com/Chef5/stock-bar),欢迎点星星 ⭐️、提 issue 或者 pr

## 启动

(ctrl + shift + P) 后运行命令:

- stock watch start 开始显示
- stock watch stop 关闭显示

## 插件配置

修改用户配置,添加你所需要监控的股票代码

``` js
```js
// 股票:这是一个数组,你可以直接添加股票代码字符串,也可以添加对象,对象格式如下:
// {
// code: string, // 股票代码:需要添加股市前缀,前缀参考文档下方:前缀说明
Expand All @@ -29,6 +36,22 @@ VScode 插件 | A 股 | 港股 | 实时股票数据 | 状态栏实时更新
}
],

// 期货: 数组,可以添加期货代码
// {
// code: string, //期货代码, 比如 sa9999, sa2409
// alias: string, // 期货别名,这个可以不填,会自动根据代码获取
// hold_price: number, //持仓价格,即多空单的价格
// hold_number: number //持仓, 正数代表持有多单,负数代表持有空单,当配置了持仓价格和持仓后,
// 会显示持仓盈亏,否则显示当日价格涨跌幅
// }
"stock-bar.futures": [
{
"code": "sa9999",
"hold_price": 2500,
"hold_number": -1
}
],

// 更新数据时间间隔,单位:毫秒
"stock-bar.updateInterval": 10000

Expand All @@ -41,10 +64,10 @@ VScode 插件 | A 股 | 港股 | 实时股票数据 | 状态栏实时更新

## 前缀说明

- sh:沪市,不加前缀的情况下,6开头的代码默认加上sh(上证指数:sh000001)
- sz:深市,不加前缀的情况下,除6开头的代码外,默认加上sz
- sh:沪市,不加前缀的情况下,6 开头的代码默认加上 sh(上证指数:sh000001)
- sz:深市,不加前缀的情况下,除 6 开头的代码外,默认加上 sz
- hk:港股,如:阿里巴巴港股 hk09988
- US_:美股,如:苹果股票 US_AAPL
- US\_:美股,如:苹果股票 US_AAPL
- hkHSC:工商指数(港股指数)
- hkHSCEI:恒生中国企业指数(港股指数)
- hkHSI:恒生指数(港股指数)
Expand All @@ -55,15 +78,15 @@ VScode 插件 | A 股 | 港股 | 实时股票数据 | 状态栏实时更新
- hkGEM:标普香港创业板指(港股指数)
- US_DOWJONES:道琼斯指数(美股指数)
- US_NASDAQ:纳斯达克(美股指数)
- US_SP500:标普500(美股指数)
- US_SP500:标普 500(美股指数)

## 更新日志

[CHANGELOG](./CHANGELOG.md)

## 贡献者

感谢这些可爱的贡献者参与开发和维护Stock Bar,让`Stock Bar`更加完美!
感谢这些可爱的贡献者参与开发和维护 Stock Bar,让`Stock Bar`更加完美!

<p>
<a href="https://github.com/arrebole">
Expand All @@ -80,7 +103,9 @@ VScode 插件 | A 股 | 港股 | 实时股票数据 | 状态栏实时更新

##

> 插件源:`Stock Bar`最初Fork自[stock-watch](https://github.com/TDGarden/stock-watch),现在已对其进行了重大重构。
> 插件源:`Stock Bar`最初 Fork 自[stock-watch](https://github.com/TDGarden/stock-watch),现在已对其进行了重大重构。
> 股票数据来源:
> - 新浪
>
> - 新浪
> - 同花顺
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 23 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"Other"
],
"activationEvents": [
"*"
"*"
],
"main": "./dist/extension",
"contributes": {
Expand All @@ -34,6 +34,17 @@
],
"description": "股票代码数组,配置需要监控的股票代码,具体配置规则请查看插件详情页"
},
"stock-bar.futures": {
"type": "array",
"default": [
{
"code": "sa9999",
"hold_price": 2300,
"hold_number": -1
}
],
"description": "商品期货数组,配置需要监控的商品期货,具体配置规则请查看插件详情页"
},
"stock-bar.updateInterval": {
"type": "number",
"default": 10000,
Expand All @@ -50,7 +61,17 @@
"description": "股票跌的颜色,默认跟随系统"
}
}
}
},
"commands": [
{
"command": "stockbar.start",
"title": "stock watch start"
},
{
"command": "stockbar.stop",
"title": "stock watch stop"
}
]
},
"scripts": {
"vscode:prepublish": "npm run package",
Expand Down
12 changes: 11 additions & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { StockOptions } from 'stock-bar';
import { StockOptions, FutureOptions } from 'stock-bar';

export default class Configuration {
/**
Expand All @@ -24,6 +24,16 @@ export default class Configuration {
return stocks as StockOptions;
}

static getFutures() {
const futures = Configuration.stockBarConfig().get('futures');
if (!futures) {
return [];
}
const items = futures as FutureOptions;
items.forEach((item) => (item.code = item.code.toUpperCase()));
return items;
}

static getUpdateInterval() {
const updateInterval = Configuration.stockBarConfig().get('updateInterval');
return typeof updateInterval === 'number' ? updateInterval : 10000;
Expand Down
99 changes: 65 additions & 34 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import * as vscode from 'vscode';
import logger from './logger';
import Configuration from './configuration';
import { sinaStockProvider } from './provider';
import { render } from './render';
import timer from './timer';
import { render, renderFutures, stopAllRender } from './render';
import Stock from './stock';
import FutureHandler from './futures';
import { clearInterval } from 'timers';

function loadChoiceStocks() {
return Configuration.getStocks().map((v) => {
Expand All @@ -20,45 +21,75 @@ function loadChoiceStocks() {
});
}

export function activate(context: vscode.ExtensionContext) {
let stocks = loadChoiceStocks();
let timer = null;
let stocks: Stock[];

context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration(() => {
stocks = loadChoiceStocks();
}),
);
function restart() {
//console.log('restart');
const interval = Configuration.getUpdateInterval();
if (timer) {
clearInterval(timer);
timer = null;
}
stocks = loadChoiceStocks();
futureHandler.updateConfig(Configuration.getFutures());

const task: () => any = async () => {
try {
// 从云端获取最新状态
logger.debug('call fetchData');
const data = await sinaStockProvider.fetch(stocks.map((v) => v.code));
// 更新本地的数据
for (const origin of data) {
const stock = stocks.find((v) => v.code === origin.code);
if (!stock) {
continue;
}
stock.update(origin);
timer = setInterval(ticker, interval);
ticker();
}

const futureHandler = new FutureHandler();

async function ticker() {
try {
// 从云端获取最新状态
logger.debug('call fetchData');
const [data, _] = await Promise.all([
sinaStockProvider.fetch(stocks.map((v) => v.code)),
futureHandler.updateData(),
]);
// 更新本地的数据
for (const origin of data) {
const stock = stocks.find((v) => v.code === origin.code);
if (!stock) {
continue;
}
// 渲染内容
logger.debug('render');
render(stocks);
} catch (e) {
logger.error('%O', e);
stock.update(origin);
}
// 渲染内容
logger.debug('render');
render(stocks);
renderFutures(futureHandler.futures);
} catch (e) {
logger.error('%O', e);
}
}

// 阻塞等待下一个循环
logger.debug('timer await');
await timer.await();
function stop() {
//console.log('stop');
if (timer) {
clearInterval(timer);
timer = null;
}
stopAllRender();
}

export function activate(context: vscode.ExtensionContext) {
//console.log('activivate');
stocks = loadChoiceStocks();

// 继续循环
return task();
};
const startCmd = vscode.commands.registerCommand('stockbar.start', restart);
const stopCmd = vscode.commands.registerCommand('stockbar.stop', stop);

// 丢进宏任务队列
setTimeout(task);
context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration(() => {
if (timer) {
restart();
}
}),
);
context.subscriptions.push(startCmd);
context.subscriptions.push(stopCmd);
}

export function deactivate() {
Expand Down
Loading

0 comments on commit 73e077c

Please sign in to comment.