forked from vieter-v/vieter
				
			feat(console): tabled outputs now optionally return without decorations
							parent
							
								
									cf67b46df0
								
							
						
					
					
						commit
						8a08788935
					
				|  | @ -5,6 +5,11 @@ import strings | ||||||
| import cli | import cli | ||||||
| import os | import os | ||||||
| 
 | 
 | ||||||
|  | // tabbed_table returns a simple textual table, with tabs as separators. | ||||||
|  | pub fn tabbed_table(data [][]string) string { | ||||||
|  | 	return data.map(it.join('\t')).join('\n') | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // pretty_table converts a list of string data into a pretty table. Many thanks | // pretty_table converts a list of string data into a pretty table. Many thanks | ||||||
| // to @hungrybluedev in the Vlang Discord for providing this code! | // to @hungrybluedev in the Vlang Discord for providing this code! | ||||||
| // https://ptb.discord.com/channels/592103645835821068/592106336838352923/970278787143045192 | // https://ptb.discord.com/channels/592103645835821068/592106336838352923/970278787143045192 | ||||||
|  |  | ||||||
|  | @ -133,7 +133,9 @@ pub fn cmd() cli.Command { | ||||||
| 						] | 						] | ||||||
| 					} | 					} | ||||||
| 
 | 
 | ||||||
| 					list(conf, filter)? | 					raw := cmd.flags.get_bool('raw')? | ||||||
|  | 
 | ||||||
|  | 					list(conf, filter, raw)? | ||||||
| 				} | 				} | ||||||
| 			}, | 			}, | ||||||
| 			cli.Command{ | 			cli.Command{ | ||||||
|  | @ -167,27 +169,31 @@ pub fn cmd() cli.Command { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // print_log_list prints a list of logs. | // print_log_list prints a list of logs. | ||||||
| fn print_log_list(logs []BuildLog) ? { | fn print_log_list(logs []BuildLog, raw bool) ? { | ||||||
| 	data := logs.map([it.id.str(), it.target_id.str(), it.start_time.local().str(), | 	data := logs.map([it.id.str(), it.target_id.str(), it.start_time.local().str(), | ||||||
| 		it.exit_code.str()]) | 		it.exit_code.str()]) | ||||||
| 
 | 
 | ||||||
|  | 	if raw { | ||||||
|  | 		println(console.tabbed_table(data)) | ||||||
|  | 	} else { | ||||||
| 		println(console.pretty_table(['id', 'target', 'start time', 'exit code'], data)?) | 		println(console.pretty_table(['id', 'target', 'start time', 'exit code'], data)?) | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // list prints a list of all build logs. | // list prints a list of all build logs. | ||||||
| fn list(conf Config, filter BuildLogFilter) ? { | fn list(conf Config, filter BuildLogFilter, raw bool) ? { | ||||||
| 	c := client.new(conf.address, conf.api_key) | 	c := client.new(conf.address, conf.api_key) | ||||||
| 	logs := c.get_build_logs(filter)?.data | 	logs := c.get_build_logs(filter)?.data | ||||||
| 
 | 
 | ||||||
| 	print_log_list(logs)? | 	print_log_list(logs, raw)? | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // list prints a list of all build logs for a given target. | // list prints a list of all build logs for a given target. | ||||||
| fn list_for_target(conf Config, target_id int) ? { | fn list_for_target(conf Config, target_id int, raw bool) ? { | ||||||
| 	c := client.new(conf.address, conf.api_key) | 	c := client.new(conf.address, conf.api_key) | ||||||
| 	logs := c.get_build_logs_for_target(target_id)?.data | 	logs := c.get_build_logs_for_target(target_id)?.data | ||||||
| 
 | 
 | ||||||
| 	print_log_list(logs)? | 	print_log_list(logs, raw)? | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // info print the detailed info for a given build log. | // info print the detailed info for a given build log. | ||||||
|  |  | ||||||
|  | @ -60,7 +60,9 @@ pub fn cmd() cli.Command { | ||||||
| 						filter.repo = repo | 						filter.repo = repo | ||||||
| 					} | 					} | ||||||
| 
 | 
 | ||||||
| 					list(conf, filter)? | 					raw := cmd.flags.get_bool('raw')? | ||||||
|  | 
 | ||||||
|  | 					list(conf, filter, raw)? | ||||||
| 				} | 				} | ||||||
| 			}, | 			}, | ||||||
| 			cli.Command{ | 			cli.Command{ | ||||||
|  | @ -193,12 +195,16 @@ pub fn cmd() cli.Command { | ||||||
| // ID. If multiple or none are found, an error is raised. | // ID. If multiple or none are found, an error is raised. | ||||||
| 
 | 
 | ||||||
| // list prints out a list of all repositories. | // list prints out a list of all repositories. | ||||||
| fn list(conf Config, filter TargetFilter) ? { | fn list(conf Config, filter TargetFilter, raw bool) ? { | ||||||
| 	c := client.new(conf.address, conf.api_key) | 	c := client.new(conf.address, conf.api_key) | ||||||
| 	repos := c.get_targets(filter)? | 	repos := c.get_targets(filter)? | ||||||
| 	data := repos.map([it.id.str(), it.kind, it.url, it.repo]) | 	data := repos.map([it.id.str(), it.kind, it.url, it.repo]) | ||||||
| 
 | 
 | ||||||
|  | 	if raw { | ||||||
|  | 		println(console.tabbed_table(data)) | ||||||
|  | 	} else { | ||||||
| 		println(console.pretty_table(['id', 'kind', 'url', 'repo'], data)?) | 		println(console.pretty_table(['id', 'kind', 'url', 'repo'], data)?) | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // add adds a new repository to the server's list. | // add adds a new repository to the server's list. | ||||||
|  |  | ||||||
|  | @ -24,6 +24,13 @@ fn main() { | ||||||
| 				global: true | 				global: true | ||||||
| 				default_value: [os.expand_tilde_to_home('~/.vieterrc')] | 				default_value: [os.expand_tilde_to_home('~/.vieterrc')] | ||||||
| 			}, | 			}, | ||||||
|  | 			cli.Flag{ | ||||||
|  | 				flag: cli.FlagType.bool | ||||||
|  | 				name: 'raw' | ||||||
|  | 				abbrev: 'r' | ||||||
|  | 				description: 'Only output minimal information (no formatted tables, etc.)' | ||||||
|  | 				global: true | ||||||
|  | 			}, | ||||||
| 		] | 		] | ||||||
| 		commands: [ | 		commands: [ | ||||||
| 			server.cmd(), | 			server.cmd(), | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue