This repository has been archived by the owner on Mar 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
Customizable layouts #59
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3ff349e
advanced layout
alexkuz d6c6d6f
add missing files
alexkuz c34bf7e
fix tests
alexkuz ac6c5fd
add tests for generate-layouts
alexkuz ff86ec1
shim Object.assign for older Node versions
alexkuz 491197d
Use js layout config files instead of json
alexkuz 07b7ca1
use jsonschema to validate layout config; update tests for compability
alexkuz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
"use strict"; | ||
var _ = require("lodash"); | ||
var fs = require("fs"); | ||
/* eslint-disable no-magic-numbers */ | ||
|
||
// Each layout consists of vertical panels, that contains its position and horizontal views. | ||
// Flex-like positions of panels and views defined by 'grow' and 'size' parameters. | ||
// View or panel with 'size' has exactly <size> height or width respectively. | ||
// View or panel with 'grow' fills <grow> part of the residuary space (it works like flex-grow). | ||
// By default, position = { grow: 1 } | ||
|
||
var DEFAULT_LAYOUT_CONFIG = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default config should live in a separate file, say There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, js is better here. I replaced |
||
[ | ||
{ | ||
position: { | ||
grow: 3 | ||
}, | ||
views: [ | ||
{ | ||
name: "stdout" | ||
}, | ||
{ | ||
name: "stderr" | ||
} | ||
] | ||
}, | ||
{ | ||
views: [ | ||
{ | ||
name: "cpu", | ||
limit: 30 | ||
}, | ||
{ | ||
name: "eventLoop", | ||
limit: 30 | ||
}, | ||
{ | ||
name: "memory", | ||
position: { | ||
size: 15 | ||
} | ||
} | ||
] | ||
} | ||
], | ||
[ | ||
{ | ||
position: { | ||
grow: 3 | ||
}, | ||
views: [ | ||
{ | ||
name: "stdouterr" | ||
} | ||
] | ||
}, | ||
{ | ||
views: [ | ||
{ | ||
name: "cpu", | ||
limit: 30 | ||
}, | ||
{ | ||
name: "eventLoop", | ||
limit: 30 | ||
}, | ||
{ | ||
name: "memory", | ||
position: { | ||
size: 15 | ||
} | ||
} | ||
] | ||
} | ||
], | ||
[ | ||
{ | ||
views: [ | ||
{ | ||
name: "cpu", | ||
limit: 30 | ||
}, | ||
{ | ||
name: "eventLoop", | ||
limit: 30 | ||
} | ||
] | ||
} | ||
], | ||
[ | ||
{ | ||
views: [ | ||
{ | ||
name: "stdout" | ||
}, | ||
{ | ||
name: "stderr" | ||
} | ||
] | ||
} | ||
], | ||
[ | ||
{ | ||
views: [ | ||
{ | ||
name: "stdouterr" | ||
} | ||
] | ||
} | ||
] | ||
]; | ||
|
||
var normalizePosition = function (position) { | ||
if (!_.has(position, "grow") && !_.has(position, "size")) { | ||
position = { grow: 1 }; | ||
} | ||
|
||
return position; | ||
}; | ||
|
||
var concatPosition = function (position1, position2) { | ||
position1 = normalizePosition(position1); | ||
position2 = normalizePosition(position2); | ||
|
||
return { | ||
grow: (position1.grow || 0) + (position2.grow || 0), | ||
size: (position1.size || 0) + (position2.size || 0) | ||
}; | ||
}; | ||
|
||
var getSummaryPosition = function (items) { | ||
return items.map(function (item) { return item.position; }) | ||
.reduce(concatPosition, { grow: 0, size: 0 }); | ||
}; | ||
|
||
var getSize = function (parentSize, itemPosition) { | ||
var position = normalizePosition(itemPosition.position); | ||
if (_.has(position, "size")) { | ||
return position.size; | ||
} | ||
|
||
// Prevent last growing view from overflowing screen | ||
var round = itemPosition.offset.grow + position.grow === itemPosition.summary.grow ? | ||
Math.floor : Math.ceil; | ||
|
||
return round( | ||
(parentSize - itemPosition.summary.size) * position.grow / itemPosition.summary.grow | ||
); | ||
}; | ||
|
||
var getOffset = function (parentSize, itemPosition) { | ||
return itemPosition.summary.grow ? Math.ceil( | ||
itemPosition.offset.size + | ||
(parentSize - itemPosition.summary.size) * itemPosition.offset.grow / itemPosition.summary.grow | ||
) : 0; | ||
}; | ||
|
||
var createViewLayout = function (view, viewPosition, panelPosition) { | ||
return { | ||
name: view.name, | ||
limit: view.limit, | ||
getPosition: function (parent) { | ||
return { | ||
width: getSize(parent.width, panelPosition), | ||
height: getSize(parent.height, viewPosition), | ||
left: getOffset(parent.width, panelPosition), | ||
top: getOffset(parent.height, viewPosition) | ||
}; | ||
} | ||
}; | ||
}; | ||
|
||
var createPanelLayout = function (panelPosition, views) { | ||
var viewSummaryPosition = getSummaryPosition(views); | ||
var offsetPosition = { size: 0, grow: 0 }; | ||
|
||
return views.map(function (view) { | ||
var viewPosition = { | ||
summary: viewSummaryPosition, | ||
offset: offsetPosition, | ||
position: view.position | ||
}; | ||
|
||
offsetPosition = concatPosition(view.position, offsetPosition); | ||
|
||
return createViewLayout(view, viewPosition, panelPosition); | ||
}); | ||
}; | ||
|
||
var createLayout = function (panelsConfig) { | ||
var panelSummaryPosition = getSummaryPosition(panelsConfig); | ||
var offsetPosition = { size: 0, grow: 0 }; | ||
|
||
return panelsConfig.reduce(function (layouts, panelConfig) { | ||
var panelPosition = { | ||
summary: panelSummaryPosition, | ||
offset: offsetPosition, | ||
position: panelConfig.position | ||
}; | ||
|
||
var viewLayouts = createPanelLayout(panelPosition, panelConfig.views); | ||
_.each(viewLayouts, function (viewLayout) { | ||
layouts[viewLayout.name] = { | ||
getPosition: viewLayout.getPosition, | ||
limit: viewLayout.limit | ||
}; | ||
}); | ||
|
||
offsetPosition = concatPosition(panelConfig.position, offsetPosition); | ||
|
||
return layouts; | ||
}, {}); | ||
}; | ||
|
||
module.exports = function generateLayouts(layoutsFile) { | ||
var layoutConfig = DEFAULT_LAYOUT_CONFIG; | ||
if (layoutsFile) { | ||
var json = fs.readFileSync(layoutsFile, { encoding: "utf8" }); | ||
layoutConfig = JSON.parse(json); | ||
} | ||
|
||
return layoutConfig.map(createLayout); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file doesn't seem to exist?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops! Missed it.