From ef70c483338391fe2a6e2be5177b04cf1d754b6d Mon Sep 17 00:00:00 2001 From: "S. Co1" Date: Fri, 1 Mar 2024 12:54:44 -0500 Subject: [PATCH] Add pre-commit hooks --- .gitignore | 5 ++++- .pre-commit-config.yaml | 17 ++++++++++++++++ README.md | 6 +++--- uiget.m | 45 +++++++++++++++++------------------------ 4 files changed, 43 insertions(+), 30 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.gitignore b/.gitignore index 3175821..82e935a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,7 @@ *.asv # OSX / *nix default autosave extension -*.m~ \ No newline at end of file +*.m~ + +# Python env (for pre-commit) +.venv/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..e9fa24e --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,17 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: check-merge-conflict + - id: check-toml + - id: check-yaml + - id: end-of-file-fixer + exclude_types: [binary] + - id: mixed-line-ending + exclude_types: [binary] + - id: trailing-whitespace + - repo: https://github.com/sco1/pre-commit-matlab + rev: v1.2.0 + hooks: + - id: matlab-reflow-comments + args: [--line-length=100] diff --git a/README.md b/README.md index af23b15..4c92b90 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ This utility was inspired by: [Making a dialog where the user can choose either ``` >> file = uiget() -file = +file = "uiget.m" ``` @@ -49,7 +49,7 @@ Selecting a directory with a single output provides the behavior described above ``` >> file = uiget(); Warning: One or more paths have been selected without requesting the path output. -Please specify a second output to uiget to receive these paths. +Please specify a second output to uiget to receive these paths. ``` ### Select a File and Display Full File Specification @@ -104,4 +104,4 @@ User Selected: C:\uiget\uiget.m 1. Extension filter syntax follows that of [uigetfile](https://www.mathworks.com/help/matlab/ref/uigetfile.html), see [the documentation](https://www.mathworks.com/help/matlab/ref/uigetfile.html#mw_d51d3e26-4b0d-4017-a1ed-28162572b6bc) for additional details and examples. -2. The "All Files" file filter is already provided by the dialog window, so explicitly specifying it will create an additional entry. \ No newline at end of file +2. The "All Files" file filter is already provided by the dialog window, so explicitly specifying it will create an additional entry. diff --git a/uiget.m b/uiget.m index 27f8079..989da49 100644 --- a/uiget.m +++ b/uiget.m @@ -22,8 +22,8 @@ % Use current working directory if no inputs are passed basepath = pwd; elseif nargin == 1 - % Check for existence of basepath as a directory, default to current - % directory if it doesn't exist + % Check for existence of basepath as a directory, default to current directory if it doesn't + % exist if ~exist(basepath, 'dir') basepath = pwd; end @@ -42,7 +42,7 @@ % Build file filter if ~isempty(p.Results.ExtensionFilter) extensions = parsefilter(p.Results.ExtensionFilter(:, 1)); - + nfilters = size(p.Results.ExtensionFilter, 1); for ii = 1:nfilters if isempty(extensions{ii}) @@ -52,14 +52,14 @@ jExtensionFilter = javax.swing.filechooser.FileNameExtensionFilter(p.Results.ExtensionFilter{ii, 2}, extensions{ii}); jFC.addChoosableFileFilter(jExtensionFilter) end - + tmp = jFC.getChoosableFileFilters(); jFC.setFileFilter(tmp(2)) end if p.Results.MultiSelect jFC.setMultiSelectionEnabled(true) - + % Change title if default is being used if any(strcmp(p.UsingDefaults, 'Title')) jFC.setDialogTitle('Select File(s) and/or Folder(s)') @@ -72,8 +72,7 @@ returnVal = jFC.showOpenDialog([]); switch returnVal case jFC.APPROVE_OPTION - % Selection string will be empty if getSelectedFiles is used when - % MultiSelect is disabled + % Selection string will be empty if getSelectedFiles is used when MultiSelect is disabled if jFC.isMultiSelectionEnabled selectionStr = string(jFC.getSelectedFiles()); else @@ -98,12 +97,10 @@ for ii = 1:npicked [path(ii), filename, ext] = fileparts(selectionStr(ii)); file(ii) = filename + ext; - - % Because we can select directories, we want to have them output as a - % path and not a file + + % Because we can select directories, we want to have them output as a path and not a file if verLessThan('matlab','9.4') - % string inputs to fullfile were silently added in R2018a, use char - % for R2017a and R2017b + % string inputs to fullfile were silently added in R2018a, use char for R2017a and R2017b tmppath = char(path(ii)); tmpfile = char(file(ii)); if exist(fullfile(tmppath, tmpfile), 'dir') @@ -118,9 +115,8 @@ end end -% Since we've now adjusted file in cases where a folder was selected, warn -% the user if file is going to be empty and they're not requesting path to -% go with it +% Since we've now adjusted file in cases where a folder was selected, warn the user if file is going +% to be empty and they're not requesting path to go with it emptyfiletest = (file == ''); if nargout <= 1 && any(emptyfiletest) warning("uiget:uiget:nopathoutputrequested", ... @@ -131,15 +127,14 @@ % Simplify path output if needed if p.Results.ScalarPathOutput - % Check for number of unique paths - % If more than one is present, use the first & throw a warning + % Check for number of unique paths If more than one is present, use the first & throw a warning uniquepaths = unique(path); nuniquepaths = numel(uniquepaths); if nuniquepaths == 1 path = uniquepaths; elseif nuniquepaths > 1 path = uniquepaths(1); - + warning("uiget:ScalarPathOutput:multipleuniquepaths", ... "Multiple unique paths selected, ignoring %u extra selections.", ... nuniquepaths - 1); @@ -157,8 +152,7 @@ function p = buildParser() % Validate input Name,Value pairs -% Initialize verbosely, since inputParser apparently doesn't have a -% constructor that takes inputs... +% Initialize verbosely, since inputParser apparently doesn't have a constructor that takes inputs p = inputParser(); p.FunctionName = 'uiget'; p.CaseSensitive = false; @@ -174,15 +168,14 @@ end function extensions = parsefilter(incell) -% Parse the extension filter extensions into a format usable by +% Parse the extension filter extensions into a format usable by % javax.swing.filechooser.FileNameExtensionFilter -% -% Since we're keeping with the uigetdir-esque extension syntax -% (e.g. *.extension), we need strip off '*.' from each for compatibility -% with the Java component. +% +% Since we're keeping with the uigetdir-esque extension syntax (e.g. *.extension), we need strip off +% '*.' from each for compatibility with the Java component. extensions = cell(size(incell)); for ii = 1:numel(incell) exp = '\*\.(\w+)'; extensions{ii} = string(regexp(incell{ii}, exp, 'tokens')); end -end \ No newline at end of file +end