3
3
[ PowerCommandTest ( tests : "exit|start --docs" ) ]
4
4
[ PowerCommandDesign ( description : "With help command you will be shown the provided description or online documentation of the command or a PowerCommand feature." ,
5
5
arguments : "<command name or feature you are interested of knowing more>" ,
6
- options : "docs|clear " ,
6
+ options : "new|all " ,
7
7
disableProxyOutput : true ,
8
8
example : "describe exit|describe cls|describe log|//Open documentation about options (if any)|describe options --doc" ) ]
9
9
public class DescribeCommand ( string identifier , CommandsConfiguration configuration ) : CommandBase < CommandsConfiguration > ( identifier , configuration )
10
10
{
11
+ protected static List < Doc > Items = new ( ) ;
12
+ protected static Doc SelectedItem = new ( ) ;
11
13
public override RunResult Run ( )
12
14
{
13
- if ( Input . HasOption ( "docs" ) ) ShowDoc ( ) ;
14
- else ShowCommand ( ) ;
15
+ if ( HasOption ( "new" ) )
16
+ {
17
+ SelectedItem = new Doc ( ) ;
18
+ AddOrEdit ( ) ;
19
+ return Ok ( ) ;
20
+ }
21
+ if ( HasOption ( "all" ) )
22
+ {
23
+ Items = StorageService < DocsDB > . Service . GetObject ( ) . Docs ;
24
+ ShowResult ( "All created descriptions..." ) ;
25
+ if ( Items . Count == 0 ) WriteLine ( "No description documents added..." ) ;
26
+ return Ok ( ) ;
27
+ }
28
+ ShowDoc ( ) ;
15
29
return Ok ( ) ;
16
30
}
17
-
18
31
public void ShowDoc ( )
19
32
{
20
- var docSearch = Input . HasOption ( "docs" ) ? Input . GetOptionValue ( "docs" ) . ToLower ( ) : Input . SingleArgument . ToLower ( ) ;
33
+ var docSearch = Input . SingleArgument . ToLower ( ) ;
21
34
var docs = StorageService < DocsDB > . Service . GetObject ( ) . Docs ;
22
35
var matchDocs = docs . Where ( d => d . DocID . ToString ( ) . PadLeft ( 4 , '0' ) == docSearch || d . Name . ToLower ( ) . Contains ( docSearch ) || d . Tags . ToLower ( ) . Contains ( docSearch ) ) . ToArray ( ) ;
23
- if ( matchDocs . Length == 1 || matchDocs . Select ( d => d . DocID ) . Distinct ( ) . Count ( ) == 1 )
36
+ if ( Input . Arguments . Length > 1 )
24
37
{
25
- ShellService . Service . OpenWithDefaultProgram ( matchDocs . First ( ) . Uri ) ;
26
- return ;
38
+ var arguments = Input . Arguments . ToList ( ) ;
39
+ //Add filters as many as the user have given, but limit the filter count to 10, higher value must some kind of abuse.
40
+ var iterations = arguments . Count < 100 ? arguments . Count : 10 ;
41
+ for ( var i = 1 ; i < iterations ; i ++ ) matchDocs = matchDocs . Where ( d => d . Name . ToLower ( ) . Contains ( arguments [ i ] . ToLower ( ) ) || d . Tags . ToLower ( ) . Contains ( arguments [ i ] . ToLower ( ) ) ) . ToArray ( ) ; ;
27
42
}
28
- if ( matchDocs . Length > 1 )
43
+ if ( matchDocs . Length > 0 )
29
44
{
30
- WriteHeadLine ( $ "Found { matchDocs . Length } number of documents, you can use the docID the for digit number to the right to choose document to show.") ;
31
- foreach ( var matchDoc in matchDocs )
32
- {
33
- WriteLine ( $ "{ matchDoc . DocID . ToString ( ) . PadLeft ( 4 , '0' ) } { matchDoc . Name } { matchDoc . Uri . Split ( '/' ) . Last ( ) } { matchDoc . Tags } ") ;
34
- }
45
+ Items = matchDocs . ToList ( ) ;
46
+ if ( ShowResult ( docSearch ) ) ShellService . Service . OpenWithDefaultProgram ( matchDocs . First ( ) . Uri ) ;
35
47
return ;
36
48
}
37
- WriteHeadLine ( "Could not find any command or documentation to describe" ) ;
38
- WriteHeadLine ( "Documentation " ) ;
39
- foreach ( var doc in docs ) WriteLine ( $ " { doc . Name } { doc . Uri . Split ( '/' ) . Last ( ) } { doc . Tags } " ) ;
49
+ if ( ShowCommand ( ) ) return ;
50
+ WriteHeadLine ( "Could not find any command or documentation to describe, configured AI services will try to get you an answer. " ) ;
51
+ ShellService . Service . OpenWithDefaultProgram ( Configuration . DefaultAIBotUri . Replace ( ConfigurationGlobals . QueryPlaceholder , string . Join ( " " , Input . Arguments ) ) ) ;
40
52
}
41
-
42
- private void ShowCommand ( )
53
+ private bool ShowCommand ( )
43
54
{
44
55
var commandIdentifier = string . IsNullOrEmpty ( Input . SingleArgument ) ? "describe" : Input . SingleArgument ;
45
56
var command = IPowerCommandsRuntime . DefaultInstance ? . Commands . FirstOrDefault ( c => c . Identifier == commandIdentifier ) ;
46
57
if ( command == null )
47
58
{
48
59
if ( Input . Identifier != nameof ( DescribeCommand ) . ToLower ( ) . Replace ( "command" , "" ) ) WriteLine ( $ "Command with identifier:{ Input . Identifier } not found") ;
49
- ShowDoc ( ) ;
50
- return ;
60
+ return false ;
51
61
}
52
- HelpService . Service . ShowHelp ( command , Input . HasOption ( "clear" ) ) ;
62
+ HelpService . Service . ShowHelp ( command , clearConsole : true ) ;
53
63
Console . WriteLine ( ) ;
64
+ return true ;
65
+ }
66
+ protected bool ShowResult ( string headLine )
67
+ {
68
+ var selected = ListService . ListDialog ( $ "{ headLine } \n Search phrase(s): { Input . Raw . Replace ( "find " , "" ) } ", Items . Select ( i => $ "{ i . Name } { i . Uri } { i . Tags } ") . ToList ( ) ) ;
69
+ if ( selected . Count == 0 ) return false ;
70
+
71
+ SelectedItem = Items [ selected . Keys . First ( ) ] ;
72
+ var navigateOption = ToolbarService . NavigateToolbar < DescribeDialogAlternatives > ( ) ;
73
+ if ( navigateOption == DescribeDialogAlternatives . Continue ) return false ;
74
+ if ( navigateOption == DescribeDialogAlternatives . Open ) return OpenDocument ( ) ;
75
+ if ( navigateOption == DescribeDialogAlternatives . Edit ) return AddOrEdit ( ) ;
76
+ if ( navigateOption == DescribeDialogAlternatives . Delete ) return Delete ( ) ;
77
+ if ( navigateOption == DescribeDialogAlternatives . CreateMarkdownFile ) return CreateMarkdownFile ( SelectedItem . Name ) ;
78
+ if ( navigateOption == DescribeDialogAlternatives . UseYourAIService ) return OpenAIService ( ) ;
79
+ return false ;
80
+ }
81
+ public bool OpenAIService ( )
82
+ {
83
+ var arguments = new List < string > { $ "{ SelectedItem . Name } " } ;
84
+ arguments . AddRange ( SelectedItem . Tags . Split ( "," ) ) ;
85
+ ShellService . Service . OpenWithDefaultProgram ( Configuration . DefaultAIBotUri . Replace ( ConfigurationGlobals . QueryPlaceholder , string . Join ( " " , arguments ) ) ) ;
86
+ return true ;
87
+ }
88
+ public bool OpenDocument ( )
89
+ {
90
+ ShellService . Service . OpenWithDefaultProgram ( SelectedItem . Uri ) ;
91
+ WriteHeadLine ( SelectedItem . Name ) ;
92
+ WriteCodeExample ( "Tags:" , SelectedItem . Tags ) ;
93
+ WriteCodeExample ( "Uri:" , SelectedItem . Uri ) ;
94
+ WriteCodeExample ( "Last updated:" , SelectedItem . Updated . ToShortDateString ( ) ) ;
95
+ return true ;
96
+ }
97
+ public bool AddOrEdit ( )
98
+ {
99
+ if ( string . IsNullOrEmpty ( SelectedItem . Name ) ) SelectedItem . Name = string . IsNullOrEmpty ( Input . SingleArgument ) ? DialogService . QuestionAnswerDialog ( "Name your decription:" ) : Input . SingleArgument ;
100
+ SelectedItem . Tags = DialogService . QuestionAnswerDialog ( "Add tags separated with ," ) ;
101
+ var attachFile = DialogService . YesNoDialog ( "Attach a file? (or just add a uri in next step)" ) ;
102
+ if ( attachFile )
103
+ {
104
+ var selectedFile = DialogService . SelectFileDialog ( Environment . CurrentDirectory , onlyTextFiles : true ) ;
105
+ if ( ! string . IsNullOrEmpty ( selectedFile ) )
106
+ {
107
+ var targetDirPath = Path . Combine ( ConfigurationGlobals . ApplicationDataFolder , ConfigurationGlobals . DocsDirectoryName ) ;
108
+ var selectedFileInfo = new FileInfo ( selectedFile ) ;
109
+ if ( ! Directory . Exists ( targetDirPath ) ) Directory . CreateDirectory ( targetDirPath ) ;
110
+ if ( File . Exists ( Path . Combine ( Environment . CurrentDirectory , selectedFile ) ) && ! File . Exists ( Path . Combine ( targetDirPath , selectedFileInfo . Name ) ) )
111
+ {
112
+ var sourceFile = Path . Combine ( Environment . CurrentDirectory , selectedFile ) ;
113
+ var destinationFile = Path . Combine ( targetDirPath , selectedFileInfo . Name ) ;
114
+ File . Copy ( sourceFile , destinationFile ) ;
115
+ WriteSuccessLine ( $ "File [{ sourceFile } ] copied to [{ destinationFile } ].") ;
116
+ }
117
+ SelectedItem . Uri = $ "{ ConfigurationGlobals . RoamingDirectoryPlaceholder } \\ { ConfigurationGlobals . DocsDirectoryName } \\ { selectedFileInfo . Name } ";
118
+ }
119
+ }
120
+ else SelectedItem . Uri = DialogService . QuestionAnswerDialog ( "Add uri (could be left blank)" ) ;
121
+ var db = StorageService < DocsDB > . Service . GetObject ( ) ;
122
+ if ( SelectedItem . DocID > - 1 )
123
+ {
124
+ var existingDoc = db . Docs . FirstOrDefault ( d => d . DocID == SelectedItem . DocID ) ;
125
+ if ( existingDoc != null ) db . Docs . Remove ( existingDoc ) ;
126
+ }
127
+ else
128
+ {
129
+ if ( db . Docs . Count == 0 ) SelectedItem . DocID = 1 ;
130
+ else SelectedItem . DocID = db . Docs . Max ( d => d . DocID ) + 1 ;
131
+ if ( string . IsNullOrEmpty ( SelectedItem . Uri ) ) if ( DialogService . YesNoDialog ( "Do you want to add a markdown file to this description?" ) ) CreateMarkdownFile ( SelectedItem . Name ) ;
132
+ }
133
+ db . Docs . Add ( SelectedItem ) ;
134
+ StorageService < DocsDB > . Service . StoreObject ( db ) ;
135
+ WriteSuccessLine ( $ "\n Document [{ SelectedItem . Name } ] saved.") ;
136
+ return true ;
137
+ }
138
+ public bool Delete ( )
139
+ {
140
+ var db = StorageService < DocsDB > . Service . GetObject ( ) ;
141
+ var existingDoc = db . Docs . FirstOrDefault ( d => d . DocID == SelectedItem . DocID ) ;
142
+ if ( existingDoc == null ) return false ;
143
+ var confirmDeletion = DialogService . YesNoDialog ( $ "Delete [{ SelectedItem . Name } ] are you sure?") ;
144
+ if ( ! confirmDeletion ) return false ;
145
+ db . Docs . Remove ( existingDoc ) ;
146
+ StorageService < DocsDB > . Service . StoreObject ( db ) ;
147
+ WriteSuccessLine ( $ "\n Document [{ SelectedItem . Name } ] deleted.") ;
148
+ if ( SelectedItem . Uri . StartsWith ( ConfigurationGlobals . RoamingDirectoryPlaceholder ) )
149
+ {
150
+ var localFileExist = db . Docs . Any ( d => d . Uri == SelectedItem . Uri ) ;
151
+ if ( ! localFileExist )
152
+ {
153
+ var localFilePath = SelectedItem . Uri . Replace ( ConfigurationGlobals . RoamingDirectoryPlaceholder , ConfigurationGlobals . ApplicationDataFolder ) ;
154
+ if ( File . Exists ( localFilePath ) ) File . Delete ( localFilePath ) ;
155
+ WriteSuccessLine ( $ "\n File [{ localFilePath } ] deleted.") ;
156
+ }
157
+ }
158
+ return true ;
159
+ }
160
+ public bool CreateMarkdownFile ( string name )
161
+ {
162
+ var targetDirPath = Path . Combine ( ConfigurationGlobals . ApplicationDataFolder , ConfigurationGlobals . DocsDirectoryName ) ;
163
+ var fileName = Path . Combine ( targetDirPath , $ "{ name } .md") ;
164
+ var markdownEditor = new MarkdownEditorManager ( fileName , ConsoleService . Service , Configuration . Prompt ) ;
165
+ markdownEditor . Run ( ) ;
166
+ SelectedItem . Uri = $ "{ ConfigurationGlobals . RoamingDirectoryPlaceholder } \\ { ConfigurationGlobals . DocsDirectoryName } \\ { name } .md";
167
+
168
+ var db = StorageService < DocsDB > . Service . GetObject ( ) ;
169
+ var existingDoc = db . Docs . FirstOrDefault ( d => d . DocID == SelectedItem . DocID ) ;
170
+ ShellService . Service . OpenWithDefaultProgram ( fileName ) ;
171
+ if ( existingDoc == null ) return false ;
172
+ db . Docs . Remove ( existingDoc ) ;
173
+ db . Docs . Add ( SelectedItem ) ;
174
+ StorageService < DocsDB > . Service . StoreObject ( db ) ;
175
+ WriteSuccessLine ( $ "\n Document [{ fileName } ] created.") ;
176
+ return true ;
54
177
}
55
178
}
56
179
}
0 commit comments