Skip to content

Commit

Permalink
refactor list to separate command
Browse files Browse the repository at this point in the history
  • Loading branch information
yeastplume committed Jun 4, 2020
1 parent 907018a commit beefb51
Show file tree
Hide file tree
Showing 18 changed files with 266 additions and 115 deletions.
27 changes: 16 additions & 11 deletions src/bin/aloevera.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: aloevera
about: Graphic Asset Pipeline for the Commander X1
about: Graphic Asset Pipeline for the Commander X16
author: Revcore Technologies Ltd.

args:
Expand Down Expand Up @@ -84,8 +84,6 @@ subcommands:
- input_file:
help: Input PNG file name
index: 2
- list:
about: List palettes in a project file
- imageset:
about: Import and manipulate image sets
subcommands:
Expand Down Expand Up @@ -116,8 +114,6 @@ subcommands:
- pixel_depth:
help: Target Pixel Depth (8, 4, 2 or 1)
index: 3
- list:
about: List image sets in a project file
- tilemap:
about: Import and manipulate tilemaps
subcommands:
Expand Down Expand Up @@ -187,8 +183,6 @@ subcommands:
short: c
takes_value: true
default_value: "0"
- list:
about: List tilemaps in a project file
- sprite:
about: Interpret an existing Imageset as a Sprite
subcommands:
Expand All @@ -201,8 +195,6 @@ subcommands:
- imageset_id:
help: ID of the existing, formatted Imageset to use as a Sprite
index: 2
- list:
about: List sprites in a project file
- bitmap:
about: Interpret an existing Imageset as a Bitmap
subcommands:
Expand All @@ -215,5 +207,18 @@ subcommands:
- imageset_id:
help: ID of the existing, formatted Imageset to use as a Bitmap
index: 2
- list:
about: List bitmaps in a project file
- list:
about: List elements in a project file
args:
- object_type:
help: Type of elements to view
possible_values:
- all
- palettes
- imagesets
- tilemaps
- sprites
- bitmaps
default_value: "all"
index: 1

14 changes: 0 additions & 14 deletions src/cmd/bitmap/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,3 @@ pub fn bitmap_init(g_args: &GlobalArgs, args: &BitmapInitArgs) -> Result<(), Err

Ok(())
}

