gmuth / ipp-client-kotlin

A client implementation of the ipp protocol written in kotlin
MIT License
66 stars 9 forks source link

pageRanges #5

Closed muhammadn closed 2 years ago

muhammadn commented 2 years ago

Hi

I found out pageRanges allows multiple IntRange, for example 1..2,2..3 - for hardcoding, it would be okay but for programmatic use of the library i would have to use List<IntRange> and an IntRange of 1..2,2..3 is not valid.

Is it possible we fix this to use String or List ? Example: String: "1..2,3..4,6..6" or List [1..2,3..4,5..6]

Programmatically 1..2,2..3 is not a valid IntRange

Example code:

     val pages: String? = "1,2,3,4,5"
     val pagesArray = pages?.split(",")
     val range: List<IntRange>? = pagesArray?.map { it.toInt()..it.toInt() }

     ippPrinter.printJob(
       file,
       copies(numCopies),
       IppColorMode.Monochrome,
       pageRanges(range)
     )
gmuth commented 2 years ago

You can define attributes like this IppAttribute("page-ranges", IppTag.RangeOfInteger, listOfIntRange) and use it as parameter for printJob. The template attributes are provided for convenience and ease of use.

muhammadn commented 2 years ago

Hi @gmuth . Thank you and i hope you had a good holiday.

I've tried above before i opened an issue but IppTemplateAttributes.kt function arguments relies on IppTag.kt so,

    @JvmStatic
    fun pageRanges(vararg range: List<IntRange>) =
            IppAttribute("page-ranges", IppTag.RangeOfInteger, range)

the code above won't work because IppTag only validates it as IntRange and not List<IntRange>

I can also try using String as an argument with eg "1..2,2..3,10..15" like below:

    @JvmStatic
    fun pageRanges(vararg range: String) =
            IppAttribute("page-ranges", IppTag.RangeOfInteger, listOf(range))

IppTag will cause an error because it does not match IntRange

code: https://github.com/gmuth/ipp-client-kotlin/blob/master/src/main/kotlin/de/gmuth/ipp/core/IppTag.kt#L47

If i modify IppTag which is a low level interaction, it will break while trying to communicate with Ipp

gmuth commented 2 years ago

I understand your issue and have provided another method that supports Collection<IntRange>.

Your first try is wrong because you really want to eliminate vararg completley. Your second try does not work because you again use vararg, but in fact really want just one String and also because you don't convert the string to the required type IntRange and therefore run into class cast exceptions (or failed checks I've implemented for better error handling).

pageRanges("1,3,4,10".toListOfIntRange())

now works together with extension

fun String.toListOfIntRange() = split(",").map { it.toInt()..it.toInt() }