Skip to content

Commit

Permalink
feat: add adjustSize action, resolve #523
Browse files Browse the repository at this point in the history
  • Loading branch information
weirongxu committed May 13, 2022
1 parent 8b97058 commit a87e24b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 47 deletions.
124 changes: 77 additions & 47 deletions src/actions/globalActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,83 @@ export function loadGlobalActions(action: ActionExplorer) {
},
);

// resize / adjust size
const parseSize = (sizeStr: string) => {
const [widthStr, heightStr] = sizeStr
.split(/,|x/)
.map((it) => it.trim()) as [string | undefined, string | undefined];
return [
widthStr ? parseInt(widthStr) : undefined,
heightStr ? parseInt(heightStr) : undefined,
] as const;
};
action.addNodeAction(
'resize',
async ({ args }) => {
const [sizeStr] = args as [string | undefined];
if (!sizeStr) return;
const [width, height] = parseSize(sizeStr);

if (explorer.isFloating) {
await explorer.resize([width, height]);
} else {
await explorer.resize([width]);
}
await explorer.render();
},
'resize',
{
args: [
{
name: 'size',
description: '{WIDTH}x{HEIGHT} | {WIDTH},{HEIGHT}',
},
],
menus: {
path: {
description: 'resize the explorer window',
args: '[size]',
async actionArgs() {
return [await input('input a the size:', '20,10', 'file')];
},
},
},
},
);
action.addNodeAction(
'adjustSize',
async ({ args }) => {
const [sizeStr] = args as [string | undefined];
if (!sizeStr) return;
const [width, height] = parseSize(sizeStr);

if (explorer.isFloating) {
await explorer.adjustSize([width, height]);
} else {
await explorer.adjustSize([width]);
}
await explorer.render();
},
'adjust window size',
{
args: [
{
name: 'size',
description: '+-{WIDTH}x+-{HEIGHT} | +-{WIDTH},+-{HEIGHT}',
},
],
menus: {
path: {
description: 'resize the explorer window',
args: '[size]',
async actionArgs() {
return [await input('input a the size:', '+20,-10', 'file')];
},
},
},
},
);

// other
action.addNodeAction(
'refresh',
Expand Down Expand Up @@ -668,53 +745,6 @@ export function loadGlobalActions(action: ActionExplorer) {
},
'render',
);
action.addNodeAction(
'resize',
async ({ args }) => {
await (async () => {
if (explorer.isFloating) {
const [sizeStr] = args as [string | undefined];
if (!sizeStr) {
return;
}
const [widthStr, heightStr] = sizeStr
.split(/,|x/)
.map((it) => it.trim()) as [string | undefined, string | undefined];
const [width, height] = [
widthStr ? parseInt(widthStr) : undefined,
heightStr ? parseInt(heightStr) : undefined,
];
await explorer.resize([width, height]);
} else {
const [widthStr] = args as [string | undefined];
if (!widthStr) {
return;
}
const width = parseInt(widthStr);
await explorer.resize([width]);
}
})();
await explorer.render();
},
'resize',
{
args: [
{
name: 'size',
description: 'widthxheight | width,height',
},
],
menus: {
path: {
description: 'resize the explorer window',
args: '[size]',
async actionArgs() {
return [await input('input a the size:', '20,10', 'file')];
},
},
},
},
);
action.addNodeAction(
'help',
async ({ source }) => {
Expand Down
11 changes: 11 additions & 0 deletions src/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,17 @@ export class Explorer implements Disposable {
await this.resizeNotifier(size).run();
}

async adjustSize(sizeOffset?: [width?: number, height?: number]) {
const [widthOff = 0, heightOff = 0] = sizeOffset ?? [0, 0];
const dimension = Explorer.genExplorerPosition(this.argValues, {
width: this.storedSize?.width,
height: this.storedSize?.height,
});
const { width, height } = dimension;

await this.resize([width + widthOff, height + heightOff]);
}

/**
* Focus on explorer window
* @returns Whether the focus is successful
Expand Down

0 comments on commit a87e24b

Please sign in to comment.