pub fn bitmap_list(g_args: &GlobalArgs) -> Result<(), Error> {
let proj = common::load_project(g_args.project_file.clone())?;
println!("Bitmaps:");
for (id, bitmap) in proj.bitmaps {
println!(
" {}: width {} depth {}",
id,
bitmap.width.val_as_u32(),
bitmap.depth
);
}
Ok(())
}
1 change: 0 additions & 1 deletion src/cmd/bitmap/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub fn execute_bitmap_command(g_args: &GlobalArgs, args: &ArgMatches) -> Result<
let a = arg_parse!(parse_bitmap_init_args(g_args, args));
command::bitmap_init(g_args, &a)
}
("list", Some(_)) => command::bitmap_list(g_args),
_ => {
let msg = format!("Unknown sub command, use 'aloevera bitmap --help' for details");
return Err(ErrorKind::ArgumentError(msg).into());
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use clap::ArgMatches;

use crate::cmd::common::{self, GlobalArgs};
use crate::cmd::{asm, bitmap, create, imageset, palette, sprite, tilemap};
use crate::cmd::{asm, bitmap, create, imageset, list, palette, sprite, tilemap};
use crate::{Error, ErrorKind};

fn parse_and_execute(g_args: &GlobalArgs, args: &ArgMatches) -> Result<(), Error> {
Expand All @@ -27,6 +27,7 @@ fn parse_and_execute(g_args: &GlobalArgs, args: &ArgMatches) -> Result<(), Error
("bitmap", Some(args)) => bitmap::parse::execute_bitmap_command(g_args, &args),
("imageset", Some(args)) => imageset::parse::execute_imageset_command(g_args, &args),
("tilemap", Some(args)) => tilemap::parse::execute_tilemap_command(g_args, &args),
("list", Some(args)) => list::parse::execute_list_command(g_args, &args),
_ => {
let msg = format!("Unknown command, use 'aloevera --help' for details");
return Err(ErrorKind::ArgumentError(msg).into());
Expand Down
26 changes: 0 additions & 26 deletions src/cmd/imageset/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,3 @@ pub fn imageset_format(g_args: &GlobalArgs, args: &ImageSetFormatArgs) -> Result

Ok(())
}

/// Imageset list
pub fn imageset_list(g_args: &GlobalArgs) -> Result<(), Error> {
let proj = common::load_project(g_args.project_file.clone())?;
println!("Image sets:");
for (id, imageset) in proj.imagesets {
match imageset.depth {
None => println!(
" {}: {} {}x{} frames",
id,
imageset.frame_data.len(),
imageset.frame_width,
imageset.frame_height
),
pixel_depth => print!(
" {}: {} {}x{} frames depth {}",
id,
imageset.frame_data.len(),
imageset.frame_width,
imageset.frame_height,
pixel_depth.unwrap()
),
}
}
Ok(())
}
1 change: 0 additions & 1 deletion src/cmd/imageset/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ pub fn execute_imageset_command(g_args: &GlobalArgs, args: &ArgMatches) -> Resul
let a = arg_parse!(parse_imageset_format_args(g_args, args));
command::imageset_format(g_args, &a)
}
("list", Some(_)) => command::imageset_list(g_args),
_ => {
let msg = format!("Unknown sub command, use 'aloevera imageset --help' for details");
return Err(ErrorKind::ArgumentError(msg).into());
Expand Down
184 changes: 184 additions & 0 deletions src/cmd/list/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// Copyright 2020 Revcore Technologies Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::convert::TryFrom;

use crate::{Error, ErrorKind};
use proj::{AloeVeraProject, Binable};

use crate::cmd::common::{self, GlobalArgs};

/// Arguments for palette command
pub struct ListArgs {
pub object_type: ListObjectType,
}

#[derive(Debug)]
/// Supported Elements to List
pub enum ListObjectType {
All,
Palettes,
Imagesets,
Tilemaps,
Sprites,
Bitmaps,
}

impl TryFrom<&str> for ListObjectType {
type Error = Error;
fn try_from(input: &str) -> Result<Self, Self::Error> {
let res = match input {
"all" => ListObjectType::All,
"palettes" => ListObjectType::Palettes,
"imagesets" => ListObjectType::Imagesets,
"tilemaps" => ListObjectType::Tilemaps,
"sprites" => ListObjectType::Sprites,
"bitmaps" => ListObjectType::Bitmaps,
n => {
return Err(ErrorKind::ArgumentError(format!("Invalid object type: {}", n)).into())
}
};
Ok(res)
}
}

/// Project file list command
pub fn list(g_args: &GlobalArgs, args: &ListArgs) -> Result<(), Error> {
// load up the project json
let project_file = match &g_args.project_file {
Some(f) => f,
None => {
return Err(ErrorKind::ArgumentError("Missing project file name".to_string()).into())
}
};
let encoded = common::read_file_bin(&project_file)?;
let proj = *AloeVeraProject::from_bin(&encoded)?;
println!("Elements in {}", project_file);
println!("--------------");

match args.object_type {
ListObjectType::All => {
list_palettes(&proj)?;
list_imagesets(&proj)?;
list_tilemaps(&proj)?;
list_sprites(&proj)?;
list_bitmaps(&proj)?;
}
ListObjectType::Palettes => {
list_palettes(&proj)?;
}
ListObjectType::Imagesets => {
list_imagesets(&proj)?;
}
ListObjectType::Tilemaps => {
list_tilemaps(&proj)?;
}
ListObjectType::Sprites => {
list_sprites(&proj)?;
}
ListObjectType::Bitmaps => {
list_bitmaps(&proj)?;
}
}
Ok(())
}

/// Palette list
fn list_palettes(proj: &AloeVeraProject) -> Result<(), Error> {
println!("Palettes:");
for (id, palette) in proj.palettes.iter() {
println!(" {}:", id);
println!(" Color Count: {}", palette.len());
}
Ok(())
}

/// Imageset list
pub fn list_imagesets(proj: &AloeVeraProject) -> Result<(), Error> {
println!("Imagesets:");
for (id, imageset) in proj.imagesets.iter() {
let depth = match imageset.depth {
Some(d) => format!("{}", d),
None => "Unformatted".to_owned(),
};
println!(" {}:", id);
println!(" Frame Count: {}", imageset.frame_data.len());
println!(
" Frame Size: {}w x {}h",
imageset.frame_width, imageset.frame_height,
);
println!(" Pixel Depth: {}", depth,);
}
Ok(())
}

/// Tilemap list
pub fn list_tilemaps(proj: &AloeVeraProject) -> Result<(), Error> {
println!("Tilemaps:");
for (id, tilemap) in proj.tilemaps.iter() {
println!(" {}:", id);
println!(" Using Imageset: {}", tilemap.imageset_id,);
println!(
" Map Size: {}x{} Tiles",
tilemap.map_width(),
tilemap.map_height(),
);
println!(
" Tile Size: {}w x{}h",
tilemap.tile_width(),
tilemap.tile_height(),
);
println!(" Mode: {}", tilemap.mode,);
}
Ok(())
}

/// Sprite list
pub fn list_sprites(proj: &AloeVeraProject) -> Result<(), Error> {
println!("Sprites:");
for (id, sprite) in proj.sprites.iter() {
let imageset = match proj.imagesets.get(&sprite.imageset_id) {
Some(i) => i,
None => {
let msg = format!(
"Imageset with id {} needed by sprite {} does not exist in project file.",
sprite.id, sprite.imageset_id
);
return Err(ErrorKind::ArgumentError(msg).into());
}
};
println!(" {}:", id);
println!(" Using Imageset: {}", sprite.imageset_id,);
println!(" Frame Count: {}", imageset.frame_data.len(),);
println!(
" Frame Size: {}w x {}h",
sprite.frame_width.val_as_u32(),
sprite.frame_height.val_as_u32(),
);
println!(" Pixel Depth: {}", sprite.depth,);
}
Ok(())
}

/// Bitmap list
pub fn list_bitmaps(proj: &AloeVeraProject) -> Result<(), Error> {
println!("Bitmaps:");
for (id, bitmap) in proj.bitmaps.iter() {
println!(" {}:", id);
println!(" Using Imageset: {}", bitmap.imageset_id,);
println!(" Width: {}", bitmap.width.val_as_u32(),);
println!(" Pixel Depth: {}", bitmap.depth,);
}
Ok(())
}
16 changes: 16 additions & 0 deletions src/cmd/list/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2020 Revcore Technologies Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod command;
pub mod parse;
37 changes: 37 additions & 0 deletions src/cmd/list/parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2020 Revcore Technologies Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::convert::TryFrom;

use clap::ArgMatches;

use super::command::{self, ListArgs, ListObjectType};
use crate::cmd::common::GlobalArgs;
use crate::{Error, ErrorKind};

pub fn parse_list_args(g_args: &GlobalArgs, args: &ArgMatches) -> Result<ListArgs, Error> {
if g_args.project_file.is_none() {
let msg = format!("--project_file is required in this context");
return Err(ErrorKind::ArgumentError(msg).into());
}
let object_type = match args.value_of("object_type") {
Some(v) => ListObjectType::try_from(v)?,
None => ListObjectType::All,
};
Ok(ListArgs { object_type })
}

pub fn execute_list_command(g_args: &GlobalArgs, args: &ArgMatches) -> Result<(), Error> {
let a = arg_parse!(parse_list_args(g_args, args));
command::list(g_args, &a)
}
1 change: 1 addition & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod asm;
pub mod bitmap;
pub mod create;
pub mod imageset;
pub mod list;
pub mod palette;
pub mod sprite;
pub mod tilemap;
Expand Down
Loading

0 comments on commit beefb51

Please sign in to comment.