11import { window } from 'vscode'
22import { Title } from './title'
33import { isValidStringInteger } from '../util/number'
4+ import { getIsoDate , isFreeTextDate } from '../util/date'
45
5- export const getInput = ( title : Title , value ?: string ) =>
6+ export const getInput = (
7+ title : Title ,
8+ value ?: string
9+ ) : Thenable < string | undefined > =>
610 window . showInputBox ( {
711 title,
812 value
913 } )
1014
11- export const getValidInput = (
15+ const getValidInput = (
1216 title : Title ,
1317 validateInput : ( text ?: string ) => null | string ,
1418 options ?: { prompt ?: string ; value ?: string }
15- ) =>
19+ ) : Thenable < string | undefined > =>
1620 window . showInputBox ( {
1721 prompt : options ?. prompt ,
1822 title,
1923 validateInput,
2024 value : options ?. value
2125 } )
2226
27+ const isPositiveInteger = (
28+ input : string | undefined ,
29+ includeZero : boolean | undefined
30+ ) : boolean => {
31+ if ( ! isValidStringInteger ( input ) ) {
32+ return false
33+ }
34+
35+ const number = Number ( input )
36+
37+ if ( ! includeZero ) {
38+ return number > 0
39+ }
40+ return number >= 0
41+ }
42+
2343export const getPositiveIntegerInput = async (
2444 title : Title ,
25- options : { prompt : string ; value : string }
26- ) => {
45+ options : { prompt : string ; value : string } ,
46+ includeZero ?: boolean
47+ ) : Promise < string | undefined > => {
2748 const input = await getValidInput (
2849 title ,
29- val => {
30- if ( isValidStringInteger ( val ) && Number ( val ) > 0 ) {
50+ input => {
51+ if ( isPositiveInteger ( input , includeZero ) ) {
3152 return ''
3253 }
3354
34- return 'Input needs to be a positive integer'
55+ return `please enter a positive integer${ includeZero ? ' or 0' : '' } `
3556 } ,
3657 options
3758 )
@@ -41,3 +62,13 @@ export const getPositiveIntegerInput = async (
4162 }
4263 return Number . parseInt ( input ) . toString ( )
4364}
65+
66+ export const getValidDateInput = ( title : Title ) : Thenable < string | undefined > =>
67+ getValidInput (
68+ title ,
69+ ( text ?: string ) : null | string =>
70+ isFreeTextDate ( text )
71+ ? null
72+ : 'please enter a valid date of the form yyyy-mm-dd' ,
73+ { value : getIsoDate ( ) }
74+ )
0 commit comments