reorder

Un outil pour réarranger les lignes de stdin - retour accueil

git clone git://bebou.netlib.re/reorder
Log | Files | Refs | README |

commit 831b3574e59ad885ee85cb9d2bf64b92326fb733
parent 1389c2d50d6df2c61744a8eda828155e32ed865b
Auteurice: Arthur Pons <arthur.pons@unistra.fr>
Date:   Fri, 17 Jan 2025 15:04:32 +0100

Ligne courante diff pile + facto mvmt curseur

J'aurais du en faire deux commits

Dorénavant chaque pile a sa propre ligne courante, on ne revient pas à
chaque fois à la find de la pile en passant de l'un à l'autre. Bug
connu, quand on bouge une carte d'une pile à une autre ça bug et on se
retrouve à avoir sa sélection sur une autre carte que celle
sélectionnée.

Factorisation du mouvement du curseur dans les piles.

Diffstat:
Mreorder.c | 77++++++++++++++++++++++++++++++-----------------------------------------------
1 file changed, 30 insertions(+), 47 deletions(-)

diff --git a/reorder.c b/reorder.c @@ -13,6 +13,7 @@ struct pile { int cardCount; char **cards; + int curline; }; void addcardtopile(char* cardname, struct pile* p) { @@ -28,10 +29,26 @@ void removecardofpile(int cardposition, struct pile* p) { } } +void moveselection(struct pile* p, char* direction, bool selection) { + int step; + if (direction == "up") { step = -1; } + else { step = 1; } + if (p->curline+step >= 0 && p->curline+step < p->cardCount) { + if (selection) { + char* tmp; + tmp=p->cards[p->curline]; + p->cards[p->curline]=p->cards[p->curline+step]; + p->cards[p->curline+step]=tmp; + } + p->curline=p->curline+step; + } +} + struct pile file2pile(FILE *fp) { struct pile pi; struct pile *p=&pi; p->cardCount=0; + p->curline=0; p->cards = malloc(LIST_SIZE); char line[CARD_NAME_SIZE]; while (fgets(line, sizeof(line), fp)) { @@ -40,11 +57,11 @@ struct pile file2pile(FILE *fp) { return pi; } -void display(struct pile* h, struct pile* p, int curline, char* curmenu,bool selection) { +void display(struct pile* h, struct pile* p, int handcurline, int playedcurline, char* curmenu,bool selection) { tb_clear(); int i; int j; for (i=0;i<h->cardCount;i++) { - if (i==curline && curmenu=="hand") { + if (i==handcurline && curmenu=="hand") { if (selection) { tb_printf(0, i, TB_BLACK, TB_BLUE, h->cards[i]); } else { @@ -55,7 +72,7 @@ void display(struct pile* h, struct pile* p, int curline, char* curmenu,bool sel } tb_printf(0, i, TB_DEFAULT, TB_BLACK, "------"); for (j=0;j<p->cardCount;j++) { - if (j==curline && curmenu=="played") { + if (j==playedcurline && curmenu=="played") { if (selection) { tb_printf(0, i+j+1, TB_BLACK, TB_BLUE, p->cards[j]); } else { @@ -69,7 +86,6 @@ void display(struct pile* h, struct pile* p, int curline, char* curmenu,bool sel int main(int argc, char **argv) { struct tb_event ev; - int curline = 0; FILE *fp = fopen("deck", "r"); @@ -79,6 +95,7 @@ int main(int argc, char **argv) { struct pile *p=&played; played.cards=malloc(LIST_SIZE); played.cardCount=0; + played.curline=0; tb_init(); bool selection=false; @@ -86,77 +103,43 @@ int main(int argc, char **argv) { char* curmenu="hand"; while(quit) { - display(h,p,curline,curmenu,selection); + display(h,p,hand.curline,played.curline,curmenu,selection); tb_poll_event(&ev); switch(ev.key) { /* up */ case 65517: if (curmenu=="hand") { - if (curline>0) { - if (selection) { - char* tmp; - tmp=hand.cards[curline]; - hand.cards[curline]=hand.cards[curline-1]; - hand.cards[curline-1]=tmp; - } - curline--; - } + moveselection(h,"up",selection); } else if (curmenu=="played") { - if (curline>0) { - if (selection) { - char* tmp; - tmp=played.cards[curline]; - played.cards[curline]=played.cards[curline-1]; - played.cards[curline-1]=tmp; - } - curline--; - } + moveselection(p,"up",selection); } break; /* down */ case 65516: if (curmenu=="hand") { - if (curline<hand.cardCount-1) { - if (selection) { - char* tmp; - tmp=hand.cards[curline]; - hand.cards[curline]=hand.cards[curline+1]; - hand.cards[curline+1]=tmp; - } - curline++; - } + moveselection(h,"down",selection); } else if (curmenu=="played") { - if (curline<played.cardCount-1) { - if (selection) { - char* tmp; - tmp=played.cards[curline]; - played.cards[curline]=played.cards[curline+1]; - played.cards[curline+1]=tmp; - } - curline++; - } + moveselection(p,"down",selection); } break; /* <- */ case 65515: if (curmenu=="played") { if (selection) { - addcardtopile(played.cards[curline],h); - removecardofpile(curline,p); + addcardtopile(played.cards[played.curline],h); + removecardofpile(played.curline,p); } curmenu="hand"; - curline=hand.cardCount-1; } break; /* -> */ case 65514: if (curmenu=="hand") { if (selection) { - addcardtopile(hand.cards[curline],p); - removecardofpile(curline,h); + addcardtopile(hand.cards[hand.curline],p); + removecardofpile(hand.curline,h); } curmenu="played"; - curline=played.cardCount-1; } break; case 13: